subtracting numbers

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Maca
Posts: 9
Joined: Wed Jan 17, 2007 12:47 pm

subtracting numbers

Post by Maca »

i need to subtract the number in $4031 from the number in $4030.
if the result is zero - store in $4032
if the result is negative - store in $4033
if the result is positive - store in $4034

Code: Select all

   
4000 lda  $4030
4003 sec 
4004 sbc  $4031
4007 beq  $07
4009 bmi  $09
400a sta  $4034
400d brk
400e sta  $4032
4011 brk
4012 sta  $4033
4015 brk
is that how it's done? i'm unsure about 2 branches one after another. thanks.
User avatar
Bregalad
Posts: 8036
Joined: Fri Nov 12, 2004 2:49 pm
Location: Caen, France

Post by Bregalad »

I think you'd want to have lablels for your branches, and why do you use those stupid BRKs ? This will trigger an IRQ and skip the next byte once IRQ returns, wich is probably not what you want to do.
Useless, lumbering half-wits don't scare us.
Maca
Posts: 9
Joined: Wed Jan 17, 2007 12:47 pm

Post by Maca »

yeah, very helpful thanks. :roll:
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

Assuming you're treating your numbers as signed... your code is conceptually correct -- but your branches are off (they're both 1 byte too much)

you'd want

beq $06
bmi $08

in this case. But you should use labels for this anyway to avoid these kinds of mistakes (and because it's just all around easier)

If you're treating your numbers as unsigned (ie: $FF is 255 and not -1), then this code will not quite work. You'd want a BCC instead of BMI in that case.


Also -- if the numbers on the left are supposed to be offsets, they're all wrong. (BMI takes two bytes, so if it is at $4009, STA $4034 would start at $400B, not $400A)

Plus... your address range is not typical for NES work. On the NES, $4000-$4017 is CPU/APU registers and the rest of $4xxx is generally open bus. So this isn't an area where you would ever have code or RAM.
albailey
Posts: 177
Joined: Thu Jul 13, 2006 3:15 pm

Post by albailey »

Any time I try out new assembly, I use 6502 simulator

http://home.pacbell.net/michal_k/6502.html

When I drop in your code (and use labels) it seems to work fine.

Al
Maca
Posts: 9
Joined: Wed Jan 17, 2007 12:47 pm

Post by Maca »

I see now, thanks.
Post Reply