I have tried with the algorithm below but it does not generate the results i am looking for and i am a bit stumped. I have tried swapping the elements in the array, unless you know there is a less trouble sum alternative example: 1,2,3,4,5 becomes 5,4,3,2,1. i have searched all over online and nothing seems to work efficiently. my professor does not want us to code our own but to find one online and understand what is happening to use it in a larger program with reversing the array being a smaller function.

(sorry the daniweb insert code function is not working)

mov esi, array

mov edi, array
add edi, 16

reverseloop:

nop
mov eax, edi
mov ebx, esi

;swap elements

mov dword [edi], eax
mov dword [esi], ebx
add esi, 4
sub edi, 4
cmp esi, edi
jb reverseloop
ret

Try the following:

        ; save ebx
        push ebx
        mov esi, array
        mov edi, array
        add edi, 16

    reverseloop:
        ; move content of pointers into a register (dereference pointers)
        mov eax, [edi]
        mov ebx, [esi]
        ;swap elements
        mov [edi], ebx
        mov [esi], eax

        add esi, 4
        sub edi, 4
        cmp esi, edi
        jb reverseloop
        ;restore ebx
        pop ebx
        ret

this logically makes sense but i receive a seg fault, and is the reversed array stored in the original place of the array or is it simply sitting in a register waiting to be printed to the screen?

Sorry, I don't have nasm installed to test, but if the code is inline then leave out the ret.
The code stores the reversed array in the original memory location.

Edited 1 Year Ago by nullptr

This article has been dead for over six months. Start a new discussion instead.