.TITLE Factorial ; Function Factorial (K) ; Calculates K! for 0 <= K <= 12 using recursion. ; Argument List: ; Parameter Offset Purpose Type Passed by K = 4 ; Function argument Longword Reference ; Result: ; K! returned as a longword in R0 ; Algorithm (Assumes K is nonnegative): ; 1. Find room on stack for local variable ; 2. Get K and calculate K - 1 ; 3. If K - 1 <= 0 then ; Factorial <-- 1 ; else ; Factorial <-- Factorial(K - 1) * K ; Register usage: ; R0 Returns K factorial ; R2 Holds K ; Local Storage: KMinus1 = -4 ; K - 1 LocalStorage = 4 ; Number of bytes of local storage needed .ENTRY Factorial, ^M Begin: SUBL2 #LocalStorage, SP ; Make room on stack for ; local variable MOVL @K(AP), R2 ; Put K in R2 SUBL3 #1, R2, KMinus1(FP) ; Calculate K - 1 BGTR Bigger ; If K - 1 <= 0 then MOVL #1, R0 ; Factorial <-- 1 BRB Done ; else Bigger: PUSHAL KMinus1(FP) ; Factorial <-- CALLS #1, Factorial ; Factorial(K - 1) MULL2 R2, R0 ; * K Done: RET ; Return from procedure .END