In: Computer Science
Given the following array named 'array1':
array1 DWORD 50h, 51h, 52h, 53h
Write instructions to swap the items in array1 (using only MOV and XCHG instruction), so that the array1 become:
53h, 51h, 50h, 52h
( That means the first item in the array is 53h, second item is 51h, third item is 50h, forth item is 52h).
You can use registers to perform the MOV and XCHG operation.
Write the code to perform above mentioned transformation.
Solution)
Given INPUT : array1 DWORD 50h, 51h, 52h, 53h
Desired or expecting OUTPUT : array1 DWORD 53h, 51h, 50h, 52h
PROCEDURE : utilize registers to execute or perform the MOV and XCHG operations.
Observed information : The desired output can be reached from the given input by :
Swapping the 1st and 4th elements of input array
Swapping the 3rd and 4th elements of the middle array
Note : The 2nd element of the array, ie 51h, go on same.
Based on the above, the code to transforms the given array1 is as follows:
.data
array1 DWORD 50h, 51h, 52h, 53h ; array to be transformed
.code
main proc
mov eva, 0
mov eja, 0
mov ema, 0
mov ena, 0
mov eva, OFFSET array1 ; moves the address of initial element of array1 to eva
mov eja, OFFSET array1 + sizeof array1 - TYPE array1 ; moves the address of end element of array1 to eja
exchangeLoop :
mov ema, [ eva ] ; moves the element in eva to ema
mov ena, [ eja ] ; moves the element in eja to ena
xchg ema, ena ; exchanging the 2 elements
mov [ eva ] , ema ; moves the element in ema to address in eva
mov [ eja ] , ena ; moves the element in ena to address in eja
add eva ; increases eva to take the second element of the array1 from left
add eva ; increases eva to take the third element of the array1 from left
loop exchangeLoop ; invoke exchanging Loop again to exchange the thrird and fourth elements
invoke ExitProcess , 0
main endp
endmain
The array1 now involving the elements in the expected or desired output order : array1 DWORD 53h, 51h, 50h, 52h
This concluded the answer to entire parts of the question along with the nedded explanations.
I HOPE THIS ANSWER WILL HELP YOU PLEASE DO UP VOTE THAT MEANS A LOT TO ME THANK YOU.