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: 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.
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: 1790
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: 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... ;)
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: 5094
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: 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.
2006-05-02 08:36
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
2006-05-02 11:43
Oswald

Registered: Apr 2002
Posts: 5094
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
macx
LordCrass
Guests online: 91
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Organizers
1 Burglar  (9.9)
2 Sixx  (9.8)
3 hedning  (9.7)
4 Irata  (9.7)
5 Tim  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.041 sec.