【发布时间】:2019-01-30 07:10:37
【问题描述】:
我正在尝试从汇编程序调用 C 函数,但我没有得到按预期传递的值(参数)。
fastcall 属性从 gcc 生成警告并被忽略!
我正在使用 MASM 样式编码并使用 jwasm(或 uasm)进行编译。
jwasm -q -10 -elf64 -mf -zf0 asmfunc.asm -Fo asmfunc.o
main.c
#include <stdio.h>
__attribute__((fastcall)) void DumpRCX(unsigned long long rcx)
{
printf("%llx\n", rcx);
}
void asmfunc(void);
main(arc, *argv[])
{
asmfunc();
}
asmfunc.asm
EXTERN DumpRCX: PROC
PUBLIC asmfunc
asmfunc PROC
sub rsp, 28h
mov rcx, 84h ; any value for testing
call DumpRCX
add rsp, 28h
ret
asmfunc ENDP
运行并进入DumpRCX函数,但RCX中的值从来不是84h。
我的理解是fastcall应该在RCX寄存器中传递函数调用的第一个参数。
我在 mingw gcc + jwasm 和 VS 2015 下对此进行了测试,它工作正常。
我不确定我在 Linux 上缺少什么!
【问题讨论】:
-
在 Linux 上,64 位代码使用x86-64 System V ABI。前 6 个参数始终通过 rdi、rsi、rdx、rcx、r8、r9(按此顺序)传递。其余的按从右到左的顺序压入堆栈。如果要转储 RCX,则必须在调用 DumpRCX 之前将其移动到 RDI。
-
Linux 上也没有影子空间,因此您也不需要从 RSP 中减去 28h。在调用 DumpRCX 之前,您需要减去 8(或像
push rbp一样进行推送)以确保 16 字节堆栈对齐。
标签: gcc assembly x86-64 calling-convention