Author Topic: Code loops infinitely  (Read 14750 times)

Offline nohemon

  • Jr. Member
  • *
  • Posts: 8
Code loops infinitely
« on: September 16, 2020, 11:44:15 PM »
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: [Select]
    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

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Code loops infinitely
« Reply #1 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


Offline nohemon

  • Jr. Member
  • *
  • Posts: 8
Re: Code loops infinitely
« Reply #2 on: September 17, 2020, 10:48:50 AM »
Hi nohemon,

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

Best,
Frank

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

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: Code loops infinitely
« Reply #3 on: September 17, 2020, 01:34:52 PM »
I would change to the following

Code: [Select]
_main:

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

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.
« Last Edit: September 17, 2020, 01:45:02 PM by debs3759 »
My graphics card database: www.gpuzoo.com

Offline nohemon

  • Jr. Member
  • *
  • Posts: 8
Re: Code loops infinitely
« Reply #4 on: September 17, 2020, 04:02:27 PM »
I would change to the following

Code: [Select]
_main:

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

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.

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

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Code loops infinitely
« Reply #5 on: September 17, 2020, 04:41:41 PM »
So, the conditional jump instructions use the value that is in the offset RIGHT BEFORE them? even if there is no CMP instruction?

No... conditional jumps uses the FLAGS to decide if the jump is taken or not... DEC affects the FLAGS.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: Code loops infinitely
« Reply #6 on: September 17, 2020, 04:48:45 PM »
So, the conditional jump instructions use the value that is in the offset RIGHT BEFORE them? even if there is no CMP instruction?

The conditional jump uses whatever is the last condition generated (ie the flags, as fred said). In this case it uses the result of the preceding dec. Your code would work with the new push/pop, it's just slightly less optimal.
My graphics card database: www.gpuzoo.com

Offline nohemon

  • Jr. Member
  • *
  • Posts: 8
Re: Code loops infinitely
« Reply #7 on: September 17, 2020, 06:47:48 PM »
Ok now I get it. Thank you both.