Copy a String in Reverse Order Write a program with a loop a
Copy a String in Reverse Order
Write a program with a loop and indirect addressing that copies a string from source to target,
reversing the character order in the process. Use the following variables:
source BYTE \"This is the source string\",0
target BYTE SIZEOF source DUP(\'#\')
and here is what I have got so far
;reverse
INCLUDE IRVINE32.INC
.DATA
SOURCE byte \"this is the source string\", 0
target BYTE SIZEOF source DUP(\'#\')
.code
main PROC
Call Clrscr
mov esi,0
mov edi,SIZEOF source-TYPE source
mov ecx,LENGTHOF source
L1: mov al, source[edi]
mov target[esi]
sub edi TYPE source
loop L1
exit
main ENDP
END main
Solution
program with a loop and indirect addressing that copies a string from source to target
MOV SI, source ; Get source address
MOV DI, (target + SIZEOF source) ; Get the ending address for target
LOOP:
MOV AL, [SI] ; Get a byte
MOV [DI], AL ; Store a byte
INC SI ; Move forward one in source
DEC DI ; Move back one in target
CMP AL, 0 ; Have we reached the zero termination?
JNZ LOOP
RET
