简体   繁体   中英

How to load the value stored in extended register(eax) into st0 of floating point stack?

How do I load the value stored in extended register(eax) into st0 of floating point stack?

fld dword [eax]

When I use the above instruction, I do not get the desired change in transfer maybe because the bracket signs have something to do with memory address and not content. Though, I am not sure.

Your attempt to load EAX using this instruction won't do what you think it does:

fld dword [eax]

This loads the 32-bit floating point value from the memory location pointed to by EAX (this is indirect addressing ). I believe you want to load ST(0) with the value of EAX instead.


If you do have to use x87 FPU instruction rather than something like SSE then I'd recommend storing the value in EAX temporarily on the stack rather than at a fixed memory location like @quasar66 suggested. Putting it on the stack also allows your code to become re-entrant .

If the code is in a function that already allocates space for temporaries, just increase the stack allocation to allow another 32-bit value to be stored to it and move EAX to that stack location and then use fld to load from there.

Ultimately the 32-bit code could have been written to have a similar effect as:

push EAX           ; Store EAX to top of stack
fld dword [esp]    ; Load 32-bit DWORD value at top of stack to ST(0)    
POP EAX            ; Restore stack, alternatively could have used add esp, 4         
; do other floating point work here   

On 64-Bit OSes that follow the System V 64-bit ABI (doesn't apply to 64-bit Windows) you can move data directly to the stack's red zone without fear of it being clobbered. Such 64-bit code could have looked like this:

mov [rsp-4], eax   ; Temporarily place EAX in the stack's red zone beneath current RSP
fld dword[rsp-4]   ; Load it into ST0
; do other stuff here
                   ; No need for stack cleanup

Do not attempt this in 32-bit code as it is possible for an an asynchronous event (signal etc) to clobber the value on the stack after the mov and before the fld .


What isn't clear in your question is whether the value in EAX is an integer or it happens to be a 32-bit floating point value already. If it were an integer you'd have to replace FLD with FILD in my answers to convert the integer value to a floating point value when loaded onto the FPU stack.

The FLD instruction can only read from memory, so you need to save the value of EAX into a memory location and then do a memory upload.

a_memory_place    dd     ?
.
mov dword ptr [a_memory_place],eax
fld dword ptr [a_memory_place]      ; loads a 32 bit data into ST(0)

Trust this helps

  fld     dword ptr [eax]

This code works good! But eax must point to valid 32 bit real! If you are using 64 bit real then use:

  fld     qword ptr [eax]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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