Skip to content

Instantly share code, notes, and snippets.

@cacharle
Created November 24, 2019 20:48
Show Gist options
  • Save cacharle/da47c227811ab1b438812a711e0670b5 to your computer and use it in GitHub Desktop.
Save cacharle/da47c227811ab1b438812a711e0670b5 to your computer and use it in GitHub Desktop.
cheat sheet of c calling conventions
; Only need to save reg if we use them after
; caller rules:
; 1. save caller-saved reg r10, r11
; /!\ all param reg
; 2. pass param with regs rdi, rsi, rdx, rcx, r8, r9
; if there is more pass them onto the stack in reverse order
; 3. use call
; 4. restore stack state by removing passed on the stack param
; 5. return value of callee in rax
; 6. restore the caller-saved register and saved passed params
; We can assume that no other register has been altered.
; callee rules:
; 1. allocate local variables by sub sum of sizes of thoses var to rsp
; the stack grows downward so we will access those local by adding to rsp.
; 2. push callee-saved reg onto the stack: rbx, rbp, r12, r13, r14, r15.
;
; >>> function body <<<
;
; 3. place return value in rax
; 4. restore callee-saved registers
; 5. remove local var, add to the stack the previoulsy subtracted amount.
; 6. use ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment