Skip to main content
Engineering LibreTexts

12.9: Summary

  • Page ID
    58317
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This section presents a brief summary of the standard calling convention requirements which are as follows:

    Caller Operations:

    • The first six integer arguments are passed in registers
      • rdi, rsi, rdx, rcx, r8, r9
    • The \(7^{th}\) and on arguments are passed on the stack-based
      • Pushes the arguments on the stack in reverse order (right to left, so that the first stack argument specified in the function call is pushed last).
      • Pushed arguments are passed as quadwords.
    • The caller executes a call instruction to pass control to the function (callee).
    • Stack-based arguments are cleared from the stack.
      • add rsp, <argCount*8>

    Callee Operations:

    • Function Prologue
      • If arguments are passed on the stack, the callee must save rbp to the stack and move the value of rsp into rbp. This allows the callee to use rbp as a frame pointer to access arguments on the stack in a uniform manner.
        • The callee may then access its parameters relative to rbp. The quadword at [rbp] holds the previous value of rbp as it was pushed; the next quadword, at [rbp+8], holds the return address, pushed by the call. The parameters start after that, at [rbp+16].
      • If local variables are needed, the callee decreases rsp further to allocate space on the stack for the local variables. The local variables are accessible at negative offsets from rbp.
      • The callee, if it wishes to return a value to the caller, should leave the value in al, ax, eax, rax, depending on the size of the value being returned.
        • A floating-point result is returned in xmm0.
      • If altered, registers rbx, r12, r13, r14, r15 and rbp must be saved on the

        stack.

    • Function Execution
      • The function code is executed.
    • Function Epilogue
      • Restores any pushed registers.
      • If local variables were used, the callee restores rsp from rbp to clear the stack-based local variables.
      • The callee restores (i.e., pops) the previous value of rbp.
      • The call returns via ret instruction (return).

    Refer to the sample functions to see specific examples of the calling convention.


    12.9: Summary is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?