Calling Conventions

Question: What are the popular Windows calling conventions?

Answer:

Calling convention is the rules or schemes between caller and callee when function call is made. It mainly describes how parameters are passed and who is resposible for stack cleanup.

x86 Calling Conventions

Below table shows the brief summary of x86 calling conventions in Windows.

cdecl
  • C/C++ default calling convention
  • Push parameters onto stack in the right-to-left order
  • Caller cleans the stack
  • Name decoration: _foo
stdcall
  • Most Win32 APIs use stdcall
  • Push parameters onto stack in the right-to-left order
  • Callee cleans the stack
  • Name decoration: _foo@8
fastcall
  • Pass first two parameters to ECX/EDX registers. Remaining parameters are pushed onto stack from right to left. For functions with 2 or less arguments, function call is fast since stack push and cleanup are not needed.
  • Callee cleans the stack
  • Name decoration: @foo@8
thiscall
  • Pass this pointer to ECX register. Remaining parameters are pushed onto stack from right to left. For functions with 2 or less arguments, function call is fast since stack push and cleanup are not needed.
  • Callee cleans the stack
  • No name decoration

x64 Calling Convention

Unlike x86 calling conventions, there is only one x64 calling convention in Windows, which is called "x64 calling convention."

  x64  
  • Pass first 4 parameters to registers (RCX/RDX/R8/R9 for 64bit int types; XMM0~XMM3 for 128bit float types). Remaining parameters are pushed onto stack from right to left.
  • Caller reserves spaces for at least 4 parameters (called home space) and callee typically copies parameters to home space at function prolog.
  • 16 bytes memory alignment