Search code examples
assemblymasmmasm32

Why this loops infinitely?


I'm simply trying to print the elements of the array. From the output i can see that the loop goes beyond my array's allocated memory.

.386 ; 386 Processor Instruction Set

.model flat,stdcall 

option casemap:none 
include \masm32\include\masm32rt.inc
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib  

.data

array DWORD 72,50,22,0
asd DWORD ?

start:

mov ecx, 4
mov edi, 0
//-- loop start--//
loop_start:

mov eax, [array + edi * 4] 

push offset asd
push eax
call dwtoa


Invoke StdOut, addr asd

inc edi  //incrementing edi
dec ecx  // decrementing ecx
cmp ecx,0 // comparing ecx against 0


jne loop_start // if not equal loop again
//--loop end--//


invoke ExitProcess, 0
end start 

Here is the output

EDIT: tried to add at the end

cmp ecx,0
je loop_end


loop_end:
Invoke ExitProcess,0

none of these worked.

Thanks in advance.


Solution

  • It seems that these two instructions change the ecx register:

    call dwtoa
    Invoke StdOut, addr asd
    

    My guess would be at dwtoa it might return the length of the asci array returned in the ecx register.

    Try this:

    loop_start:
    
    mov eax, [array + edi * 4] 
    
    push ecx // saving ecx before call
    
    push offset asd
    push eax
    call dwtoa
    
    
    Invoke StdOut, addr asd
    
    pop ecx // restore the ecx from before the calls.
    
    inc edi  //incrementing edi
    dec ecx  // decrementing ecx
    cmp ecx,0 // comparing ecx against 0
    
    
    jne loop_start // if not equal loop again