SLAU131Y October 2004 – June 2021
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
.if N < 2
MOV #1,&loc
.else
MOV #N,&loc
.eval N-1,N
fact1
.endif
.endm
fact1 .macro
.if N > 1
MOV #N,R12 ; Assume MPY requires args to be in R12,R13
MOV &loc,R13
CALL MPY
MOV R12,&loc ; Assume MPY returns product in R12
.eval N - 1,N
fact1
.endif
.endm
.global fact_result
.global MPY
fact 5,fact_result