Search code examples
arraysassemblyx86intel

Why is my array not changing in x86 Assembly MASM


I'm getting into Assembly and having issues with arrays. Coming from C++ background, array manipulation is relatively easy as you can do lots of magic with pointers, referencing etc. however this is not the case with Assembly. I'm doing array wrapping and wrote this program, however when I assemble this code and run it, the array does not change (I check it from Watch list). What am I doing wrong?

.386
.model flat,stdcall
.stack 4096

ExitProcess PROTO,dwExitCode:dword

.data
arrayA DWORD 1001d,1002d,1003d,1004d,1005d
len DWORD 5

.code
main proc
    
    mov ebx, OFFSET arrayA
    mov edx,len
    cmp edx,1
    je lengthOne
    
    cmp edx,2
    je lengthTwo

    cmp edx,3
    jge lengthGreater

lengthOne:
    invoke ExitProcess,0

lengthTwo:
    mov eax, [ebx + 4]
    mov dx, [ebx]
    mov [ebx + 4], dx
    mov [ebx], eax
    invoke ExitProcess,0

lengthGreater:
    mov edx, [ebx + 16]                     
    push edx
    mov ecx, 16                                     
start:
    mov eax, [ebx + ecx]
    cmp eax, [ebx]
    je firstIndex

    mov edx, [ebx + ecx - 4]
    mov [arrayA + ecx], edx
    sub ecx, 3
    loop start
    invoke ExitProcess,0

firstIndex:
    pop edx
    mov [arrayA + ecx],edx
    invoke ExitProcess,0

main endp
end main

Solution

  • I debugged your code in visual studio. Your array is actually changing and it is changing nicely. There is only one problem with your code:

    start:
        mov eax, [ebx + ecx]
        cmp eax, [ebx]
        je firstIndex
    
        mov edx, [ebx + ecx - 4]
        mov [arrayA + ecx], edx
        sub ecx, 3
        loop start
        invoke ExitProcess,0
    
    firstIndex:
        pop edx
        mov [arrayA + ecx],edx
        invoke ExitProcess,0
    

    If you look at this part you can see that after loop you are leaving the program. The problem with that is that you are not doing the firstIndex check after modifying the array. To fix it you can just remove the invoke ExitProcess,0 after then loop.

    start:
        mov eax, [ebx + ecx]
        cmp eax, [ebx]
        je firstIndex
    
        mov edx, [ebx + ecx - 4]
        mov [arrayA + ecx], edx
        sub ecx, 3
        loop start
    
    firstIndex:
        pop edx
        mov [arrayA + ecx],edx
        invoke ExitProcess,0
    

    So that once your loop finishes you automatically do the firstIndex

    If you run this code you get the following array structure:

    enter image description here

    Edit:

    You can maybe also do something like, but I haven't tested this one so I do not know if it works as this point:

    start:
    
        mov edx, [ebx + ecx - 4]
        mov [arrayA + ecx], edx
        sub ecx, 3
        
        mov eax, [ebx + ecx]
        cmp eax, [ebx]
        je firstIndex
    
        loop start
        invoke ExitProcess,0
    
    firstIndex:
        pop edx
        mov [arrayA + ecx],edx
        invoke ExitProcess,0