Why is this resulting in infinite loop

14 Views Asked by At
format ELF64 executable

segment readable executable

SYSCALL_EXIT = 60
SYSCALL_WRITE = 1
STD_IN = 0
STD_OUT = 1

macro exit code {
    mov rax, SYSCALL_EXIT
    mov rdi, code
    syscall
}

macro write fd, buf, size {
    mov rax, SYSCALL_WRITE
    mov rdi, fd
    mov rsi, buf
    mov rdx, size
    syscall
}

entry main
main:
    mov rcx, 5; All loop instructions automatically decrement this register
my_loop:
    write STD_OUT, msg, msg_len
    loop my_loop
    exit 0

msg db "Hello", 10
msg_len = $ - msg

The above code goes into an infinite loop. my guess is that system call write is somehow modifying the rcx register, which is decremented by loop and expected to be 0 for the loop to stop. But I am not seeing anywhere documented this behavior of write. Why is rcx used as default loop counter if other things can modify this register.

Tried running this code, was expecting it to run 5 times. Instead it keeps printing Hello infinitely many times.

0

There are 0 best solutions below