trouble with assembly

Started by
2 comments, last by Jerax 16 years, 3 months ago
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.
Advertisement
If you don't have it already, I would suggest getting the official 469 page manual, it should explain everything. I haven't been through all the examples, but I'm sure there's something there that uses ADC with carry.
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.)
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).

This topic is closed to new replies.

Advertisement