Results 1 to 6 of 6

Thread: Difference between CDECL and STDCALL?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Difference between CDECL and STDCALL?

    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.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Difference between CDECL and STDCALL?

    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 by Ben321; Nov 19th, 2015 at 09:56 PM.

  3. #3

    Re: Difference between CDECL and STDCALL?

    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!

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Difference between CDECL and STDCALL?

    Quote Originally Posted by taiba123 View Post
    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.

  5. #5

    Re: Difference between CDECL and STDCALL?

    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Difference between CDECL and STDCALL?

    Quote Originally Posted by taiba123 View Post
    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.
    According to https://blogs.msdn.microsoft.com/old...8-00/?p=41163/ both CDECL and STDCALL push parameters from right to left.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width