6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Jun 16, 2024 9:33 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Fri Jan 21, 2022 11:04 pm 
Offline

Joined: Sun Jul 11, 2021 9:12 am
Posts: 140
Hi Guys,

I'm trying to get my head around adding numbers that a greater than 8 bit.

I've managed to get 16 bit addition working to an extent.

Here I'm adding 6000 to the previous contents of result, which is working ok if the result is below 65536.

Code:
        clc

        lda #<6000
        adc result + 0
        sta result + 0       ; store sum of LSBs
        lda #>6000
        adc result + 1       ; add the MSBs using carry
        sta result + 1


I'll only ever be adding 16 bit number at maximum. How would I go about expanding this to flow over in to a 24 bit result? I have provision for "result + 2" allocated in RAM.

I've tried a few different things but am struggling to get a 24 bit result.

I thought (logically) something like this at the end of the code, but that doesn't seem to work at all.

Code:
        adc #0            ; Add the overflow to highest byte
        sta result + 2


Math on the 6502 is definitely not my strong point and is one of those areas that always trips me up.

Any advice is greatly appreciated. :D


[Edited to reflect that "CLC" was being called prior to the routine - Pointing this out to reflect the context of Garth's reply]


Last edited by J64C on Fri Jan 21, 2022 11:42 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 21, 2022 11:30 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8458
Location: Southern California
Don't forget to start with the carry flag in its desired state, which will usually be clear. (Don't clear it between bytes though.)

You can extend the same process as far as you want; but in this case, since you don't have a third byte to do, the carry flag becomes bit 0 of an otherwise 00 high byte. If you want the 3-byte variable, result+2 will start out clear anyway, so you can do something like
Code:
        clc

        lda  #<6000
        adc  result
        sta  result           ; store sum of LSBs

        lda  #>6000
        adc  result + 1       ; add the mid bytes, with carry
        sta  result + 1

        bcc  label
        inc  result + 2       ; carry into high byte if appropriate
label:

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 21, 2022 11:40 pm 
Offline

Joined: Sun Jul 11, 2021 9:12 am
Posts: 140
Thanks Garth! I'll give that a try.

I should have clarified that I am in fact doing "CLC" in the line above the snippet of code up to (I'll edit and add it now).

So is the method of the ADC in my second code snippet failing because the carry bit is already (potentially) set from the addition prior to it?


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 21, 2022 11:45 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8458
Location: Southern California
J64C wrote:
So is the method of the ADC in my second code snippet failing because the carry bit is already (potentially) set from the addition prior to it?

You're adding 0, with carry, to the result of the middle byte which was still in the accumulator, instead of to an initially 0 high byte.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 22, 2022 12:21 am 
Offline

Joined: Sun Jul 11, 2021 9:12 am
Posts: 140
Thanks again, your solution works well and I understand where you have gone with it there.

I'll have to get out the pencils and paper to fully understand your reasoning as to why my method failed. Which is not a bad thing, I'm sure it will click when I analyse it much deeper.


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 22, 2022 12:32 am 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
J64C wrote:
Code:
        adc #0            ; Add the overflow to highest byte
        sta result + 2

Garths listing is excellent if you are keeping a running tally, but if you just want to add two 16-bit numbers and know the immediate total, then the code should be:
Code:
      lda #0
      adc #0
      sta result + 2

Otherwise you risk having a random value in the RESULT+2 position without zeroing it first.

The real code to prevent addition errors should be:
Code:
init  lda #0
       sta result
       sta result+1
       sta result+2

subseq.entries equ *
       clc
        lda #<6000
        adc result + 0
        sta result + 0       ; store sum of LSBs
        lda #>6000
        adc result + 1       ; add the MSBs using carry
        sta result + 1
        bcc label
        inc result+2
label


Last edited by IamRob on Sat Jan 22, 2022 12:48 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 22, 2022 12:40 am 
Offline

Joined: Sun Jul 11, 2021 9:12 am
Posts: 140
Thanks Rob!

Yeah, in my particular case I'm adding back to the 24 bit number each time.

Code:
// Pseudocode

24bitVal = 24bitVal + 16bitVal


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 26 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: