Shashank Shekhar
Shashank Shekhar

Reputation: 105

8086Assembly - Unable to Reverse String

JMP START
MSG1 DB "ISSK$"
MSG2 DB 80 DUP("$")
START:

MOV SI,00H
MOV DI,00H

LOOPER:          
CMP MSG1[DI],"$"
JE COMPARE
INC DI
JMP LOOPER  ;AFTER THIS POINT DI=STRINGLENGTH-1


COMPARE:
MOV AL,MSG1[DI]
MOV MSG2[SI],AL
INC SI
DEC DI
CMP MSG1[DI],00H
JE OUTER
JMP COMPARE

OUTER:
MOV DX,OFFSET MSG2
MOV AH,09H
INT 21H
HLT

The console of my EMULATOR(emu8086) finally prints a Blank Screen instead of the Reversed string. Where did I go wrong?

Upvotes: 0

Views: 52

Answers (1)

Michael
Michael

Reputation: 58487

When you exit LOOPER and go to COMPARE, DI contains the index of the '$' terminator character, which you then place at the start of MSG2. You should decrement DI before entering the COMPARE loop so that it contains the index of 'K'.

Then you've got CMP MSG1[DI],00H which doesn't make any sense. I suspect that you wanted to check if DI == 0. If so, this entire part:

DEC DI
CMP MSG1[DI],00H
JE OUTER
JMP COMPARE

could be replaced by:

DEC DI
JNZ COMPARE  ; continue the COMPARE loop as long as DI != 0

Upvotes: 3

Related Questions