Question

In: Computer Science

1. Given the following array named 'array1': array1 DWORD 50h, 51h, 52h, 53h Write instructions to...

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.

Solutions

Expert Solution

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.


Related Solutions

Given the following array named 'array1': array1 DWORD 50h, 51h, 52h, 53h Write instructions to swap...
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.
Given an array labeled as array1 with the values: 11111h, 22222h, 33333h, 44444h And another array...
Given an array labeled as array1 with the values: 11111h, 22222h, 33333h, 44444h And another array labeled as array2 with the values: 0AEFFh, 23CAH, 1156H Sum up BOTH arrays through direct addressing and place the sum into the EAX register. array1 is a DWORD and array2 is a WORD
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
Given an array, write code to scan the array for a particular purpose. Given a class,...
Given an array, write code to scan the array for a particular purpose. Given a class, write a getter and/or a setter. Given a class with a list, write code that manages the list.
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function named timesOfLetter that reads an array and returns the number of times of...
Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase letter and each uppercase letter appear in it, using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it, using reference parameter. • Write a function named isChar() that determines if the input is alphabetic or not during inputting. • Write a function...
Write a sequence of assembly language instructions to subtract each entry of an array A of...
Write a sequence of assembly language instructions to subtract each entry of an array A of five two’s complement 16-bit binary integers from the corresponding entry of an array B of five two’s complement 16-bit binary integers and construct a third array C of two’s complement 16-bit binary integers. i.e. C[i] = A[i] - B[i]. Use the following data for the arrays A and B. A: 10, -15, 20, 4, -5 B: 25, -5, -30, 6, 10 please answer in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT