I have asked a similar question a few days ago, was very imprecise back then though, so here is a much more detailed post. So I am currently writing x86 32 Bit assembly using NASM and I want to use my custom function in a c++ program. I have issues working with the returned value though.
Here is my sample code in assembly:
section .data
value: dq 1.0
section .text
global _arsinh
_arsinh:
fld dword[esi-8] ;loads the given value into st0
ret
Here is my sample/test code in c++:
#include <iostream>
extern "C" float arsinh(float);
int main()
{
float test = arsinh(5.0);
printf("%f\n", test); //Displays -96715160...
printf("%f\n", arsinh(5.0)); //Displays -96715160...
std::cout << test << std::endl; //Displays -9.671512e+24
std::cout << arsinh(5.0) << std::endl; //Displays 5
}
All the assembly code does is load given value from the stack(5.0 in this case) and store it in st0. The problem I am now encountering is, that all but the last of the print statements display a wrong value (not 5.0). I assume the issue is that I am loading a dword and not a qword, as the following code works(displays the correct values) completly fine:
section .data
value: dq 1.0
section .text
global _arsinh
_arsinh:
fld qword[value] ;loads value into st0
ret
I would like to save the returned value into a float and then do some additional calculations in c++/c with it, but I can't seem to figure out how to do that, as I have no choice but to load the given value as a dword.