从内联ASM(X64)调用printf

我有这个代码:

#include <stdio.h> #include <stdint.h> int main(void){ char *fmt = "%s"; char *s = "Hello world!\n"; //gcc -m32 test.c #ifdef __i386__ int32_t ret; __asm__ __volatile__ ( "push %1\n\t" "push %2\n\t" "movl $2, %%eax\n\t" "call printf\n\t" "movl %0, %%eax" : "=r" (ret) : "r" (s), "r" (fmt) : ); #endif //gcc -m64 test.c #ifdef __x86_64__ int64_t ret; __asm__ __volatile__ ( "push %1\n\t" "push %2\n\t" "movq $2, %%rax\n\t" "call printf\n\t" "movq %0, %%rax" : "=r" (ret) : "r" (s), "r" (fmt) : ); #endif return ret; } 

x86版本按预期工作,但x64版本段错误。 为什么是segfault?

64位ABI使用寄存器(RDI,RSI,RDX,RCX,R8和R9),而不是用于参数传递的堆栈。 所以代码应该是:

 movl %2,%%rdi movl %1,%%rsi call printf movq %0,%%rax 

我认为这是相对于64位EABI。 你可以找到一些关于这个问题的信息 。