Adding floating point / double numbers in assembly

I am trying to experiment with an inline assembly, and I am trying to add decimal numbers (no, NOT integers) in an inline assembly. The problem occurs 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 about the wrong operand size on the built-in assembly (ALL assembly lines, excluding push and pop lines). So I need to switch to an integer type like unsigned long, and then it works, but of course it only supports integer types; decimal results are rounded.

Is there a way to add to an assembly that allows decimal results, like 8.4?

+4
source share
5 answers

I did not build x87 in ten years, but it should be something like this:

 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 
+6
source

You might need the ADDSD command, but I don’t know for sure.

Here are links to manuals for Intel instructions. http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html/

They used to send your hardcopy copies for free, but it doesn't seem like that anymore.

+6
source

To manage floating point numbers, you need a different set of instructions. Here's an introduction that should help: x86 Build: floating point

+1
source

The answer above is that you have to push the operands onto the FP stack and get the result correctly.

However, the closest cause of "incorrect operand size" errors is that the "extended" e__ "registers (for example, eax) are 32-bit, and double-precision floating-point numbers are 64-bit.

0
source

Try the following:

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

All Articles