-1

Pretty simple question but why does this code result in an infinite loop?

It should iterate 10 times but its iterating forever for some reason

Assembly x86 64 bit btw

    _start: 
        mov ecx, 0
        
        loop:
            add ecx, 1
            
            mov eax, 1
            mov edi, 1
            mov esi, filename
            mov edx, 10
            syscall

            cmp ecx, 10
            jne loop
    ```
user259137
  • 57
  • 1
  • Use a debugger to see the value of `ecx` after `syscall`. – xiver77 Jun 01 '22 at 15:42
  • 1
    That's because RCX (and R11) are not preserved across system calls. See [What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64](https://stackoverflow.com/questions/2535989/what-are-the-calling-conventions-for-unix-linux-system-calls-and-user-space-f) – Marco Bonelli Jun 01 '22 at 15:52
  • 1
    If you don't know debugging, now is a good time to learn. It works similar to other languages: single step and observe the program state in between instructions. You should be able to see the problem in one iteration of the loop. – Erik Eidt Jun 01 '22 at 16:08
  • you are making a syscall, seems obvious – old_timer Jun 01 '22 at 16:51

0 Answers0