SPNU118Z September 1995 – March 2023 66AK2E05 , 66AK2H06 , 66AK2H12 , 66AK2H14 , AM1705 , AM1707 , AM1802 , AM1806 , AM1808 , AM1810 , AM5K2E04 , OMAP-L132 , OMAP-L137 , OMAP-L138 , SM470R1B1M-HT , TMS470R1A288 , TMS470R1A384 , TMS470R1A64 , TMS470R1B1M , TMS470R1B512 , TMS470R1B768
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 data memory address loc. The fact macro accomplishes this by calling fact1, which calls itself recursively.
fact .macro N, loc ; N is an integer constant. Register loc address = N!
.if N < 2 ; 0! = 1! = 1
MOV loc, #1
.else
MOV loc, #N ; N >= 2 so, store N in loc.
.eval -1, N ; Decrement N, and do the factorial of N - 1.
fact1 ; Call fact with current environment.
.endm
fact1 .macro
.if N > 1
MOV R0, #N ; N > 1 so, store N in R0.
MUL loc, R0, loc ; Multiply present factorial by present position.
.eval N - 1, N ; Decrement position.
fact1 ; Recursive call.
.endif
.endm