In: Computer Science
1. 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.
Explanation :
MOV :
MOV (Move) transfers a byte, word, or double-word from the source
operand to the destination operand. The MOV instruction is useful
for transferring data along any of these paths. There are also
variants of MOV as follows :
To a register from memory
To memory from a register
Between general registers
Immediate data to a register
Immediate data to a memory
XCHG :
The XCHG (exchange data) instruction exchanges the contents of two
operands.
There are three variants:
XCHG reg, reg
XCHG reg, mem
XCHG mem, reg
You can exchange data between registers or between registers and
memory, but not from memory to memory:
xchg ax, bx ; Put AX in BX and BX in AX
xchg memory, ax ; Put "memory" in AX and AX in "memory"
xchg mem1, mem2 ; Illegal , can't exchange memory locations!
The rules for operands in the XCHG instruction are the same as
those for the MOV instruction except that XCHG does not accept
immediate operands.
In the light of the above discussions, the solution to the given
set of questions are as follows:
The Solution :
Given INPUT : array1 DWORD 50h, 51h, 52h, 53h
Desired OUTPUT : array1 DWORD 53h, 51h, 50h, 52h
PROCESS : use registers to perform MOV and XCHG operations.
Observation : The desired output can be reached from the given
input by :
Swap the 1st and 4th elements of input array
Swap the 3rd and 4th elements of the intermediate array
Note : The 2nd element of the array, ie 51h, remains the
same.
Based on the above, the code to transform the given array1 is as
follows:
.data
array1 DWORD 50h, 51h, 52h, 53h ; array to be transformed
.code
main proc
mov esi, 0
mov edi, 0
mov eax, 0
mov ebx, 0
mov esi, OFFSET array1 ; move address of first element of array1 to
esi
mov edi, OFFSET array1 + sizeof array1 - TYPE array1 ; move address
of last element of array1 to edi
exchangeLoop :
mov eax, [ esi ] ; move the element in esi to eax
mov ebx, [ edi ] ; move the element in edi to ebx
xchg eax, ebx ; exchange the two elements
mov [ esi ] , eax ; move the element in eax to address in esi
mov [ edi ] , ebx ; move the element in ebx to address in edi
add esi ; increase esi to take the second element of the array1
from left
add esi ; increase esi to take the third element of the array1 from
left
loop exchangeLoop ; invoke exchangeLoop again to exchange the
thrird and fourth elements
invoke ExitProcess , 0
main endp
endmain
The array1 now contains the elements in the desired output order :
array1 DWORD 53h, 51h, 50h, 52h
This concludes the answer to all parts of the question along with
the necessary explanations.
Please do not forget to like the answer if it helps you. Thank
you.