You copied the Doc URL to your clipboard.

21.51 MACRO and MEND

The MACRO directive marks the start of the definition of a macro. Macro expansion terminates at the MEND directive.


These two directives define a macro. The syntax is:

     MACRO{$label}  macroname{$cond} {$parameter{,$parameter}...}     ; code     MEND



is a parameter that is substituted with a symbol given when the macro is invoked. The symbol is usually a label.


is the name of the macro. It must not begin with an instruction or directive name.


is a special parameter designed to contain a condition code. Values other than valid condition codes are permitted.


is a parameter that is substituted when the macro is invoked. A default value for a parameter can be set using this format:

$parameter="default value"

Double quotes must be used if there are any spaces within, or at either end of, the default value.


If you start any WHILE...WEND loopsor IF...ENDIF conditions within a macro,they must be closed before the MEND directiveis reached. You can use MEXIT to enable anearly exit from a macro, for example, from within a loop.

Within the macro body, parameters such as $label, $parameter or $cond can be used in the same way as other variables. They are given new values each time the macro is invoked. Parameters must begin with $ to distinguish them from ordinary symbols. Any number of parameters can be used.

$label is optional. It is useful if the macro defines internal labels. It is treated as a parameter to the macro. It does not necessarily represent the first instruction in the macro expansion. The macro defines the locations of any labels.

Use | as the argument to use the defaultvalue of a parameter. An empty string is used if the argument isomitted.

In a macro that uses several internal labels, it is usefulto define each internal label as the base label with a differentsuffix.

Use a dot between a parameter and following text, or a followingparameter, if a space is not required in the expansion. Do not usea dot between preceding text and a parameter.

You can use the $cond parameter for condition codes. Use the unary operator :REVERSE_CC: to find the inverse condition code, and :CC_ENCODING: to find the 4-bit encoding of the condition code.

Macros define the scope of local variables.

Macros can be nested.


A macro that uses internal labels to implement loops:

 ; macro definition                MACRO                 ; start macro definition$label          xmac    $p1,$p2                ; code$label.loop1    ; code                ; code                BGE     $label.loop1$label.loop2    ; code                BL      $p1                BGT     $label.loop2                ; code                ADR     $p2                ; code                MEND                  ; end macro definition ; macro invocationabc             xmac    subr1,de      ; invoke macro                ; code                ; this is what isabcloop1        ; code                ; is produced when                ; code                ; the xmac macro is                BGE     abcloop1      ; expandedabcloop2        ; code                BL      subr1                BGT     abcloop2                ; code                ADR     de                ; code

A macro that produces assembly-time diagnostics:

        MACRO                        ; Macro definition        diagnose  $param1="default"  ; This macro produces        INFO      0,"$param1"        ; assembly-time diagnostics        MEND                         ; (on second assembly pass) ; macro expansion        diagnose            ; Prints blank line at assembly-time        diagnose "hello"    ; Prints "hello" at assembly-time        diagnose |          ; Prints "default" at assembly-time

When variables are being passed in as arguments, use of | might leave some variables unsubstituted. To work around this, define the | in a LCLS or GBLS variable and pass this variable as an argument instead of |. For example:

        MACRO                ; Macro definition        m2 $a,$b=r1,$c       ; The default value for $b is r1        add $a,$b,$c         ; The macro adds $b and $c and puts result in $a.        MEND                 ; Macro end        MACRO                ; Macro definition        m1 $a,$b             ; This macro adds $b to r1 and puts result in $a.        LCLS def             ; Declare a local string variable for |def     SETS "|"             ; Define |        m2 $a,$def,$b        ; Invoke macro m2 with $def instead of |                             ; to use the default value for the second argument.        MEND                 ; Macro end

A macro that uses a condition code parameter:

        AREA    codx, CODE, READONLY; macro definition        MACRO        Return$cond        [ {ARCHITECTURE} <> "4"          BX$cond lr          |          MOV$cond pc,lr         ]         MEND; macro invocationfun     PROC        CMP      r0,#0        MOVEQ    r0,#1        ReturnEQ        MOV      r0,#0                Return                ENDP        END
Was this page helpful? Yes No