我有一个用AT&T汇编器为Linux中的x64处理器编写的小程序,并使用fpatan函数使用Gnu汇编器对其进行了编译。我知道我可以通过其他方式获得atan值。我不明白如何将在st0-st7寄存器中创建的浮点值移动到C ++程序中。
在此示例中,我可以使用GDB查看结果值。
我的代码如下:
# trigtest1.s - An example of using the FPACOS instructions
.section .data
y:
.float 1.625
x:
.float .25
.section .bss
.lcomm result, 4
.section .text
.globl _start
_start:
nop
finit
flds y
flds x
fpatan
fsts result
movl $1, %eax
movl $0, %ebx
int $0x80
我可以编译并将其链接为:
作为-gstabs trigtest1.s -o trigtest.o ld trigtest.o -o trigtest
在使用断点设置为movl $ 1,%eax的程序逐步执行程序后,使用GDB,我得到: (gdb)x / f和结果 0x6000e0:1.41814697
这就是我所期望的。当我尝试在C ++程序中使用它时,问题就来了。首先是功能。
# asm.s - An example of using the FPATAN function
.global ArcTan
.section .data
y:
.float 1.625
x:
.float .25
.section .bss
.lcomm result, 4
.section .text
ArcTan:
nop
finit
flds y
flds x
fpatan
fsts result
nop
ret
这是调用汇编器函数的C ++程序:
#include <iostream>
using namespace std;
extern "C" float ArcTan();
int main()
{
cout<<"Arctan is "<<ArcTan()<<endl;
cout<<endl;
return 0;
}
我使用以下命令进行编译和链接: as -gstabs asm.s -o asm.o g ++ -gstabs -O0 main.cpp asm.o -o runme
当我使用GDB查看正在发生的情况时,结果中的值永远不会更新并保持为0。我想在C ++程序中使用fpatan计算的值,但未成功返回该值。任何帮助将不胜感激。
x86-64 returns
float
in XMM0, not legacy x87st0
. Build your code withg++ -m32 asm.s main.cpp -o runme
to build 32-bit code that uses the 32-bit calling convention if you want to use legacy x87 for FP math.Note that your C++ code isn't reading
result
, it's reading the return value of the function which it expects in a register. If you did useextern float result;
in your C++ code, you could read it just fine.(turns out Michael Petch has already written a full answer on Return a float from a 64-bit assembly function that uses x87 FPU, closing this as a duplicate.)