Jump to content
  • Advertisement
Sign in to follow this  
ed209

trouble with assembly

This topic is 3853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!