I’m trying to print Hi 10 times. This is my code.
section .data msg db "Hi" section .text global _start _start: mov cx, 10 L1: mov eax, 4 mov ebx, 1 mov ecx, msg mov edx, 3 int 0x80 dec cx jnz L1 mov eax, 1 mov ebx, 0 int 0x80
gdb reports that mov edx, 3 overwrites the cx register to some crazy value and so the loop keeps going forever.
What am i doing wrong? Is it because they are the same register?
How does one program in assembly with so few registers?
Compiling on centos with nasm and ld
Thanks
Advertisement
Answer
You’re looking at the wrong line. The problem is “mov ecx, msg”. ECX is the extended register of which CX is the lower part, so you’re writing over it.
It’s best to save your loop counter on the stack, because who knows that the ‘int’ call might change. Add ‘push cx’ (or ecx) after ‘L1:’. and ‘pop cx’ after the ‘int’ call to preserve the contents of the register.