代码之家  ›  专栏  ›  技术社区  ›  jbcreix

x86 Linux中的内联ASM系统调用?

  •  12
  • jbcreix  · 技术社区  · 15 年前

    为什么这个打印垃圾而不是优雅地退出我的程序?我在BSD上以这种方式使用系统调用,我想知道在Linux中使用它需要什么。

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

    谢谢。

    2 回复  |  直到 15 年前
        1
  •  13
  •   Marsh Ray    15 年前

    为什么这个打印垃圾而不是优雅地退出我的程序?

    CESA-2009-001 “syscall 1在i386上退出,但在x86_64上写入。”

    我需要什么才能使它在Linux中工作?

    使用当前 unistd_64.h

    希望这有帮助!

        2
  •  3
  •   Sean A.O. Harney    15 年前

    syscall 1在i386上退出,但我相信在x86-64上写入。

    编辑:这似乎不准确: 根据Web,它似乎没有太多关于x86-64Linux程序集的信息,这似乎是在syscall指令之前预期的寄存器设置。

     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??