User Tools

Site Tools


base:signed_8bit_16bit_addition

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
base:signed_8bit_16bit_addition [2018-09-28 03:58] white_flamebase: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 
-</code> 
- 
-The following version is 2 bytes shorter & 2 cycles faster, using .X: 
  
 <code 6502tasm> <code 6502tasm>
Line 34: Line 15:
  txa         ; .X is the high byte  txa         ; .X is the high byte
  adc value+1  adc value+1
 + sta value+1
 +</code>
 +
 +The following version uses only the accumulator, adding 2 bytes and 1 cycle:
 +<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
 </code> </code>
base/signed_8bit_16bit_addition.1538099890.txt.gz · Last modified: 2018-09-28 03:58 by white_flame