A good tutorial for the x87 FPU is beyond the scope of Stackoverflow but I can recommend the one on MASM forums. Another good source is the Intel Instruction Set Reference. In particular most functions starting with F
are the x87 floating point unit (FPU) related instructions.
In general you can't just add a floating point value to an integer. They are two different representations. What you can do is convert the integer to a floating point value and then do floating point computations with it. In particular the instructions that start with FI
are floating point operations that involve a conversion of an integer memory operand to floating point.
There are many ways to skin a cat, but if you review the FPU tutorial linked to above you may realize that one simplistic way is to do this:
sub esp, 8 ; Allocate space on stack to store integer
mov dword [esp], 2 ; Move the 32-bit integer value onto stack temporarily
fild dword [esp] ; Load integer 2 from stack into top of FPU stack at st(0)
; Converting it to 2.0 in the process
mov dword [esp], __float32__(3.25)
; Move 3.25 onto stack temporarily
fadd dword [esp] ; Add 3.25 to st(0). Result in st(0). So st(0)=5.25
fstp qword [esp] ; Store 64-bit double to stack for printf and pop FPU stack.
Rather than using global variables to store values in main memory temporarily I used the stack space we reserve as a temporary staging area to load/operate with the x87 FPU.
If you are on a CPU that supports the SSE2 instruction set (This includes any X86-64 processor in 32-bit mode) then you have other options. One is to use the SIMD instructions and registers to do 32-bit and 64-bit floating point operations. Using the instruction set reference you find some useful instructions like:
- cvtsi2sd : Convert Dword Integer to Scalar Double-Precision FP Value
- cvtss2sd : Convert Scalar Single-Precision FP Value to Scalar Double-Precision FP Value
- addsd : Add Scalar Double-Precision Floating-Point Values
- movsd : Move Scalar Double-Precision Floating-Point Value
A Scalar Single-Precision FP value is a 32-bit float. A Scalar Double-Precision is a 64-bit double.
sub esp, 8
mov dword [esp], 2 ; Load integer 2 (32-bit signed value) onto stack temporarily
cvtsi2sd xmm0, [esp] ; Convert 2 on stack to 64-bit float and store in XMM0
mov dword [esp], __float32__(3.25)
; Load 32-bit float value of 3.25 onto stack
cvtss2sd xmm1, [esp] ; Load 32-bit single and convert it to 64-bit double. Store in XMM1
addsd xmm0, xmm1 ; Add 64-bit float in XMM0 and XMM1 store XMM0
movsd qword [esp], xmm0 ; Move 64-bit float back onto stack to be printed by printf