Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Difference between CDECL and STDCALL?

Ben321

New member
Joined
Oct 25, 2008
Messages
997
I am learning assembly language, and want to make a function that will be compiled into a DLL file, My assembler is NASM, and linker is GoLink. I want the DLL file's functions to be able to be called from VB6, and I know VB6 is only able to use STDCALL functions directly. I remember reading that in STDCALL, there are certain registers that must be preserved, so that when the function is finished, the final state is the same as the initial state. I also remember reading that with CDECL the function is not required to preserve all of these registers (but I think it is required to preserve some of them), and the caller must know to return everything to its initial state before the function was called. Can you tell me exactly what must be preserved, and what is not required to be, for each of these calling conventions?

I need to know EXACTLY what must be preserved by the called function (and what doesn't need to be), in each of these 2 calling conventions, because when writing in assembly code, you are writing every executed instruction by hand. This is different than a language like C++ where the compiler automatically creates the required assembly code from the higher level instructions, based on the compiler settings you use to set the calling convention.
 

Ben321

New member
Joined
Oct 25, 2008
Messages
997
Sorry to bump this, but I need to know this information. There used to be a website that had these 2 very nice diagrams. It showed what the stack looked like after a CDECL function was called and after a STDCALL function was called. It showed how they were different (such as which registers got pushed onto the stack, what parts were the caller was responsible for cleaning up, and what parts the callee was responsible for cleaning up). I can't find the site that had this pair of diagrams now though. I'm hoping somebody else here has the info I need. Please help me.

From the best of my recollection, for STDCALL, the order things were pushed onto the stack are.
Last function parameter
Other function parameters
First function parameter
EIP register
EBP register
First function variable
Other function variables
Last function variable
Any other registers that are required to be preserved (EDI and ESI if I remember correctly are the only other registers that must be preserved)

In STDCALL the callee is required to clean up everything, if I'm remembering correctly. Problem is, I don't know if I'm remembering correctly.

The other thing I don't know is how this differs from CDECL. Does what is put in the stack differ? Is the caller responsible for allocating local variables for the function? Which parts of the stack is the caller responsible for cleaning up? I know that one thing that differs is that the callee doesn't clean up the part of the stack that contains the parameters that were passed to it. This is the job of the caller in CDECL calling convention. But I'm not sure if that's the only difference.

Again, please help me figure this out.
 
Last edited:

taiba123

New member
Joined
Sep 2, 2015
Messages
3
The main difference between the two lies in who is responsible for cleaning the stack for local variables. For cdecl, this is the caller while for stdcall this is the callee (the function being called). Of course if you're interfacing with external libraries and give the wrong one, the stack would get corrupted and magic things would happen (hope it doesn't destroy your computer). NEVER try it on space shuttle softwares!
 

Ben321

New member
Joined
Oct 25, 2008
Messages
997
The main difference between the two lies in who is responsible for cleaning the stack for local variables. For cdecl, this is the caller while for stdcall this is the callee (the function being called). Of course if you're interfacing with external libraries and give the wrong one, the stack would get corrupted and magic things would happen (hope it doesn't destroy your computer). NEVER try it on space shuttle softwares!

I was thinking it was who was responsible for cleaning up the function's parameters. While it is possible for either the caller or the callee to clean up the parameters, there's no way for the caller to know how many bytes are in the called function's local variables. So I don't see how it would be possible for the caller to clean up the stack from variables stored by the callee.
 

taiba123

New member
Joined
Sep 2, 2015
Messages
3
Oh , one more thing, stdcall and cdecl differ not only in who cleans up the stack but also in the sequence how the parameters are pushed on the stack, which is what makes varargs-calls possible with cdecl only.
 
Top