Skip to main content
Engineering LibreTexts

9.4: Stack Example

  • Page ID
    19914
  • \( \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 following is an example program to use the stack to reverse a list of quadwords in place. Specifically, each value in a quadword array is placed on the stack in the first loop. In the second loop, each element is removed from the stack and placed back into the array (over-writing) the previous value.

    ; Simple example demonstrating basic stack operations.
    
    ; Reverse a list of numbers - in place.
    ; Method: Put each number on stack, then pop each number
    ;         back off, and then put back into memory.
    
    ; *****************************************************
    ;  Data declarations
    
    section .data
    
    ; -----
    ;  Define constants
    
    EXIT_SUCCESS     equ     0             ; successful operation
    SYS_exit         equ     60            ; call code for terminate
    
    ; -----
    ;  Define Data.
    
    numbers         dq        121, 122, 123, 124, 125 
    len             dq        5 
    
    ; **************************************************** section .text
    global _start
    _start: 
    
    ;  Loop to put numbers on stack.
    
        mov     rcx, qword [len] 
        mov     rbx, numbers
        mov     r12, 0
        mov     rax, 0
    
    pushLoop:
        push    qword [rbx+r12*8]
        inc     r12 
        loop    pushLoop
    
    ; -----
    ;  All the numbers are on stack (in reverse order).
    ;  Loop to get them back off.
    ;  the original list...
    
        mov     rcx, qword [len] 
        mov     rbx, numbers 
        mov     r12, 0
    
    popLoop:
        pop     rax
        mov     qword [rbx+r12*8], rax 
        inc     r12
        loop    popLoop
    
    ; -----
    ;  Done, terminate program.
    
    last:
        mov     rax, SYS_exit                 ; call code for exit
        mov     rdi, EXIT_SUCCESS             ; exit with success
        syscall 
    

    There are other ways to accomplish this function (reversing a list), however this is meant to demonstrate the stack operations.


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

    • Was this article helpful?