Author Topic: Does anyone know how to reverse an Array?  (Read 5458 times)

Offline captainamerica

  • Jr. Member
  • *
  • Posts: 5
Does anyone know how to reverse an Array?
« on: October 07, 2018, 10:26:40 PM »
Hello, I am trying to figure out how to reverse an array, all i know is that I can use pop and push to do that, but i have no idea how that works within a loop. Can anyone help me understand, I have been looking at a lot of different sites and testing it myself with my code but I cannot get it to work.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Does anyone know how to reverse an Array?
« Reply #1 on: October 07, 2018, 11:25:20 PM »
To answer your question: Yes.

To give you a hint: use two loops. ASSuming a byte array and a 32-bit stack... like your previous question...
Code: [Select]
; setup like before
pushloop:
mov al, [esi]
push eax ; three bytes "wasted"
inc esi
loop pushloop

mov ecx, ARRAY_SIZE
poploop:
pop eax
mov [edi], al
inc edi
loop poploop

; print 'em to prove it worked?
; ...

This is probably not the best way to reverse an array...

If that doesn't help you, show us what you've got.

Best,
Frank



Offline captainamerica

  • Jr. Member
  • *
  • Posts: 5
Re: Does anyone know how to reverse an Array?
« Reply #2 on: October 08, 2018, 12:04:00 AM »
Thanks for replying, i have been trying to get help for so long (even chegg tutors wont help me).

But i have tested your code with my data, for some reason it is doing what was happening to me– printing the last element of my array 6 times.
Also, just a quick question, for edi, do i initialize it just like
Code: [Select]
mov edi, Array3 just like esi?

Code: [Select]
        ;LOOP INITIALIZATION
;PUSH
mov ecx, Array3.len
mov esi, Array3
ReverseOrderPush:
mov al, [esi]                          ;al = Array3[i]
push eax ;push contents in stack
inc esi                                    ;point to next int
Loop ReverseOrderPush
;POP
mov ecx, Array3.len
ReverseOrderPop:
pop eax ;take the contents out
mov [edi], al                             ;move the contents from al to edi
inc edi                                           ;move to next byte

push DWORD [Array3] ;push array content
call Print32bitNumDecimal         ;prints the array
call PrintComma
    Loop ReverseOrderPop
call Printend
l

note: to print this array, I am using a library
« Last Edit: October 08, 2018, 12:10:25 AM by captainamerica »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Does anyone know how to reverse an Array?
« Reply #3 on: October 08, 2018, 03:43:31 AM »
Well, the code you show looks fine, so look closely at the code after that. If it's printing the last value, did you go back to the beginning of the array again? The library part should be okay. Is this "along32"?

Some libraries - C - trash ecx, which can cause problems with "loop". but that doesn't sound like the issue here. I think "irvine32" preserves all registers, so your library probably does too..

If all else fails, post the code...

Best,
Frank