Sign in to follow this  
ed209

trouble with assembly

Recommended Posts

I don't understand the "Add with Carry" and how the carry bit works in 6502. Could someone please give me an example of addition that requires a carry and show me where the carry bit is located? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by ed209
I don't understand the "Add with Carry" and how the carry bit works in 6502. Could someone please give me an example of addition that requires a carry and show me where the carry bit is located?

Thanks.


Any addition requires a carry wherever the result is too big to fit in the output register. For example, if you add.b 250 and 10, the result is 260, and that doesn't fit in a byte (which ranges 0-255). So the carry bit gets set, and 4 (the lowest 8 bits of the result) is stored in the register.

The carry bit is a status flag, like the zero bit used for conditional branching. You can think of it, though, as an extra bit that's magically always just to the left of whatever destination register you just used. For a byte addition, it represents the '256s' place in the binary representation of the result. (I'm assuming unsigned values for this discussion, but it's really the same with signed ones, thanks to the magic of 2s-complement.)

(With multiplication, things are bit trickier, because the result could overflow by a lot more.)

Share this post


Link to post
Share on other sites
Add with carry is usually used for multi-word arithmetic. For instance if your processor has 8-bit registers (such as the 6502) and you want to add two 16-bit numbers together you'd add the low bytes together without carry (i.e. ADC with the carry bit cleared), then add the high bytes with carry. This allows the carry from the low byte addition to be included in the high byte sum.

The carry bit is located in the processor status register on 6502. You clear it with CLC and set it with SEC.

Carry is also used for subtract (called subtract with borrow).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this