SPRU513Y August 2001 – June 2022 SM320F28335-EP
The macro language supports recursive and nested macro calls. This means that you can call other macros in a macro definition. You can nest macros up to 32 levels deep. When you use recursive macros, you call a macro from its own definition (the macro calls itself).
When you create recursive or nested macros, you should pay close attention to the arguments that you pass to macro parameters because the assembler uses dynamic scoping for parameters. This means that the called macro uses the environment of the macro from which it was called.
Using Nested Macros shows nested macros. The y in the in_block macro hides the y in the out_block macro. The x and z from the out_block macro, however, are accessible to the in_block macro.
in_block .macro y,a
. ; visible parameters are y,a and x,z from the calling macro
.endm
out_block .macrox,y,z
. ; visible parameters are x,y,z
.
in_block x,y ; macro call with x and y as arguments
.
.
.endm
out_block ; macro call
Using Recursive Macros shows recursive and fact macros. The fact macro produces assembly code necessary to calculate the factorial of n, where n is an immediate value. The result is placed in the A register. The fact macro accomplishes this by calling fact1, which calls itself recursively.
1 .fcnolist
2
3 fact .macro N, LOC
4
5 .if N < 2
6 MOV @LOC, #1
7 .else
8 MOV @LOC, #N
9
10
11 .eval N-1, N
12 fact1
13
14 .endif
15 .endm
16
17 fact1 .macro
18 .if N > 1
19 MOV @T, @LOC
20 MPYB @P, @T, #N
21 MOV @LOC, @P
22 MOV ACC, @LOC
23 .eval N - 1, N
24 fact1
25
26 .endif
27 .endm