Example 16. Using Recursive Macros

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