section .data
lol: db "lol",10
global _start
section .text
_start:
mov rcx, 5
mainloop:
mov rax,1
mov rdi,1
mov rsi, lol
mov rdx, 4
syscall
loop mainloop
mov rax, 60
syscall
nasm linux x86_64
I wanted to output 5 times, but it ended up being an infinite loop.
GT LOL hoh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The syscall
instruction overwrites rcx
and r11
, see https://www.felixcloutier.com/x86/syscall.
So if you’re using rcx
as a loop counter, you’ll need to save it in another register, or on the stack, before the syscall
, and then restore it afterwards.
However, the loop
instruction is actually quite inefficient, and not really a good way to implement a loop: Why is the loop instruction slow? Couldn’t Intel have implemented it efficiently?. So it’s probably better to rewrite the code with a decrement and conditional jump, and then you’ll be free to use a different register for your loop counter.
For example, EBX, using mov ebx, 5
and dec ebx
/ jnz mainloop
. RBX is call-preserved so would work even if you made a function-call inside the loop. R9D is a good choice for a call-clobbered register since this system call doesn’t need all 6 operands. If you were writing a function which returned, the calling convention would require you to save/restore RBX if you used it. But you just exit via a system call so it doesn’t matter which registers are modified.