从调用内联ASM的printf(X64)(Calling printf from inline AS

2019-11-02 17:01发布

我有这样的代码:

#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版本段错误。 为什么会出现段错误-ING?

Answer 1:

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

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


Answer 2:

我想,这是相对于64位EABI。 你可以找到一些信息SO问题 。



文章来源: Calling printf from inline ASM (X64)
标签: c linux