controller( list( assign("continue", label("fact_done")), // set up final return address "fact_loop", test(list(op("="), reg("n"), constant(1))), branch(label("base_case")), // Set up for recursive call by saving $\texttt{n}$ and $\texttt{continue}$. // Set up $\texttt{continue}$ so that the computation will continue // at $\texttt{after_fact}$ when the subroutine returns. save("continue"), save("n"), assign("n", list(op("-"), reg("n"), constant(1))), assign("continue", label("after_fact")), go_to(label("fact_loop")), "after_fact", restore("n"), restore("continue"), assign("val", // $\texttt{val}$ now contains $n(n-1)!$ list(op("*"), reg("n"), reg("val"))), go_to(reg("continue")), // return to caller "base_case", assign("val", constant(1)), // base case: 1! = 1 go_to(reg("continue")), // return to caller "fact_done"))
Figure 5.13 A recursive factorial machine.

[1] One might argue that we don't need to save the old n; after we decrement it and solve the subproblem, we could simply increment it to recover the old value. Although this strategy works for factorial, it cannot work in general, since the old value of a register cannot always be computed from the new one.
[2] In section 5.3 we will see how to implement a stack in terms of more primitive operations.
5.1.4   Using a Stack to Implement Recursion