NASM Forum > Programming with NASM

Code loops infinitely

(1/2) > >>

nohemon:
I'm trying to loop through a piece of code for 5 times but the code loops forever.

It seems like inc cl doesn't occur.

This is my code:


--- Code: ---    global _main
    extern  _printf

    section .text
_main:

        mov     cl, 0
       
   loupa:
       
        push    message
        call    _printf
        add     esp, 4
        inc      cl
        cmp     cl, 5
        jne     loupa
       
        ; exit
        mov eax,0
        ret

    section .data
    message db 'Hello, World', 10, 0
--- End code ---

Frank Kotler:
Hi nohemon,

ecx is probably not  preserved across _printf. try preserving it yourself (push/pop) or use bl instead.

Best,
Frank

nohemon:

--- Quote from: Frank Kotler on September 17, 2020, 12:17:44 AM ---Hi nohemon,

ecx is probably not  preserved across _printf. try preserving it yourself (push/pop) or use bl instead.

Best,
Frank

--- End quote ---

It works with bl, thanks! But for learning purposes, could you show me how it's done with push/pop?

debs3759:
I would change to the following


--- Code: ---_main:

        mov     cl, 5
       
   loupa:
       push ecx
        push    message
        call    _printf
        add     esp, 4
        pop ecx
        dec      cl
        jnz     loupa

--- End code ---

Note that "mov cl,5" and "dec cl" reduces the total operations by 1 compared to "mov cl,0" with a inc, cmp, as you don't need the cmp line.

nohemon:

--- Quote from: debs3759 on September 17, 2020, 01:34:52 PM ---I would change to the following


--- Code: ---_main:

        mov     cl, 5
       
   loupa:
       push ecx
        push    message
        call    _printf
        add     esp, 4
        pop ecx
        dec      cl
        jnz     loupa

--- End code ---

Note that "mov cl,5" and "dec cl" reduces the total operations by 1 compared to "mov cl,0" with a inc, cmp, as you don't need the cmp line.

--- End quote ---

So, the conditional jump instructions use the value that is in the offset RIGHT BEFORE them? even if there is no CMP instruction?

Navigation

[0] Message Index

[#] Next page

Go to full version