October 16, 2017

The calling convention(cdecl, stdcall, fastcall)

❑ The calling convention is a convention used in the function call process. There are three kinds of this.


❑ CDECL : It is the abbreviation of C language DECLaration.
* C language uses CDECL.
StandardCharacteristic
Parameter transfer method
Using stack frame
Parameter transfer sequenceRight to left
Return method4 Bytes↓ : EAX,
4~8 Bytes : EDX(High 4 bytes), EAX(Low 4 bytes)
Stack frame cleanupThe caller cleans up the stack frame of the callee
* How to call sub(a, b).
PUSH b
PUSH a
CALL sub()
ADD ESP, 8
   * Caller : The function of the calling role.
   * Callee : The function of the role being called.


❑ STDCALL : It is the abbreviation of the STanDard CALL.
* Win32 API uses STDCALL. It improves compatibility with languages other than C language.
StandardCharacteristic
Parameter transfer method
Using stack frame
Parameter transfer sequenceRight to left
Return method4 Bytes↓ : EAX,
4~8 Bytes : EDX(High 4 bytes), EAX(Low 4 bytes)
Stack frame cleanupThe callee cleans up the stack frame by itself
* How to call sub(a, b).
PUSH b
PUSH a
CALL sub()


❑ FASTCALL : It means that it is faster to use registers than to use memory.
StandardCharacteristic
Parameter transfer method
Using register and stack frame
* ECX and EDX are used when the number of parameters is 2.
* Stack frame is used when parameters exceed 2.
Parameter transfer sequenceRight to left
Return method4 Bytes↓ : EAX,
4~8 Bytes : EDX(High 4 bytes), EAX(Low 4 bytes)
Stack frame cleanupThe callee cleans up the stack frame by itself
* How to call sub(a, b).
MOV EDX, b
MOV ECX, a
CALL sub()

* How to call sub(a, b, c, d).
PUSH d
PUSH c
MOV EDX, b
MOV ECX, a
CALL sub()