Write a program using the LOOP instruction with indirect addressing that copies
ID: 3681976 • Letter: W
Question
Write a program using the LOOP instruction with indirect addressing that copies a string from source to target, reversing the character order in the process. Use the folling variables: source BYTE "This is the source string", 0 target BYTE SIZEOF source DUP('#') Insert the following statements immediately after the loop to display the hexadecimal contents of the target string: mov esi,OFFSET target ;offset of variable mov ebx, 1 ; byte format mov ecx, SIZEOF target ; counter call DumpMem If your program works correctly, it will display the following sequence of hexadecimal bytes: 67 6E 69 72 74 73 20 65 63 72 75 6F 73 20 65 68 74 20 73 69 2073 69 68 54
Explanation / Answer
Indirect addressing uses a register as a pointer to hold the offset (address) of the data to be manipulated. This is ideal for array processing because each element of the list is simply an addition to the value stored in the register. You can think of this in the same way C++ uses a pointer to access element of an array.
The LOOP instruction repeats a block of statements a specific number of times. The ECX register is automatically used as a counter and is decremented each time the loop repeats.
The loop destination must be within -128 to + 127 bytes if the current location counter.
A nested loop is a loop inside a loop. When creating a nested loop you must make sure that you save off the value of the ECX register before moving to the nested loop and back
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP('#')
.code
reversecopy PROC
mov ESI,0 ; source register
mov EDI,SIZEOF source-2D ; destination register
mov ECX,SIZEOF source ; loop counter
Copy:
mov DL,source[ESI] ; get a character from source
mov target[EDI],DL ; store it in the target
inc ESI ; move to next character - increment the source index
dec EDI ; decrement the destination index
loop Copy ; repeat for entire string;
; Outputresults;
mov ESI,OFFSET target ;offset of variable
mov EBX,1 ;byte format
mov ECX,SIZEOF target ;counter
call DumpMem;
; Our results should be: 67 6E 69 72 74 73 20 65 63 72 75 6F 73 20 65 68
; 74 20 73 69 20 73 69 68 54;
;Return to operating system;
; exit
reversecopy ENDP
END reversecopy
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.