Programming with Ophis | ||
---|---|---|
<<< Previous | The Second Step | Next >>> |
Time to use the carry bit for what it was meant to do. Adding two 8 bit numbers can produce a 9-bit result. That 9th bit is stored in the carry flag. The ADC command adds the carry value to its result, as well. Thus, carries work just as we'd expect them to. Suppose we're storing two 16-bit values, low byte first, in $C100-1 and $C102-3. To add them together and store them in $C104-5, this is very easy:
CLC LDA $C100 ADC $C102 STA $C104 LDA $C101 ADC $C103 STA $C105 |
Subtraction is identical, but you set the carry bit first with SEC (because borrow is the complement of carry—think about how the unsigned compare works if this puzzles you) and, of course, using the SBC instruction instead of ADC.
The carry/borrow bit is set appropriately to let you continue, too. As long as you just keep working your way up to bytes of ever-higher significance, this generalizes to 24 (do it three times instead of two) or 32 (four, etc.) bit integers.
<<< Previous | Home | Next >>> |
Unsigned arithmetic | Up | 16-bit comparisons |