| |
Trifox Account closed
Registered: Mar 2006 Posts: 108 |
16 bit add/sub with sign !
hi all, i am in need of a 16 bit signed add/sub command, i am a bit lazy to search and try out, can any one point me to some piece of example code ?
or leave some words about that?
thx
|
|
| |
tlr
Registered: Sep 2003 Posts: 1790 |
For 2-complement values, unsigned/signed addition (and subtraction) are the same. Possibly you would want different handling of the overflow flag, but normally not.
something like...
LDA $F9
CLC
ADC $FB
STA $FD
LDA $FA
ADC $FC
STA $FE
...will add $f9/$fa to $fb/$fc and place the result in $fd/$fe. |
| |
Trifox Account closed
Registered: Mar 2006 Posts: 108 |
great thank you, i will try it out, i was fiddling around with sec/clc a bit too often ... ;) thx |
| |
tlr
Registered: Sep 2003 Posts: 1790 |
Note that for SBC the carry works the opposite way, i.e use SEC. |
| |
Trifox Account closed
Registered: Mar 2006 Posts: 108 |
thank you, and now how is negating of a 16bit number done ?!?!
building the 2complement,
sec
lda mynumber_hi
eor #$ff
sta mynumber_hi
lda mynumber_lo
eor #$ff
sbc #1
sta mynumber_lo
? |
| |
tlr
Registered: Sep 2003 Posts: 1790 |
Theory: invert all bits and add 1.
Practice (0-value):
LDA #$00
SEC
SBC $FB
STA $FB
LDA #$00
SBC $FC
STA $FC
I'm sure someone will shave a couple of bytes from this though... ;) |
| |
Trifox Account closed
Registered: Mar 2006 Posts: 108 |
great, thank you ! |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
when coding 3d math stuff, I never really noticed a difference wether I substracted 1 or not. However you'd better do it. |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
@Oswald: Maybe your 16bit values were fixed point 8.8 values? Then adding 1 or not is simply adding 1/256 to the value or not which in most cases wouldn't be noticable. |
| |
Krill
Registered: Apr 2002 Posts: 2980 |
Trifox: When NOTting the number and then subtracting 1, take care of the possible underflow. Do it like this:
sec
lda mynumber_lo
eor #$ff
sbc #$01
sta mynumber_lo
lda mynumber_hi
eor #$ff
sbc #$00
sta mynumber_hi |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
Jack, rite:) |
... 6 posts hidden. Click here to view all posts.... |
Previous - 1 | 2 - Next |