SPRUI03E June 2015 – January 2023
You can define a macro anywhere in your program, but you must define the macro before you can use it. Macros can be defined at the beginning of a source file or in a .copy/.include file (see Copy Source File); they can also be defined in a macro library. For more information about macro libraries, see Section 7.5.
Macro definitions can be nested, and they can call other macros, but all elements of the macro must be defined in the same file. Nested macros are discussed in Section 7.10.
A macro definition is a series of source statements in the following format:
macname | .macro [parameter1] [, ... ,parametern ] |
model statements or macro directives | |
[.mexit] | |
.endm |
macname | names the macro. You must place the name in the source statement's label field. Only the first 128 characters of a macro name are significant. The assembler places the macro name in the internal opcode table, replacing any instruction or previous macro definition with the same name. |
.macro | is the directive that identifies the source statement as the first line of a macro definition. You must place .macro in the opcode field. |
parameter1, parametern |
are optional substitution symbols that appear as operands for the .macro directive. Parameters are discussed in Section 7.4. |
model statements | are instructions or assembler directives that are executed each time the macro is called. |
macro directives | are used to control macro expansion. |
.mexit | is a directive that functions as a goto .endm. The .mexit directive is useful when error testing confirms that macro expansion fails and completing the rest of the macro is unnecessary. |
.endm | is the directive that terminates the macro definition. |
If you want to include comments with your macro definition but do not want those comments to appear in the macro expansion, use an exclamation point to precede your comments. If you do want your comments to appear in the macro expansion, use an asterisk or semicolon. See Section 7.8 for more information about macro comments.
The following example shows the definition, call, and expansion of a macro.
Macro definition: The following code defines a macro, sadd4, with four parameters:
1 sadd4 .macro r1,r2,r3,r4
2 !
3 ! sadd4 r1, r2 ,r3, r4
4 ! r1 = r1 + r2 + r3 + r4 (saturated)
5 !
6 SADD r1,r2,r1
7 SADD r1,r3,r1
8 SADD r1,r4,r1
9 .endm
Macro call: The following code calls the sadd4 macro with four arguments:
10
11 00000000 sadd4 A0,A1,A2,A3
Macro expansion: The following code shows the substitution of the macro definition for the macro call. The assembler substitutes A0, A1, A2, and A3 for the r1, r2, r3, and r4 parameters of sadd4.
1 00000000 00040278 SADD A0,A1,A0
1 00000004 00080278 SADD A0,A2,A0
1 00000008 000C0278 SADD A0,A3,A0