This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

I thought i knew how to add and subtract....

I thought i knew how to add and subtract....
by on (#27144)
Hi there,
I thought i knew and understood how to add and subtract using the aritmetic instructions ADC and SEC, but these docs seem tp be proving me wrong.

ok in addition you CLC - clear tha carry flag and in subtraction you SEC - set the carry flag. point taken.
but whats this??
Code:
 A 16-bit addition routine.  $20,$21 + $22,$23 = $24,$25
           CLC         clear the carry
           LDA $20     get the low byte of the first number
           ADC $22     add to it the low byte of the second
           STA $24     store in the low byte of the result
           LDA $21     get the high byte of the first number
           ADC $23     add to it the high byte of the second, plus carry
           STA $25     store in high byte of the result



22 + 20 = 24?

and in subtraction? whats this about?
Code:
Ex.2
   ----
        A 16-bit subtraction routine.  $20,$21 - $22,$23 = $24,$25

           SEC         clear the carry
           LDA $20     get the low byte of the first number
           SBC $22     add to it the low byte of the second
           STA $24     store in the low byte of the result
           LDA $21     get the high byte of the first number
           SBC $23     add to it the high byte of the second, plus carry
           STA $25     store in high byte of the result



sorry if i'm being an meganoob here, but this makes no sense to me.

by on (#27146)
I believe you need to check the carry flag when you add the low bytes together. If it's set, you add 1 to the sum of the high bytes. As for subtraction, I believe you need to check the carry flag when you subtract the low bytes. If it's clear, subtract 1 from the difference of the high bytes.

by on (#27147)
You don't really need to check the carry flag inbetween bytes. You just gotta not modify the carry flag until all your adds or subtracts are done. So yeah, the carry flag was designed with that in mind.

by on (#27148)
strangenesfreak wrote:
I believe you need to check the carry flag when you add the low bytes together. If it's set, you add 1 to the sum of the high bytes. As for subtraction, I believe you need to check the carry flag when you subtract the low bytes. If it's clear, subtract 1 from the difference of the high bytes.

no, that's not the problem(yet)
i think i was looking at
LDA $20
ADC $22
etc...

as values and not addresses. if these are actually addresses then it would seem to make more sense to me. am i correct.? these are addresses?

by on (#27149)
Laserbeak43 wrote:
i think i was looking at
LDA $20
ADC $22
etc...

as values and not addresses. if these are actually addresses then it would seem to make more sense to me. am i correct.? these are addresses?

Yeah, those would supposed to be addresses instead of values, zero-page addresses to be exact. Values in assembly have a # sign and $xx, addresses just have $xx for zeropage or $xxxx, no # sign.

by on (#27150)
doppelganger wrote:
You don't really need to check the carry flag inbetween bytes. You just gotta not modify the carry flag until all your adds or subtracts are done. So yeah, the carry flag was designed with that in mind.


so i might have hear wrong or misunderstood, but the carry flag needs to be cleared or set before you tell the assembler to perform arithmetic, but do you need to do something after it's done? or will it take care of my stupid self?

by on (#27151)
Yes it does need to be clear before adding and set before subtracting in order to start off properly, but it's supposed to be left as-is when adding or subtracting a number larger than one byte. What you do with the carry flag after all the adds and subtracts are done is up to you.

by on (#27152)
doppelganger wrote:
Yes it does need to be clear before adding and set before subtracting in order to start off properly, but it's supposed to be left as-is when adding or subtracting a number larger than one byte. What you do with the carry flag after all the adds and subtracts are done is up to you.


ok i can willingly bug my program is what you're saying? :)

by on (#27153)
Ummm, yeah, sure. I guess.

by on (#27157)
Think back to 1st grade when you were learning how to add. They teach you to add the "ones column" first, then add the "tens column". Example:

Code:
 14
+27
---


4+7 = 11, so you write a 1 in the "ones" column.. and put a carry up in the tens column:

Code:
 1    <---  the carry
 14
+27
---
  1


you then add 1+2 for the tens... plus an additional 1 for the carry:

Code:
 1
 14
+27
---
 41


Addition on the NES is the exact same thing. Only instead of a ones column and 10s column... it's low byte and high byte. you want the carry from the low byte addition to be added into the high byte addition.

A practical example:

$03FF
+$0402
----------

you first CLC because you don't want to start with any carry. Then you add the low bytes ($FF + $02)... this results in $01 with carry (the 01 will be the low byte of the sum). You then add $03 and $04, but with the carry -- this results in $08

giving you a final sum of $0801

by on (#27169)
Disch wrote:
Think back to 1st grade when you were learning how to add. They teach you to add the "ones column" first, then add the "tens column". Example:

Code:
 14
+27
---


4+7 = 11, so you write a 1 in the "ones" column.. and put a carry up in the tens column:

Code:

nice explanaition. but did you have  to take it back to first grade? that was cold  :D

 1    <---  the carry
 14
+27
---
  1


you then add 1+2 for the tens... plus an additional 1 for the carry:

Code:
 1
 14
+27
---
 41


Addition on the NES is the exact same thing. Only instead of a ones column and 10s column... it's low byte and high byte. you want the carry from the low byte addition to be added into the high byte addition.

A practical example:

$03FF
+$0402
----------

you first CLC because you don't want to start with any carry. Then you add the low bytes ($FF + $02)... this results in $01 with carry (the 01 will be the low byte of the sum). You then add $03 and $04, but with the carry -- this results in $08

giving you a final sum of $0801

by on (#27176)
Laserbeak43 wrote:
doppelganger wrote:
What you do with the carry flag after all the adds and subtracts are done is up to you.

ok i can willingly bug my program is what you're saying? :)

I think he means that after proper addition/subtraction (clear or set the carry at the start), the result will be correct without you having to do anything with the carry. But the carry can tell some things about the operation that just took place, and it's up to you to use that or not.

You can use the carry to detect overflows and underflows. After a subtraction, the carry flag can tell you if the second number was larger than the first. You may not be interested in any of this right now, but sometimes that can be very useful.

by on (#27252)
tokumaru wrote:
Laserbeak43 wrote:
doppelganger wrote:
What you do with the carry flag after all the adds and subtracts are done is up to you.

ok i can willingly bug my program is what you're saying? :)

I think he means that after proper addition/subtraction (clear or set the carry at the start), the result will be correct without you having to do anything with the carry. But the carry can tell some things about the operation that just took place, and it's up to you to use that or not.

You can use the carry to detect overflows and underflows. After a subtraction, the carry flag can tell you if the second number was larger than the first. You may not be interested in any of this right now, but sometimes that can be very useful.


that's good to know thanks