In the following code, I am wondering about the role of fld ST(0)
. I have heard it duplicates the top value in the stack but I don't know where it brings that value in the following code:
fild dword ptr [L1000F140]
fstp qword ptr [esp+20h]
fld ST(0)
fdiv qword ptr [L1000F148]
fmul qword ptr [L1000A520]
My take is that the above code in symbolic way means:
value[esp+20h]= value[L1000F140]
new stack top value=value[esp+20h]/value[L1000F148]*value[L1000A520]
I this correct?
I wonder why fstp
does not pop the loaded value so fld
gets duplicated value[L1000F140]
?
There's a good guide to x87 FPU by Raymond Filiatreault. Chapter 1 explains how the FPU register stack works.
Yes, fld st(0)
pushes a copy of the top of the stack. The Intel insn ref manual explicitly mentions this special-case use of fld st(n)
I believe you're right that fstp
does pop after the fild
.
The first fild / fstp
pair converts a global int
(at L1000F140) to a float on the x87 stack.
Then fld st(0)
duplicates the top of the x87 stack (i.e. the value that was there before the fild/fstp
).
Then divide that value by another global, and multiply by another.
final x87 stack:
st(0)=orig / global1 * global2
st(1)=orig