Domanda

I've just started learning MASM and I wrote a sample code that should run a simple loop.

   mov eax, 1
x: add eax,1
   print str$(eax),13,10
   cmp eax, 4
   jne x
   inkey
   exit

So, I expected this tiny program to print 2,3,4. What I'm getting, however, is an infinite loop somehow and keeps printing 3. Any clues why it's not working as I thought?

È stato utile?

Soluzione

eax is a volatile register, meaning its value is not required to be saved across function/macro calls. You need to save eax before the print macro and restore it afterwards:

    mov     eax, 0
x: 
    add     eax,1   
    push    eax
    print str$(eax),13,10
    pop     eax
    cmp     eax, 4
    jne     x

    inkey
    exit

Or just use a non-volatile register which value is required to be saved by the callee (esi, edi, ebx)

    mov     ebx, 1
x: 
    add     ebx,1   
    print str$(ebx),13,10
    cmp     ebx, 4
    jne     x

    inkey
    exit
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top