Search code examples
cassemblyfloating-point64-bitfasm

How to get double return value of atof() function (from msvcrt.dll) in assembly?


I want to convert string representing floating point number to double (64bit ieee754) and store the result of conversion in the register RAX from assembly (using FASM). First of all I included library to my main.asm:

atof: jmp [imp_atof]
section '.idata' import data readable writeable
library kernel32,'KERNEL32.DLL',\
  user32,'USER32.DLL', \
  msvcrt, "MSVCRT.DLL"
include "macro\import64.inc"
   import msvcrt, \
   imp_atof, 'atof'

Next step is to call atof() in code:

push _buffer ;this is string buffer holding float number
call atof

After that something strange happening and I cannot understand what exactly. For example we need to convert -2309.12E-15. Result must be: BD844FAD0D676443. High result of the covert operation is right. It is stored in r8 register:

r8 = 00000000BD844FAD

But no one register store Low result. After debugging code calls I found that somewhere low result written to EAX register (000000000D676443) and then disappear. How can I resolve this problem?


Solution

  • On Windows x64 the return value in this case is in XMM0. From the calling convention documentation on MSDN:

    Non-scalar types including floats, doubles, and vector types such as __m128, __m128i, and __m128d are returned in XMM0.