Question

Why does this print garbage instead of exiting my program gracefully? I use system calls this way on BSD, and I wonder what would I need to make it work in Linux.

int
main(int argc, char **argv)
{
    __asm ("movq $1,%rax; movq $0,%rdi; syscall"); /* exit(0) ? */
    return 0;
}

Thanks.

Was it helpful?

Solution

Why does this print garbage instead of exiting my program gracefully?

Per CESA-2009-001, "Syscall 1 is exit on i386 but write on x86_64".

what would I need to make it work in Linux

Use the syscall ordinals from the current unistd_64.h

Hope this helps!

OTHER TIPS

Syscall 1 is exit on i386 but write on x86-64 I believe.

EDIT: this seems inaccurate: According to the web, which does not seem to have too much information about x86-64 Linux assembly this seems to be the expected register setup before the syscall instruction.

 rax  system call number
 rbx  arg0
 rcx  return address from syscall
 rdx  arg2
 rsi  arg3
 rdi  arg4
 r8   arg5
 r9   arg1    (expected by gcc in %rcx)
 r10-r15  should be saved/restored by C code
 rbp  dito What is dito??
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top