Skip to main content
Engineering LibreTexts

11.3: Macro Example

  • Page ID
    19927
  • \( \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 demonstrates the definition and use of a simple macro.

    ; Example Program to demonstrate a simple macro
    
    ; ************************************************** 
    ; Define the macro
    ;   called with three arguments: 
    ;        aver    <1st>, <len>, <ave>
    
    %macro     aver     3 
        mov    eax, 0
        mov ecx, dword [%2]                 ; length
        mov r12, 0
        lea rbx, [%1]
        
    %%sumLoop:
        add     eax, dword [rbx+r12*4]      ; get list[n]
        inc     r12
        loop    %%sumLoop
    
        cdq
        idiv    dword [%2]
        mov     dword [%3], eax
    
    %endmacro
    
    ; **************************************************
    ;  Data declarations
    
    section .data
    
    ; -----
    ;  Define constants
    
    EXIT_SUCCESS     equ     0                     ; success code
    SYS_exit         equ     60                    ; code for terminate
    
    ; Define Data. 
    
    section     .data
    list1       dd     4, 5, 2, -3, 1
    len1        dd     5
    ave1        dd     0
    
    list2       dd     2, 6, 3, -2, 1, 8, 19
    len2        dd     7
    ave2        dd     0
    ; **************************************************
    
    section     .text 
    global  _start 
    _start:
    
    ; -----
    ;  Use the macro in the program
    
        aver list1, len1, ave1                    ; 1st, data set 1
        aver list2, len2, ave2                    ; 2nd, data set 2
    
    ; -----
    ;  Done, terminate program.
    
    last:
        mov rax, SYS_exit                         ; exit
        mov rdi, EXIT_SUCCESS                     ; success
        syscall         
    

    In this example, the macro is invoked twice. Each time the macro is used, it is copied from the definition into the text section. As such, macros typically use more memory.


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

    • Was this article helpful?