Reverse the elements in a given array

In summary, the conversation is about a person seeking help with writing a code in assembly to reverse the elements of an array. The other person points out that the code is missing a loop statement and suggests using a debugger to check the correctness of the code.
  • #1
Fatima Hasan
319
14

Homework Statement


Write a sequence of statements to REVERSE the elements of array X1.

Homework Equations


-

The Attempt at a Solution


Here's my work :
reverse.png

Could somebody check my answer please ?
 

Attachments

  • reverse.png
    reverse.png
    6.7 KB · Views: 659
Physics news on Phys.org
  • #2
Is this assembly? Are you able to run the code?
 
  • #3
scottdave said:
Is this assembly?
yes
 
  • #4
Fatima Hasan said:
Could somebody check my answer please ?
Your code doesn't work, but you're not far off.
It looks like you are setting up a loop to run 5 times, and you have a label at the start of your loop, but you are missing a loop statement at the bottom of the loop body, and whose target is the label. After you fix that, I believe your code will do what it's supposed to do.
When you get your code to run, you really should use your debugger to see if the program is working correctly. Set a breakpoint on the first line after the loop statement, and inspect the contents of the array. After the loop finishes the array contents should be 1ah, 90h, 80h, ..., 10h.
 
Last edited:
  • Like
Likes berkeman and scottdave

1. How can I reverse the elements in a given array using a loop?

The most common way to reverse an array using a loop is to iterate through the array from the end to the beginning and store each element in a new array in reverse order.

2. Is there a built-in function to reverse an array in most programming languages?

Yes, most programming languages have a built-in function that can be used to reverse an array. For example, in JavaScript, you can use the reverse() method. In Python, you can use the reverse() method or the reversed() function.

3. Can I reverse an array without using extra memory or creating a new array?

Yes, you can reverse an array in-place without using extra memory by swapping the elements from the beginning and end of the array until you reach the middle.

4. How do I handle arrays with an odd number of elements when reversing?

When reversing an array with an odd number of elements, the middle element will remain unchanged. You can simply ignore this element and reverse the remaining elements in the array.

5. What is the time complexity of reversing an array?

The time complexity of reversing an array is O(n), where n is the number of elements in the array. This is because in the worst case, you will need to iterate through the entire array to swap all the elements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
873
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top