Skip to main content
Engineering LibreTexts

12.6: Linkage

  • Page ID
    19937
  • \( \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}}\)

    The linkage is about getting to and returning from a function call correctly. There are two instructions that handle the linkage, call <funcName> and ret instructions.

    The call transfers control to the named function, and ret returns control back to the calling routine.

    • The call works by saving the address of where to return to when the function completes (referred to as the return address). This is accomplished by placing contents of the rip register on the stack. Recall that the rip register points to the next instruction to be executed (which is the instruction immediately after the call).
    • The ret instruction is used in a procedure to return. The ret instruction pops the current top of the stack (rsp) into the rip register. Thus, the appropriate return address is restored.

    Since the stack is used to support the linkage, it is important that within the function the stack must not be corrupted. Specifically, any items pushed must be popped. Pushing a value and not popping would result in that value being popped off the stack and placed in the rip register. This would cause the processor to attempt to execute code at that location. Most likely the invalid location will cause the process to crash.

    The function calling or linkage instruction is summarized as follows:

    Instruction

    Explanation

    call   <funcName>
    

    Calls a function. Push the 64-bit rip register and jump to the <funcName>.

    Examples:

    call  printString
    

    ret

    Return from a function. Pop the stack into the rip register, effecting a jump to the line after the call.

    Examples:

    ret

    A more complete list of the instructions is located in Appendix B.


    This page titled 12.6: Linkage is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?