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 [2017-10-26 07:30] 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.  We hold the high byte of the delta in .X and set it to $00 or $ff based on its sign:+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.
  
-<code asm+<code 6502tasm> 
- ldx #$00  ; implied high byte of delta+ ; Precalculate the sign-extended high byte in .X 
 + ldx #$00
  lda delta  lda delta
  bpl :+  bpl :+
-  dex      ; high byte becomes $ff to reflect negative delta +  dex        decrement high byte to $ff for a negative delta 
-:clc +
- adc value normal 16-bit addition+ 
 + Normal 16-bit addition 
 + clc 
 + adc value   ; .A still holds delta
  sta value  sta value
- txa+ txa         ; .X is the high byte
  adc value+1  adc value+1
  sta value+1  sta value+1
 </code> </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
 +</code>
 +
  
  
 ---- ----
 White Flame White Flame
base/signed_8bit_16bit_addition.1508995836.txt.gz · Last modified: 2017-10-26 07:30 by white_flame