views:

340

answers:

3

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 :)

A: 

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.

Joel
+3  A: 

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.

extern "C" int    __cdecl   function1(int, double);
extern "C" double __stdcall function2(char, char*);

int a = 5;
double b = 5.0;
int retval1;
char c = '5';
char *d = "Hello";
double retval2;

__asm {
    push b
    push a
    call function1
    add esp, 4*2 // "pop" what we pushed
    mov retval1, eax
    push d
    push c
    call function2
    mov retval2, eax
}
Deadcode
For extra peace of mind, after you've figured out what it *should* be, make the equivalent call from C++, compile with optimizations disabled, and look at the assembly output. It should roughly match what you're doing.
Scott Smith
But then, how would you push a double? In c++ it is supposed to be 8 bytes, right?
Emil D
+8  A: 
I. J. Kennedy