Computer Science Department
Johns Hopkins
University
600.333/433
Computer Systems Fundamentals
Fall 2002
(Last updated:Tuesday,
15-Oct-2002 17:47:19 EDT)
Programming project 2:
Designing low level recursion
Due date Friday December 6, 2002
(By midnight)
1. Objective
- Understand how to use recursion to define concepts.
- Learn to implement a recursive function using low level instructions.
2. General Instructions
For this project you are required to implement
several programs under the MIPS programming language, and test them by using the
RISC simulator SPIM. There are several questions about your programs you must
answer and send them along with the programs.
Submit your programs by midnight of the due date to the CSF address, cs333@cs.jhu.edu. Please put all the programs
in the same message as several different attachments. Also, type the answers to
the questions on this labwithin the same e-mail where your programs are.
If
you need a guide to start working with SPIM, read the document Working with SPIM
, available at http://www.cs.jhu.edu/~cs333/#weblinks.
For a complete reference on the MIPS programming language use the textbook
Computer Organization and Design, 2nd ed. , by Hennessy and Patterson,
chapter 3 and appendix A.
- IMPORTANT: In all your submissions, please state clearly your name,
major, e-mail and level (333 or 433). Also, thoroughly comment your code.
- NOTE: The postmark in your e-mail message will indicate the time
you submitted the programs, and at most it shall be 11:59 PM on the due date. .
- REMEMBER: The concepts involved in this lab may be matter for
exams.
3. Programming Activities
- Hand trace the following recursive function and identify its purpose,
then implement it. The program must work correctly for any integer between -50 and +50.
fun (n)
if n = 0 then return 0
else if n < 1 then return fun (-1*n)
else return 1+fun(n-1)
end fun
The value n must be read from the keyboard and your program must
display the resulting number.
Questions
- What would be an appropriate name for the function?
- What is the maximum value n that can be handled without producing
a stack overflow? (Hint: Try numbers beyond the range above stated.)
- Describe the way you pass one parameter to a function.
- Show implementions of stack operations push and pop.
- Is there any significant difference between growing the stack up or down?
(Hint: Sketch a drawing of the computer memory, and follow the function.)
- What happens if your program for fun does not test the base case for n = 0?
- Implement the following recursive Fibonacci function [Hennessy's textbook,
p.205, problem 3.27]. Note that n must be greater than or equal to 0. Your program must work correctly for any integer less than 21.
Notice that the first call must be fib (1,0,n).
fib (a,b,n)
if n = 0 then return b
else return fib (a+b,a,n-1)
end fib
The value n must be read from the keyboard and your program must
produce the nth Fibonacci number.
Questions
- Describe the way you are passing the 3 parameters, a, b, and n to the Fibonacci function.
- Why is the base case tested before any other operation?
- What is the difference between your assembly language program for the Fibonacci function shown above with respect to the program for the original Fibonacci function shown below:
[Hennessy's textbook, p.205, problem 3.26].
fib (n)
if n = 0 then return 0
else if n = 1 then return 1
else return fib(n-1) + fib(n-2)
end fib
- Write the MIPS program corresponding to the previous function (you don't have
to test it.)
4. Iteration and recursion (only for 433 students)
Any concern about the goals, questions, or wording of this document, please
send a message to Jorge Vasconcelos.
Copyright © 2002 Jorge Vasconcelos-Santillan. Johns Hopkins University,
Department of Computer Science.
Return to the CSF Homepage