Jump to content



0

subtraction problem


8 replies to this topic

#1 antron OFFLINE  

antron

    Chopper Commander

  • 196 posts

Posted Fri Jan 14, 2005 11:42 AM

i have a ram location (wchman_L) that holds the low byte of a pointer to a graphic
if that pointer is pointing to a certain graphic (#
this works fine:
lda wchman_L 

sec

sbc #<MAN4_L 

beq reset



but this does not:
lda #<MAN4_L   

sec

sbc wchman_L

beq reset

why?

#2 antron OFFLINE  

antron

    Chopper Commander

  • 196 posts

Posted Fri Jan 14, 2005 11:50 AM

my bad

the difference was the state that it left the carry in.
i got it.

#3 Nukey Shay ONLINE  

Nukey Shay

    Sheik Yerbouti

  • 20,458 posts
  • Location:The land of Gorch

Posted Sat Jan 15, 2005 1:31 AM

I don't understand...why not just use CMP?

lda wchman_L 

cmp #<MAN4_L 

beq reset



...and you'll save a byte+2 cycles

#4 CPUWIZ OFFLINE  

CPUWIZ

    Rarity 11

  • 26,327 posts
  • Cartridge Recycler
  • Location:SoCal

Posted Sat Jan 15, 2005 1:42 AM

Nukey Shay said:

I don't understand...why not just use CMP?

lda wchman_L 

cmp #<MAN4_L 

beq reset



...and you'll save a byte+2 cycles

Perhaps because the algorithm is something like this ...


lda wchman_L

sec

sbc #<MAN4_L

beq reset



asl

tay

lda Location,y

.

.

blah


You never know. ;)

#5 antron OFFLINE  

antron

    Chopper Commander

  • 196 posts

Posted Fri Jan 28, 2005 12:48 PM

in my case Nukey is right, thanks.

but i don't understand why my original two cases gave different behavior.

how could they leave the carry in different states?

Edited by antron, Wed May 11, 2005 2:14 PM.


#6 Tom OFFLINE  

Tom

    Moonsweeper

  • 449 posts
  • Location:Switzerland

Posted Fri Jan 28, 2005 1:38 PM

basic math : subtraction isn't commutative.

#7 vdub_bobby OFFLINE  

vdub_bobby

    Quadrunner

  • 5,831 posts
  • Boom bam.
  • Location:Seattle, WA

Posted Fri Jan 28, 2005 1:51 PM

Tom said:

basic math : subtraction isn't commutative.
Yeah, but if you are testing for equality it doesn't matter.

If A==B, A-B==B-A==0

If A!=B, A-B!=0 and B-A!=0 even though A-B!=B-A

#8 vdub_bobby OFFLINE  

vdub_bobby

    Quadrunner

  • 5,831 posts
  • Boom bam.
  • Location:Seattle, WA

Posted Fri Jan 28, 2005 2:00 PM

[quote=antron]in my case he is right, thanks.

but i don't understand why my original two cases gave different behavior.

how could they leave the carry in different states?[/quote]
if #
This is because after a sbc the carry is cleared if the subtraction needed a borrow (i.e., if the result is negative, assuming unsigned numbers) and the carry will be set if the subtraction didn't need a borrow.

In other words,

If A > B, then A - B will leave the carry set, but B - A will clear the carry.

See here: [url="http://www.6502.org/tutorials/6502opcodes.htm#SBC"]http://www.6502.org/tutorials/6502opcodes.htm#SBC[/url]
and here:http://www.geocities.com/oneelkruns/asm1step.html[/quote]

#9 antron OFFLINE  

antron

    Chopper Commander

  • 196 posts

Posted Fri Jan 28, 2005 2:40 PM

makes sense now.
it is the case when they were not equal that things went wrong.

thanks all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users