Search code examples
assembly8085

8085 assembly: sign extend 8-bit to 16-bit


How to sign extend a group of 8 bit numbers into 16 bits and store them in little endian format. For example I have following data in my memory location.

Address = Value
0001    = 03 [counter]
0002    = 05
0003    = 43
0004    = 8C

Results:

Address = Value
0005    = 05 \ 05 => 00 05
0006    = 00 /
0007    = 43 \ 43 => 00 43
0008    = 00 /
0009    = 8C \ 8C => FF 8C
000A    = FF /

I'm currently stuck on the following code:

LXI D,0005H [memory location to store little endian]
LXI H,0001H 
MOV C,M  [initialize counter]
INX H  [increment 1 and point to first data]
MOV A,M
CALL EXPAND
HLT

Expand: PUSH B
PUSH H
checkMSB: ANI 80H  [Check the MSB to determine expand number whether is 00 or FF]
JZ SKIP
..... [still on process]

SKIP: STAX D [stuck at here]
INX H
MOV A,M
DCR C
JNZ checkMSB
POP H
POP B
HLT

Solution

  • This would also be some possibility:

       LXI  D, 0005H ;; destination
       LXI  H, 0001H ;; source
       MOV  C,M      ;; load counter
    ; --- only needed if counter may be 0 ---
       MOV  A, C     ;; copy C to A
       ORA  A        ;; check if A is 0
       JZ   done
    ; --- ---
    loop:
       INX  H        ;; inc pointer to next value
       MOV  A,M        ;; read 8-bit value
       STAX  D        ;; write low word
       INX  D        ;; increment pointer
       ADI  80h     ;; this will set CF if HSb is set
       SBB  A        ;; will result in 0FFh if CF set, 0 else
       STAX  D        ;; write high word
       INX  D        ;; increment pointer
       DCR  C        ;; dec counter
       JNZ  loop
    done: