Search code examples
assemblyfloating-pointx86fpux87

Add 32-bit floats in eax/ecx registers?


I have a pair of 32-bit floats stored in eax and ecx. Can I directly load these into the FPU to operate on them, without first storing to memory? This would simplify some compiler code significantly, but fld seems to only be able to operate on memory.


Solution

  • No, you can't do that. As far as generating code goes, you can simulate fld r32 easily enough through the following sequence for example (optimized for size ;)) :

    push r32
    fld [esp]
    pop r32
    

    Consider using SSE if available, which does offer direct GPR-to-XMM moves using the movd instruction. Adding the two registers could then look something like:

    movd xmm0, eax
    movd xmm1, ecx
    addss xmm0, xmm1
    

    If you need the result in a GPR, you can move it back using another movd.