简体   繁体   English

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

[英]Adding floating point/double numbers in assembly

I am trying to experiment with inline assembly, and I am trying to add decimal numbers (no, NOT integers) in inline assembly. 我试图尝试内联汇编,我试图在内联汇编中添加十进制数字(不,不是整数)。 Issue is, when I call the following function: 问题是,当我调用以下函数时:

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;}

The compiler complains of improper operand size at the inline assembly (ALL lines of assembly excluding the push and pop instruction lines). 编译器抱怨内联汇编中的操作数大小不合适(除了push和pop指令行之外的所有汇编行)。 So I have to change to an integer type, such as unsigned long, and then it works, but of course only supports integer types; 所以我必须更改为整数类型,例如unsigned long,然后它可以工作,但当然只支持整数类型; decimal results are rounded. 小数结果四舍五入。

Is there any way to add in assembly that allows for decimal results like 8.4? 有没有办法添加允许像8.4这样的十进制结果的汇编?

I haven't done x87 assembly in a decade, but it should be something like: 我在十年内没有完成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

The instruction you probably want is ADDSD, but I don't know for sure. 你可能想要的指令是ADDSD,但我不确定。

Here's the link to Intel's instruction set manuals. 这是英特尔指令集手册的链接。 http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html/ http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html/

They used to mail you hard copies for free, but it looks like that's no longer true. 他们过去常常免费邮寄你的硬拷贝,但看起来已经不再适用了。

You need a different set of instructions to manipulate floating point numbers. 您需要一组不同的指令来操作浮点数。 Here's an introduction that should help: x86 Assembly: Floating Point 这是一个应该有用的介绍: x86汇编:浮点

The answer, above, that you have to push the operands onto the FP stack and pop the result is correct. 上面的答案,您必须将操作数推入FP堆栈并弹出结果是正确的。

However, the proximate cause of the "improper operand size" errors is that "extended" registers, "e__" (eg eax) are 32-bit and double-precision floating-point numbers are 64-bit. 但是,“不正确的操作数大小”错误的直接原因是“扩展”寄存器,“e __”(例如eax)是32位,双精度浮点数是64位。

Try this: 尝试这个:

_asm{

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM