So, I would like to be able to call functions from a c++ dll. For certain reasons, I would like to call them from an __asm block in my C++ code. My question is this: I know that before I call the function, I have to push its arguments on the stack in the order specified by the function's calling convention.However, can i simply do something like this:
int a=5;
double b = 5.0;
__asm{
push b
push a
call functionAddress
}
What worries me is the fact that I seem to remember that the standard word size in assembly is 2bytes, while the size of an int in C++ is usually 4bytes, and 8 bytes for a double.So , in the example above, am I really pushing the full value of each variable, or just the first couple of bytes? If the code above is not correct, what would be the right way to do it? Also, if the function we are calling returns a double, where is this value stored? I'm assuming it can't be in a register, because it can only store 32bits ( 4bytes ).Any help with this mess would be greatly appreciated :)
Generally, you would be pushing the full size of the computer word. This varies according to the chip, but on a 32 bit Intel would be 4bytes, and on a 64 bit Intel, would be 8 (depending on the compiler -- Visual Studio still only supports IA32 assembly -- so 4 bytes).
The best answer is to look at the documentation for your specific compiler.
The 32-bit x86 architecture automatically pads values being pushed onto the stack to 32 bits.
There is something you have to keep in mind. If the function you're calling uses the __cdecl calling convention, you must "pop" what you pushed afterwards. However, for __stdcall functions you must not do this.
To push 8-byte values such as doubles, you won't be able to use a regular
PUSH
instruction. And neither do you push floating-point parameters (or doubles) on to the floating-point stack. You need to put these fat parameters on the stack 'by hand'. For example, to push π as a parameter to a function f: