0

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
FRob
  • 3,883
  • 2
  • 27
  • 40
Sollo
  • 163
  • 11
  • Are you missing the high byte (`00`) to `43` in your example result? – lurker Jan 14 '15 at 17:24
  • @lurker yeap! thanks for remind. – Sollo Jan 14 '15 at 17:27
  • Also, if the first data is at location `0001H`, you stored `0001H` into `H` then did `INX H` saying *increment 1 and point to first data*. But doesn't it now point to the second data byte? – lurker Jan 14 '15 at 17:31
  • @lurker I mean the first data of a group of number, basically `0001H` is for counter and the `0002H` is the first number that I want to expand until `0004H`. – Sollo Jan 14 '15 at 17:33

2 Answers2

0

Do something like

LXI D,DOUT;; destination
LXI H,DIN ;; source
MOV C,M ;; load counter
INR C
LOOP:
INX H ;; inc to next number
DCR C ;; dec counter
JZ DONE
;; load and store lo 8 bit
MOV A,M
STAX D
INX D
;; shift upper bit to carry
RAL
;; A <= A - A - CY, i.e. A <= 0x00 or 0xFF depending on CY
SBB A
;; store hi 8 bit
STAX D
INX D
JMP LOOP
DONE:
JMP DONE

DIN:
03 05 43 8C
DOUT:

You could make use of the stack if your requirements weren't these addresses. Also, possibly some rotating/shifting tricks could be employed. I dislike the syntax, so I might have a typo in there somewhere.

FRob
  • 3,883
  • 2
  • 27
  • 40
  • For your loop part after `DCR C` , you don't have any operator to change the flags so I have no idea what to do with `JZ DONE` – Sollo Jan 14 '15 at 18:28
  • DCR sets the zero flag if C was zero after decrement. If C is zero, Z is set and JZ DONE will jump to DONE. Else, another element is processed. – FRob Jan 14 '15 at 18:30
  • Ok, thanks. And for the `LDA M` , `LDA` can be only used like `LDA A000H` which means follow by memory address – Sollo Jan 14 '15 at 18:40
  • You can also condense MVI A, 00H; SBI 00H into SBB A, like Martin Rosenau did. – FRob Jan 14 '15 at 21:11
  • I did that and change to MOV A,M and replace all your STA D into STAX D because it is the same concept with LDA [memory location].But there's still have a problem with it does not copy the memory location of address. For example I move the content of 0002, but it stores 00 into my memory location and not the value of 05 – Sollo Jan 15 '15 at 00:23
  • Hi, I updated my code to be compatible with http://labs.kitiyo.com/8085/ Please insert it there, compile & simulate and press execute. You will see the correct output being written. The error must be somewhere else. Re-check that you're really writing to memory, not trying to write to ROM, etc. – FRob Jan 15 '15 at 02:28
  • Hi, this [answer](http://stackoverflow.com/questions/27948749/8085-assembly-sign-extend-8-bit-to-16-bit/27951677#27951677) solved my problem. – Sollo Jan 15 '15 at 08:48
0

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:
Sollo
  • 163
  • 11
Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • The `LDA` must be used like `LDA [memory location]` and change to `MOV A,M`. There is some syntax error and it works well after changed. Thank you very much! =D – Sollo Jan 15 '15 at 08:26