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 operators to 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.

My current code:

.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

Respuesta :

Answer:

; Use a loop with indirect or indexed addressing to  

; reverse the elements of an integer array in place

INCLUDE Irvine32.inc  

.data

      ;declare and initialize an array  

      array1 DWORD 10d,20d,30d,40d,50d,60d,70d,80d,90d  

.code  

main PROC  

      ;assign esi value as 0

      mov esi,0

      ;find the size of array

      mov edi, (SIZEOF array1-TYPE array1)

      ;find the length of the array

      ;divide the length by 2 and assign to ecx  

      mov ecx, LENGTHOF array1/2  

;iterate a loop to reverse the array elements  

L1:

      ;move the value of array at esi

      mov eax, array1[esi]  

      ;exchange the values eax and value of array at edi  

      xchg eax, array1[edi]  

      ;move the eax value into the array at esi  

      mov array1[esi], eax  

      ;increment the value of esi  

      add esi, TYPE array1

      ;decrement the value of edi

      sub edi, TYPE array1  

      loop L1  

;The below code is used to print  

;the values of array after reversed.  

      ;get the length of the array  

      mov ecx, LENGTHOF array1  

      ;get the address  

      mov esi, OFFSET array1  

L2:  

      mov eax, [esi]

      ;print the value use either WriteDec or DumpMems

      ;call WriteDec  

      ;call crlf  

      call DumpMem

      ;increment the esi value

      add esi, TYPE array1  

      LOOP L2  

exit

main ENDP  

END main