Question

In: Computer Science

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.

Solutions

Expert Solution

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.


Related Solutions

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.
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
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
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
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first node should not be able to change places. The nodes are given by: Struct nodeEl { int el; struct nodeEl * next; }; typdef struct nodeEl node; The list header (of type node *) is the first parameter of the subroutine. The second and third parameters consist of integers and are the places in the list where the nodes are to change places. The...
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
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++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT