Create an array of randomly ordered integers. Using the Swap procedure from the Demo Program 1 as a tool, write a loop that exchanges each consecutive pair of integers in the array.
DEMO Program 1:
INCLUDE Irvine32.inc
Swap PROTO, pValX:PTR DWORD, pValY:PTR DWORD
Swap2 PROTO, pValX:PTR DWORD, pValY:PTR DWORD
.data
Array DWORD 10000h,20000h
.code
main PROC
; Display the array before the exchange:
mov esi,OFFSET Array
mov ecx,2 ; count = 2
mov ebx,TYPE Array
call DumpMem ; dump the array values
INVOKE Swap, ADDR Array, ADDR [Array+4]
; Display the array after the exchange:
call DumpMem
exit
main ENDP
;-------------------------------------------------------
Swap PROC USES eax esi edi,
pValX:PTR DWORD, ; pointer to first integer
pValY:PTR DWORD ; pointer to second integer
;
; Exchange the values of two 32-bit integers
; Returns: nothing
;-------------------------------------------------------
mov esi,pValX ; get pointers
mov edi,pValY
mov eax,[esi] ; get first integer
xchg eax,[edi] ; exchange with second
mov [esi],eax ; replace first integer
ret ; PROC generates RET 8 here
Swap ENDP
;------------------------------------------------------
Swap2 PROC USES eax esi edi,
pValX:PTR DWORD, ; pointer to first integer
pValY:PTR DWORD ; pointer to second integer
LOCAL Temp:DWORD ;local doubleword variable named Temp
; Exchange the values of two 32-bit integers
; Returns: nothing
;-------------------------------------------------------
mov esi,pValX ; get pointers
mov edi,pValY
mov eax,[esi] ; get first integer
mov Temp,eax ; save a copy of the first integer
mov eax,[edi] ; exchange with second
mov [esi],eax
mov eax, Temp
mov [edi], eax
ret ; PROC generates RET 8 here
Swap2 ENDP
END main