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

 
... 6 posts hidden. Click here to view all posts....
 
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:)
2013-07-12 08:33
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?
2013-07-12 11:11
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.
2013-07-12 15:03
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 =)
2013-07-13 19:28
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
2013-07-14 08:31
Peiselulli

Registered: Oct 2006
Posts: 81
I think, the "cpx #8" is a bad idea because of the carry ...
The second solution looks better.
2013-07-14 08:41
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
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
Alakran_64
Durandal
Quetzal/Chrome
Guests online: 88
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 Coders
1 Axis  (9.8)
2 Graham  (9.8)
3 Lft  (9.8)
4 Crossbow  (9.8)
5 HCL  (9.8)

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