Page 1 of 1
I thought i knew how to add and subtract....
Posted: Sat Sep 22, 2007 11:03 am
by Laserbeak43
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: Select all
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: Select all
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.
Posted: Sat Sep 22, 2007 11:11 am
by strangenesfreak
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.
Posted: Sat Sep 22, 2007 11:13 am
by doppelganger
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.
Posted: Sat Sep 22, 2007 11:15 am
by Laserbeak43
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?
Posted: Sat Sep 22, 2007 11:17 am
by strangenesfreak
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.
Posted: Sat Sep 22, 2007 11:25 am
by Laserbeak43
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?
Posted: Sat Sep 22, 2007 11:27 am
by doppelganger
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.
Posted: Sat Sep 22, 2007 11:35 am
by Laserbeak43
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?

Posted: Sat Sep 22, 2007 11:46 am
by doppelganger
Ummm, yeah, sure. I guess.
Posted: Sat Sep 22, 2007 2:11 pm
by Disch
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:
4+7 = 11, so you write a 1 in the "ones" column.. and put
a carry up in the tens column:
you then add 1+2 for the tens...
plus an additional 1 for the carry:
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
Posted: Sat Sep 22, 2007 7:32 pm
by Laserbeak43
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:
4+7 = 11, so you write a 1 in the "ones" column.. and put
a carry up in the tens column:
Code: Select all
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:
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
Posted: Sat Sep 22, 2007 10:55 pm
by tokumaru
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.
Posted: Fri Sep 28, 2007 5:35 am
by Laserbeak43
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