Calling Conventions for Intel 64

  • %rsp contains the stack pointer,
  • %rbp contains the frame pointer,
  • %rax contains the return value, and
  • %rbxand %r12 to %r15 are callee-save registers.

Additionally:

  • The stack must always be 16-byte aligned, i.e. %rsp mod 16 == 0.

In order to ensure tha alignment is maintained at a 16-byte alignment, the stack may need to be manually aligned after function entry.

When a function is entered via call, the return address is implicitly pushed onto the stack, misaligning it by one word (8 bytes). When the return stack pointer is then pushed onto the stack in the function prologue, the stack is re-aligned.

Further pushes should occur in pairs or should be resolved using a subq instruction to re-align the stack to 16 bytes.

It is also the responsibility of the caller to ensure that when a function is called that the stack remains aligned to 16-bytes up to the call instruction.

Prologue

  1. push %rbp onto the stack,
  2. read the current value of %rsp into %rbp
  3. Reserve the stack frame and re-align the stack to 16-bytes.
push %rbp
mov  %rsp, %rbp
// This will already be aligned to 16 bytes due to the empty stack
// frame.
// subq $0x0, %rsp

Epilogue

leave
ret

Argument Passing

Arguments are passed first using registers %rdi, %rsi, %rdx, %rcx, %r8, %r9, %xmm0%xmm7, then on the stack in %rbp+8+(8 * n)