NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: nohemon 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:
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
-
Hi nohemon,
ecx is probably not preserved across _printf. try preserving it yourself (push/pop) or use bl instead.
Best,
Frank
-
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?
-
I would change to the following
_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.
-
I would change to the following
_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?
-
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.
-
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.
-
Ok now I get it. Thank you both.