Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > 16 bit add/sub with sign !
2006-05-01 19:30
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

2006-05-01 20:26
tlr

Registered: Sep 2003
Posts: 1814
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.
2006-05-01 20:30
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
2006-05-01 20:43
tlr

Registered: Sep 2003
Posts: 1814
Note that for SBC the carry works the opposite way, i.e use SEC.
2006-05-01 20:50
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
?
2006-05-01 20:58
tlr

Registered: Sep 2003
Posts: 1814
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... ;)
2006-05-01 21:33
Trifox
Account closed

Registered: Mar 2006
Posts: 108
great, thank you !
2006-05-02 06:30
Oswald

Registered: Apr 2002
Posts: 5127
when coding 3d math stuff, I never really noticed a difference wether I substracted 1 or not. However you'd better do it.
2006-05-02 08:09
JackAsser

Registered: Jun 2002
Posts: 2038
@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.
2006-05-02 08:36
Krill

Registered: Apr 2002
Posts: 3098
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
2006-05-02 11:43
Oswald

Registered: Apr 2002
Posts: 5127
Jack, rite:)
 
... 6 posts hidden. Click here to view all posts....
 
Previous - 1 | 2 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Holy Moses/Role
Acidchild/Padua
Krill/Plush
Herrera64
The MeatBall
Doc Strange/Atlantis
GuyGavin/HF
Guests online: 136
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Codeboys & Endians  (9.7)
4 Mojo  (9.6)
5 Coma Light 13  (9.6)
6 Edge of Disgrace  (9.6)
7 Signal Carnival  (9.6)
8 Wonderland XIV  (9.5)
9 Uncensored  (9.5)
10 Comaland 100%  (9.5)
Top onefile Demos
1 Nine  (9.7)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.5)
6 Scan and Spin  (9.5)
7 Onscreen 5k  (9.5)
8 Grey  (9.5)
9 Dawnfall V1.1  (9.5)
10 Rainbow Connection  (9.5)
Top Groups
1 Artline Designs  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Performers  (9.3)
5 Censor Design  (9.3)
Top Fullscreen Graphicians
1 Joe  (9.7)
2 Sulevi  (9.6)
3 The Sarge  (9.6)
4 Veto  (9.5)
5 Facet  (9.5)

Home - Disclaimer
Copyright © No Name 2001-2025
Page generated in: 0.042 sec.