Function

Function Call

  • Stack vs register calling convention
    • Wether the arguments are stored on the stack or in the registers.
    • Refer to the ABI convention.
    • May be alternated in the program, e.g. x86 offers cdecl and fastcall instruction
    • Register calling is faster for leaf functions (don’t call function and don’t access memory)
    • Reg Calling is likely to be used in architectures that have abundant registers.
  • Stack Calling Convention
    • Save caller saved registers.
    • Push args in reverse order
    • Call the function = PUSH PC, and JUMP f
    • Leave room for local variables
    • Save callee saved registers registers in the stack.
    • Such “preamble” is common across languages, and can be used to identify function calls even without debugging symbols.
  • Register Calling Convention
    • Save registers
    • Move n args to R{0..n}
    • Additional args are pushed to stack
    • Call f
    • Before pushing local variables to the stack, save space for args, as they need to be saved when calling a new function before return!
  • What happens on stack
    • CALL instruction pushes return address to stack
    • PUSH %rbp
    • MOV %rsp %rbp - new stack frame
    • MOV %rbp %rsp - clean up frame
    • POP %rbp - go back to previous stack frame1
    • RET