views:

36

answers:

1

I am having trouble believing the following code is the most efficient way to move a value from the stack to ST(0):

.data
var dd 4.2
tmp dd ?

.code
mov EAX, var
push EAX
; top of stack now contains a value

; move it to ST(0)
pop EAX
mov tmp, EAX
fld tmp

Is the temporary variable really necessary? Further, is there an easier way to get a value from the stack to ST(0)?

Update: In the example above, I'm moving floating-point values around - not integers.

+2  A: 
fld dword ptr [esp]    ; assembles to D9 04 24
I. J. Kennedy
Uh... your code is calling `fild` which loads an integer value. I need `fld` which loads a floating point value.
George Edison
Ok, I edited and changed fild to fld.
I. J. Kennedy
@I. J. From reading the documentation for `fld`, I wasn't aware that that particular addressing mode was available for the instruction. Are you sure this is correct?
George Edison
Note that you still have to increase esp by the relevant size to get proper pop behavior.
Nathan Fellman
@Nathan: Increase ESP? Why?
George Edison
You can use any of the x86 memory addressing modes.
I. J. Kennedy
Like @Nathan said, if you want to duplicate the behavior of your sample code (which did a POP EAX), you'll need to add 4 to ESP.
I. J. Kennedy
Oh I see what you're saying - I left a value on the stack. Ya, the code was just an example.
George Edison