从 C (double)、GAS 调用 ASM x64 函数

Calling ASM x64 function from C (double), GAS

我在 ASM 和 C 中得到了这个非常简单的函数。我想从 C 代码调用 ASM 函数以获得双精度值。我认为来自 ASM 的 return 值应该存储在 XMM0 中,但实际发生的是我的 return 值是从 rax 中获取的,或者如果没有设置 rax,我得到 1。

C代码:

#include <stdio.h>

int main() {
double a = 3.14;
double b = add(a);
printf("%lf\n", b);

return 0;
}

A​​SM 函数:

.type add, @function

.globl add
add:

#movq , %rax
addsd %XMM0, %XMM0

ret

有什么问题吗?感谢所有提示。

您还没有告诉编译器函数接受什么或 returns。隐式声明将使其假定为 int 的 return 值。

编译器应该就此警告您。如果没有,请调高警告。

你应该添加

extern double add(double val);

所以编译器知道发生了什么。