6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 1:10 am

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Fri Dec 14, 2012 5:38 pm 
Offline

Joined: Fri Dec 14, 2012 5:33 pm
Posts: 2
I'm working on re-creating a bit of code in 6502 assembly for a large project I'm working on. This one little line has sort of stumped me.

steep := abs(y1 - y0) > abs(x1 - x0)

I'm sure you can see what is supposed to happen here. The trouble is the need for absolutes. Now obviously I can create a routine that would have several CMP instructions to figure out which variable is larger and then reverse them before performing the subtraction. But it just seems to be there must be an easier way to accomplish this.

First of all, none of the variables will be larger than 255 so I don't need to deal with any 16-bit numbers. All I really need is to find the quickest way to determine the difference between y1 and y0, regardless of which one is larger.

Any suggestions?


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 6:21 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
As subtract and compare are pretty much the same thing, I'd suggest
Subtract
Check for negative
Conditionally negate

Note that subtracting two 8 bit unsigned numbers gives you a 9 bit result, so the carry bit would act as your sign bit.

Note your abs result is in the range 0 to 255. I'm not sure if a single byte result just falls out. When you do the comparison, you again get into 9 bit territory, but as you only want the sign, that's OK.


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 6:34 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
BTW, Welcome!
I just hacked some code and it seems to work.
As I have a thing about not doing people's homework for them, I'll show you the code if you can show that this isn't like that. Sorry for the inconvenience!
Cheers
Ed


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 6:43 pm 
Offline

Joined: Fri Dec 14, 2012 5:33 pm
Posts: 2
BigEd wrote:
BTW, Welcome!
I just hacked some code and it seems to work.
As I have a thing about not doing people's homework for them, I'll show you the code if you can show that this isn't like that. Sorry for the inconvenience!


I'm 37 years old and haven't been to school since I was 18.. Not sure how to "prove" that to you. I'm a hobby programmer on the C64. Come to think of it, I'd be shocked to learn that any 6502 code is still being taught in school since as far as I know, nobody uses it anymore in current products.

I'm actually trying to code my own line-drawing routine for a game I'm working on using a modified form of "Bresenham's line algorithm" I have the routine working already but it only works when drawing from left to right.


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 6:46 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
I'm convinced! Here's what I have for 'abs':
Code:
$0603    a5 01     LDA $01
$0605    38        SEC
$0606    e5 02     SBC $02
$0608    b0 04     BCS $060e
$060a    49 ff     EOR #$ff
$060c    69 01     ADC #$01
$060e    60        RTS


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 6:48 pm 
Offline
User avatar

Joined: Tue Nov 16, 2010 8:00 am
Posts: 2353
Location: Gouda, The Netherlands
If you're doing Bresenham, you'll probably also need to know if y1 > y0 and if x1 > x0. Maybe you can combine that with the 'steep' calculation.


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 6:57 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8544
Location: Southern California
Also, the V flag (overflow) tells if the sign of the result is wrong, like if you subtract a positive number from a negative one and the answer is so negative that the high bit (sign bit) is again clear. There's an article on the V flag at http://6502.org/tutorials/vflag.html

Quote:
Come to think of it, I'd be shocked to learn that any 6502 code is still being taught in school since as far as I know, nobody uses it anymore in current products.

You might be pleasantly surprised to know that yes, it is being taught in schools, and that the 6502 is being produced in volumes of hundreds of millions of units per year today, although they're not very visible because the products are things like engine controls instead of desktop computers. The fastest ones are running over 200 MHz. This is according to the IP owner, Western Design Center, whose main business is licensing IP to manufacturers, rather than selling hardware. It is also my understanding that the 6502 is the only processor approved for certain life-support equipment, including human-body-implantable.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 7:06 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
(Hmm, in this case I think V is no use, because it relates to signed 8-bit inputs. I took it from the problem description that we have unsigned 8-bit inputs)

(Indeed, I've seen two 6502-related elements to college courses!)


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2012 10:21 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8507
Location: Midwestern USA
GARTHWILSON wrote:
adric22 wrote:
Come to think of it, I'd be shocked to learn that any 6502 code is still being taught in school since as far as I know, nobody uses it anymore in current products.

You might be pleasantly surprised to know that yes, it is being taught in schools...

6502 assembly language is an excellent model for teaching purposes. The nearly orthogonal instruction set is one of the cleanest in existence and is surprisingly complete for a such a low-cost device (when introduced, the 6502 was about one-tenth the cost of the Motorola 6800). The 65C816 instruction set is even more powerful than its eight bit brethren, with a set of very useful stack instructions.

Quote:
It is also my understanding that the 6502 is the only processor approved for certain life-support equipment, including human-body-implantable.

That the 65C02 and 65C816 are both static core devices was the result of WDC seeking medical certification for the devices. One of the members of our large scale model railroad club has an implanted defibrillator designed around the W65C02S. He's on a list awaiting a heart transplant and that defib unit has rescued him several times when his ticker stopped ticking. I was...er...shocked to learn all this. :lol:

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 19, 2012 3:18 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
The N flag XOR the V flag gives you a signed comparison (less than) result, as described here:

http://6502.org/tutorials/compare_beyond.html


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 19, 2012 10:16 am 
Offline
User avatar

Joined: Mon Dec 08, 2008 6:32 pm
Posts: 143
Location: Brighton, England
If you're doing Bresenham Line algorithms, I suggest you download the Acorn Atom MOS ROM disassembly from the net. (At this address http://www.atomclub.nl/atom_handleidingen/splitatom/F000.txt) This fully-annotated disassembly include a set of line algorithms optimised for speed that are still easy to understand. Since the Atom's maximum screen resolution was 256x192, these routines work in only 8-bits as you want.

These listings are in Atom assembler format rather than standard 6502 format and so use # to indicate a hex number and @ to indicate immediate addressing.

EDIT - The line plot routine starts at address F5B5

_________________
Shift to the left,
Shift to the right,
Mask in, Mask Out,
BYTE! BYTE! BYTE!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 27 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: