Skip to main content
Engineering LibreTexts

8.2: Example Program, List Summation

  • Page ID
    19905
  • \( \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 example program will sum the numbers in a list.

    ; Simple example to the sum and average for
    ; a list of numbers.
    
    ; *****************************************************
    ;  Data declarations
    
    section .data 
    
    ; -----
    ;  Define constants
    
    EXIT_SUCCESS equ 0             ; successful operation
    SYS_exit equ 60                ; call code for terminate
    
    ; -----
    ; Define Data.
    
    section .data 
        lst     dd     1002, 1004, 1006, 1008, 10010
        len     dd     5
        sum     dd     0
    
    ; ******************************************************** 
    section .text
    global _start
    _start:
    
    ; -----
    ;  Summation loop.
    
        mov     ecx, dword [len]             ; get length value
        mov     rsi, 0                       ; index=0
    
    sumLoop:
        mov     eax, dword [lst+(rsi*4)]     ; get lst[rsi]
        add     dword [sum], eax             ; update sum
        inc     rsi                          ; next item
        loop     sumLoop
    
    ; -----
    ;  Done, terminate program.
    
    last:
        mov     rax, SYS_exit                 ; call code for exit
        mov     rdi, EXIT_SUCCESS             ; exit with success
        syscall        
    

    The ()'s within the [ ]'s are not required and added only for clarity. As such, the [lst+ (rsi*4)], is exactly the same as [lst+rsi*4].


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

    • Was this article helpful?