Skip to content

Instantly share code, notes, and snippets.

@sucremad
Last active November 19, 2021 12:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sucremad/c3cf6ecde3f649e77b525c764e717f1e to your computer and use it in GitHub Desktop.
Save sucremad/c3cf6ecde3f649e77b525c764e717f1e to your computer and use it in GitHub Desktop.
Function Call Conventions

Most Common Calling Conventions

Most commons are cdecl, stdcall, fastcall

In function calls, parameters are pushed onto the stack from right to left.

Example Function Pseudo Code

int func(int x, int y, int z, int m, int k);
 
int a, b, c, d, e, ret;

ret = func(a, b, c, d, e);

cdecl

  • C declaration
  • uses stack frame
  • The caller cleans up the stack

Code

push e
push d
push c
push b
push a
call func
add esp, 20  ; cleaning up the stack. 5 integer parameters --> 4-bytes x 5 = 20

stdcall

  • Standart call
  • used by Windows API
  • uses stack frame
  • callee cleans up the stack

Code

push e
push d
push c
push b
push a
call func
; callee cleans up the stack so no need to add esp, 20 

fastcall

  • uses registers(for first few instructions) and stack frame
  • callee cleans up the stack
  • ecx, edx (32-bit)(Microsoft)
  • rcx, rdx, r8, r9 (64-bit)(Microsoft, Intel)
  • left-to-right for registers
  • right-to-left for stack

Code

mov ecx, a
mov edx, b
push e
push d
push c
call func

; for x64

mov rcx, a
mov rdx, b
mov r8, c
mov r9, d
push e
call func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment