base:signed_8bit_16bit_addition
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
base:signed_8bit_16bit_addition [2018-09-28 04:10] – white_flame | base:signed_8bit_16bit_addition [2018-09-28 05:20] (current) – white_flame | ||
---|---|---|---|
Line 1: | Line 1: | ||
To add a signed 8-bit delta to a 16-bit value, we need to sign-extend the delta to a full 16 bits. The low byte can be added as normal, but the upper byte needs to be $00 or $ff based on the sign of the low byte. | To add a signed 8-bit delta to a 16-bit value, we need to sign-extend the delta to a full 16 bits. The low byte can be added as normal, but the upper byte needs to be $00 or $ff based on the sign of the low byte. | ||
- | |||
- | Using only the accumulator: | ||
- | <code 6502tasm> | ||
- | ; Standard low byte addition | ||
- | clc | ||
- | lda delta | ||
- | adc value | ||
- | sta value | ||
- | |||
- | ; Sign extend the high byte | ||
- | lda delta | ||
- | and #$80 ; Extract the sign bit | ||
- | beq :+ ; If zero, add #$00 (+ carry) | ||
- | lda #$ff ; Else, add $ff (+ carry) | ||
- | :adc value+1 | ||
- | sta value+1 | ||
- | </ | ||
- | |||
- | The following version is 2 bytes shorter & 1 cycle faster, using .X: | ||
<code 6502tasm> | <code 6502tasm> | ||
Line 34: | Line 15: | ||
| | ||
adc value+1 | adc value+1 | ||
+ | sta value+1 | ||
+ | </ | ||
+ | |||
+ | The following version uses only the accumulator, | ||
+ | <code 6502tasm> | ||
+ | ; Standard low byte addition | ||
+ | clc | ||
+ | lda delta | ||
+ | adc value | ||
+ | sta value | ||
+ | |||
+ | ; Sign extend the high byte | ||
+ | lda delta | ||
+ | and #$80 ; Extract the sign bit | ||
+ | beq :+ ; If zero, add #$00 (+ carry) | ||
+ | lda #$ff ; Else, add $ff (+ carry) | ||
+ | :adc value+1 | ||
sta value+1 | sta value+1 | ||
</ | </ |
base/signed_8bit_16bit_addition.txt · Last modified: 2018-09-28 05:20 by white_flame