Reputation: 1159
extern putchar
extern exit
section .data
section .text
global main
main:
push 's'
mov eax, 2
cmp eax, 2
point:
call putchar
jz point
push 0
call exit
On the console I see only one 's' charcter.
Compile and run:
nasm -f elf ./prog.asm
gcc -m32 -o prog ./prog.o
./prog
Upvotes: 0
Views: 105
Reputation: 25288
The cmp
is "equal to 0" (that is, it does set the ZF flag). However, the call putchar
in the next line is trashing the flags set by cmp
, so your jz
does not work (more or less by accident). If you want to save the flags for later comparison, you could use pushf
and popf
, however this won't really work in your case since putchar
will expect the character on the stack, not the flags.
Now, to answer the actual problem which you didn't state. I'll assume you want to print 's' two times. Here's how to do it properly:
mov eax, 2 ; init counter
print_loop:
push eax; save the counter since it will be trashed by putchar
push 's'
call putchar
add esp, 4 ; restore the stack pointer since putchar is cdecl
pop eax ; restore the saved counter
dec eax ; decrement it
jnz print_loop ; if it's not yet zero, do another loop
add esp, 4
can be replaced by another pop eax
for slightly shorter code.
Upvotes: 5
Reputation: 28839
The result of doing a cmp
is that flags get set, zf
for zero, and so on. You can then either branch on whether or not the flag was set or use one of the set?
instructions to have a value, e.g. the al
register, set based on whether or not the flag was set.
Upvotes: 2