Skip to content

Instantly share code, notes, and snippets.

@ilbaroni
Forked from sucremad/callcon.md
Created October 25, 2021 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilbaroni/94ba62647eea85b73a2062d4b3e4f167 to your computer and use it in GitHub Desktop.
Save ilbaroni/94ba62647eea85b73a2062d4b3e4f167 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/registers 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)

Code

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

; for x64

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