Reference no: EM133105198
Assignment: Recursion
By now, you should have a fairly thorough understanding of the stack mechanism and of its uses (e.g., storing local subroutine variables). In this assignment you are going to write a recursive subroutine that calculates the factorial of a number ("n!"). A recursive subroutine means that the subroutine will call itself, usually with a different parameter to calculate a part of the final solution. This subroutine will be about 14 instructions in length when it is finished, but writing it will be fairly difficult. Do not be discouraged if it takes you a few hours to get it working.
Exercise
Exercise 1. Copy your "inout" program to a new file called "factorial.s". Create a new subroutine called "factorial". This new subroutine should take one parameter, n, and for now it should simply do nothing and return n in the RAX register. Alter your inout subroutine in such a way that it calls factorial with the number it reads, instead of incrementing it by one. It should print the result of factorial on the terminal.
Exercise 2. Write a pseudocode specification of your factorial subroutine. The subroutine accepts a parameter n and it should return n!. Make sure your algorithm is recursive. It should not need to be more than a few lines of pseudocode.
Exercise 3. You may have your specification checked by one of the lab course assistants. The lab course assistants can prevent you from implementing the wrong algorithm. If you are confident your specification is correct, you don't have to ask an assistant to check and you may immediately proceed with the next step.
Exercise 4. Implement your factorial routine. Test your program thoroughly.
This exercise wraps up the basic assembler programming assignments. You should go and have your code checked by a lab course assistant. Well done!