| |
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
|
|
... 6 posts hidden. Click here to view all posts.... |
| |
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:) |
| |
Rastah Bar Account closed
Registered: Oct 2012 Posts: 336 |
Hi, I just want to point out a small error in the codebase article about 16 bit addition/subtraction:
http://codebase64.org/doku.php?id=base:16bit_addition_and_subtr..
It says "You can expand the routines below, putting more bytes to the numbers and repeating the lda, adc/sbc, sta block for each of them in order, to create 24-bit, 32-bit or more add and sub routines."
This is not true for 24-bit, etc. subtracion, because if the carry gets cleared after one subtraction, it will affect the subtraction of ALL higher bytes, not only the next one.
Btw, what is the simplest way (most efficient in terms of cycles) to fix this? |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
Quote: Hi, I just want to point out a small error in the codebase article about 16 bit addition/subtraction:
http://codebase64.org/doku.php?id=base:16bit_addition_and_subtr..
It says "You can expand the routines below, putting more bytes to the numbers and repeating the lda, adc/sbc, sta block for each of them in order, to create 24-bit, 32-bit or more add and sub routines."
This is not true for 24-bit, etc. subtracion, because if the carry gets cleared after one subtraction, it will affect the subtraction of ALL higher bytes, not only the next one.
Btw, what is the simplest way (most efficient in terms of cycles) to fix this?
the carry will be set/cleared by the next substraction according to the result correctly, you only have to set carry for the first byte (regardless of doing sbcs or adcs).
lda
clc
adc
sta
lda
adc
sta
lda
adc
sta
...
is correct, same for sbc, only you start with sec. |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
"because if the carry gets cleared after one subtraction, it will affect the subtraction of ALL higher bytes, not only the next one."
and its supposed to work that way, correct =) |
| |
tlr
Registered: Sep 2003 Posts: 1790 |
This also enables handling of very long numbers using loops:
ldx #0
sec
lp: lda zp1,x
sbc zp2,x
inx
cpx #8
bne lp
... or if you are size-coding:
ldx #$f8
sec
lp: lda zp1+8,x
sbc zp2+8,x
inx
bne lp |
| |
Peiselulli
Registered: Oct 2006 Posts: 81 |
I think, the "cpx #8" is a bad idea because of the carry ...
The second solution looks better. |
| |
tlr
Registered: Sep 2003 Posts: 1790 |
Quote: I think, the "cpx #8" is a bad idea because of the carry ...
The second solution looks better.
Ah, should really have spotted that. Thanks for pointing it out peiselulli!
I guess that shows which version I actually use myself... :) |
Previous - 1 | 2 - Next |