在汇编中添加浮点/双精度数

我正在尝试内联汇编,我试图在内联汇编中添加十进制数字(不,不是整数)。 问题是,当我调用以下函数时:

inline double ADD(double num1, double num2) { double res; _asm{ push eax; push the former state of eax onto stack mov eax, num1; add eax, num2; mov res, eax; pop eax; restore the former state of eax now that we are done } return res;} 

编译器抱怨内联汇编中的操作数大小不合适(除了push和pop指令行之外的所有汇编行)。 所以我必须更改为整数类型,例如unsigned long,然后它可以工作,但当然只支持整数类型; 十进制结果四舍五入。

有没有办法添加允许像8.4这样的十进制结果的程序集?

我在十年内没有做过x87汇编,但它应该是这样的:

 fld num1 ; load num1 and push it onto the fpu stack fld num2 ; load num2 and push it onto the fpu stack faddp ; pop two numbers, add them, push sum on the stack fstp res ; pop sum from the stack and store it in res 

你可能想要的指令是ADDSD,但我不确定。

这是英特尔指令集手册的链接。 http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html/

他们过去常常免费邮寄你的硬拷贝,但看起来已经不再适用了。

您需要一组不同的指令来操作浮点数。 这是一个应该有所帮助的介绍: x86汇编:浮点

上面的答案,您必须将操作数推送到FP堆栈并弹出结果是正确的。

但是,“不正确的操作数大小”错误的直接原因是“扩展”寄存器,“e __”(例如eax)是32位,双精度浮点数是64位。

试试这个:

 _asm{ movq xmm0,[num1] addpd xmm0, [num2]; movq [res],xmm0 // sse2 }