5.2 Exercise 5.2
This is a literal translation of the iterative factorial algorithm. The product and counter are both initialized to 1 at the start, mimicking the initial call to the inner recursive function. Then, in every iteration, we check whether the counter is greater than the original input n. If it is, we finish, with the final result being stored in the product register. If it isn’t, then we update the product and increment the counter before starting again.
Here’s an implementation of the given factorial function:
(controller (assign product (const 1)) (assign counter (const 1)) test-counter (test (op >) (reg counter) (reg n)) (branch (label factorial-done)) (assign t (op *) (reg product) (reg counter)) (assign product (reg t)) (assign counter (op +) (const 1) (reg counter)) (goto (label test-counter)) factorial-done)