我有这段代码:
#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版本为segfaults。为什么会出现分段错误?
答案 0 :(得分:1)
我认为这与64位EABI相关。您可以在SO question找到一些信息。
答案 1 :(得分:1)
64位ABI使用寄存器(RDI,RSI,RDX,RCX,R8和R9)代替堆栈进行参数传递。所以代码应该是:
movl %2,%%rdi
movl %1,%%rsi
call printf
movq %0,%%rax