Skip to main content
Engineering LibreTexts

9.3: Stack Implementation

  • Page ID
    19913
  • \( \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 rsp register is used to point to the current top of stack in memory. In this architecture, as with most, the stack is implemented growing downward in memory.

    Stack Layout

    As noted in Chapter 2, Architecture, the general memory layout for a program is as follows:

    截屏2021-07-27 下午1.12.35.png

    The heap is where dynamically allocated data will be placed (if requested). For example, items allocated with the C++ new operator or the C malloc() system call. As dynamically allocated data is created (at run-time), the heap typically grows upward. However, the stack starts in high memory and grows downward. The stack is used to temporarily store information such as call frames for function calls. A large program or a recursive function may use a significant amount of stack space.

    As the heap and stack expand, they grow toward each other. This is done to ensure the most effective overall use of memory.

    A program (Process A) that uses a significant amount of stack space and a minimal amount of heap space will function. A program (Process B) that uses a minimal amount of stack space and a very large amount of heap space will also function.

    For example:

    截屏2021-07-27 下午1.13.12.png

    Of course, if the stack and heap meet, the program will crash. If that occurs, there is no memory available.

    Stack Operations

    The basic stack operations of push and pop adjust the stack pointer register, rsp, during their operation.

    For a push operation:

    1. The rsp register is decreased by 8 (1 quadword).
    2. The operand is copied to the stack at [rsp].

    The operand is not altered. The order of these operations is important. For a pop operation:

    1. The current top of the stack, at [rsp], is copied into the operand.
    2. The rsp register is increased by 8 (1 quadword).

    The order of these operations is the exact reverse of the push. The item popped is not actually deleted. However, the programmer cannot count on the item remaining on the stack after the pop operation. Previously pushed, but not popped, items can be accessed.

    For example:

         mov      rax, 6700             ; 670010 = 00001A2C16
         push     rax
         mov      rax, 31               ; 3110 = 0000001F16
         push     rax
    

    Would produce the following stack configuration (where each box is a byte):

    截屏2021-07-27 下午1.14.13.png

    The layout shows the architecture is little-endian in that the least significant byte is placed into the lowest memory location.


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

    • Was this article helpful?