Develop a program to swap the last element of an array with the first element, second element with the second-to-last element, and so on. Assume the array contains 20 8-bit elements.
I want to develop this program in assembly language, But If I have a pseudo code or C program, I think I can convert it to assembly language since I think the process is the same. So can anyone help me develop a code for this program?
thank you
Assembly or C programming?
Define array A[p] //p = 20
last_element = p
first_element = 0
while first_element %26lt;= p/2 //if p is odd, then the middle element
...................................... //will not need to be switched
{
temp = A[first_element]
A[first_element] = A[last_element]
A[last_element] = temp
first_element = first_element + 1
last_element = last_element - 1
}
Reply:If you have any knowledge of programming, most of this should be pretty mundane and something you've written before.
The only part that you possibly may not be familiar with is sucessfully swapping the elements without accidently overriding one or the other. You're going to need a third, temporary value.
The swap will occur as follows:
Z = A;
A = B;
B = Z;
Where A and B are elements to be swapped, and Z is the temporary storage variable.
It would probably be easiest to pass the element variables or pointers through a function that swaps them. Then, just make a loop that iterates through the array appropriately (i.e. one variable counts forward, the other counts back, and use them to pull the elements you need to send to the swap function).
primrose
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment