Skip to main content
Engineering LibreTexts

18.8: Example Program, Sum and Average

  • Page ID
    58429
  • \( \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 example is a simple assembly language program to calculate the sum and average for a list of floating-point values.

    ; Floating-Point Example Program
    ; ***********************************************************
    
    section .data 
    
    ; -----
    ;  Define constants.
    
    NULL            equ 0        ; end of string
    TRUE            equ 1
    FALSE           equ 0
    
    EXIT_SUCCESS    equ 0        ; Successful operation
    SYS_exit        equ 60       ; system call code for terminate
    
    ; -----
    
    fltLst          dq  21.34,  6.15,  9.12, 10.05,  7.75
                    dq   1.44, 14.50,  3.32, 75.71, 11.87
                    dq  17.23, 18.25, 13.65, 24.24,  8.88
    length          dd  15
    lstSum          dq  0.0
    lstAve          dq  0.0
    
    ; *********************************************************** 
    section     .text
    global _start
    _start:
    
    ; -----
    ;  Loop to find floating-point sum.
          
        mov ecx, [length]
        mov rbx, fltLst
        mov rsi, 0
        movsd  xmm1, qword [lstSum]
          
    sumLp:
        movsd      xmm0, qword [rbx+rsi*8]        ; get fltLst[i]
        addsd      xmm1, xmm0                     ; update sum
        inc        rsi                            ; i++
        loop       sumLp                
        
        movsd      qword [lstSum], xmm1
        
    ; -----
    ; Compute average of entire list.
    
        cvtsi2sd    xmm0, dword [length]
        cvtsd2si    dword [length], xmm0
        divsd       xmm1, xmm0
        movsd       qword [lstAve], xmm1
        
    ; -----
    ; Done, terminate program. 
    
    last:
        mov    rax, SYS_exit
        mov    rbx, EXIT_SUCCESS                   ; exit w/success    
        syscall  
    

    The debugger can be used to examine the results and verify correct execution of the program.


    18.8: Example Program, Sum and Average is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?