-
Notifications
You must be signed in to change notification settings - Fork 131
Description
In factorial(n), the function iter(product, counter) is seeded by the arguments (1,1) and runs until counter > n, at which point the product is returned. Unlike gcd(a, b), the register machine has no need of a temporary register assuming that the correct order of register assignment is followed.
For the data path, we see 3 registers: product, counter, and n. There are 2 operations: multiply and plus with 2 assignments. There is 1 test: greater than.
For the controller, we start by filling the 3 registers with the desired factorial n and the value 1 (seeding product and counter). The function iter runs and tests whether counter > n. If counter <= n, then product is updated to product * counter, followed by counter being updated to counter + 1 and the process is repeated until counter > n. Once counter is > n, the product is factorial(n) and can be returned.