Skip to main content
Engineering LibreTexts

18.9: Example Program, Absolute Value

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

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    This example is a simple assembly language program to find the absolute value of a floating-point value in order to demonstrate floating-point comparisons. Recall that if a value is negative, it must be made positive and if the value is already positive, nothing should be done.

    ; Floating-Point Absolute Value Example 
    
    section .data
    
    ; -----
    ;  Define constants.
    
    TRUE         equ     1 
    FALSE        equ     0 
    
    SUCCESS      equ     0                 ; successful operation
    SYS_exit     equ     60                ; call cdoe for terminate
    
    ; -----
    ;  Define some test variables.
    
    dZero        dq      0.0
    dNegOne      dq      -1.0
    
    fltVal       dq      -8.25
    
    ; ********************************************************* 
    section     .text
    global _start
    _start:
    
    ; -----
    ;  Perform absolute value function on flt1
    
        movsd        xmm0, qword [fltVal]
        ucomisd      xmm0, qword [dZero]
        jae          isPos
        mulsd        xmm0, qword [dNegOne]
        movsd        qword [fltVal], xmm0
    isPos:
    
    ; ----- 
    ;   Done, terminate program.
    
    last:
        mov    rax, SYS_exit
        mov    rbx, EXIT_SUCCESS               ; exit w/success
        syscall  
    

    In this example, the final result for |fltVal| was saved to memory. Depending on the context, this may not be required.


    18.9: Example Program, Absolute Value is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?