Introduction

When expanding a signed two's complement number to a larger number of bits, it is necessary to replicate the sign bit into the newly added bits to maintain the value. So, for example, 40 ($28) is extended to $0028, and -40 ($d8) is extended to $ffd8. There's a trick to do this using the SBC opcode.

Extending an 8-bit signed number to 16 bits

; Extend E to DE
   LD     A, E
   RLCA            ; Move sign bit into carry flag
   SBC    A, A     ; A is now 0 or 11111111, depending on the carry
   LD     D, A

Extending a 16-bit signed number to 32 bits

; Extend DE to HLDE
   LD     H, D
   LD     L, E
   ADD    HL, HL   ; Move sign bit into carry flag
   SBC    HL, HL   ; HL is now 0 or 11111111 11111111, depending on the carry

References

Learn TI-83 Plus Assembly In 28 Days - Day 15 - Advanced Math




Return to top
0.09s