Reverse an Array Use a loop with indirect or indexed addressing to reverse the e
ID: 3760287 • Letter: R
Question
Reverse an Array
Use a loop with indirect or indexed addressing to reverse the elements of an integer array in place. Do not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF operatorsto make the program as flexible as possible if the array size and type should be changed in the future. Optionally, you may display the modified array by calling the DumpMem method from the Irvine32 library.
How do i implement this code into visual studio to give me an output.
INCLUDE Irvine32.inc
.data
array1 DWORD 10d,20d,30d,40d,50d,60d,70d,80d,90d
.code
main PROC
mov ESI, OFFSET array1 ;ESI now points to the first item of array1
mov EDI, SIZEOF array1
add EDI, OFFSET array1
sub EDI, TYPE array1 ;EDI now points to the last item of array1
mov ECX, LENGTHOF array1
shr ECX, 1 ;now ecx is half the length of the array1
L1: mov EAX, [ESI] ;in this loop we reverse the items of the array
mov EBX, [EDI]
mov [EDI],EAX
mov [ESI],EBX
add ESI, TYPE array1
sub EDI, TYPE array1
LOOP L1
mov ECX, LENGTHOF array1;here we just print the array
mov ESI, OFFSET array1
L2: MOV EAX, [ESI]
call WriteInt
call Crlf
add ESI, TYPE array1
LOOP L2
exit
main ENDP
END main
Explanation / Answer
.data
array BYTE 10h, 20h, 30h, 40h
.code
main PROC
mov esi, 0
mov edi, 0
mov esi, OFFSET array + SIZEOF array - 1
mov edi, OFFSET array + SIZEOF array - 1
mov ecx, SIZEOF array/2
l1: mov al, [esi]
mov bl, [edi]
mov [edi], al
mov [esi], bl
inc esi
dec edi
LOOP l1
call DumpRegs
call DumpMem
exit
main ENDP
END main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.