A Simple Recursion

For this assignment you will write a MAL subprogram for computing the value of a function f described in the next section. You will need to use a stack-based subprogram linkage protocol. Also, you will write a short main program that will be used to test the subprogram.

The function f

The function f is defined recursively:

  f(0) = 0,
  f(n) = 3*f(n - 1) + 1, if n > 0

You must implement this function recursively to get full credit. If you take care with planning the use of registers the code won't be too difficult.

The main Program

The main program should prompt for an integer n, print the value of f(n), and repeat until the user enters a negative value for n.

Commenting

You may lose points if your code is not well commented. Your f function comments should clearly document its calling convention and its use of registers.

You will not get debugging help from the TA or myself if you do not have good comments. I recommend writing the function comments before writing the code. You will likely make fewer mistakes and have an easier time debugging if you do.

What to Turn in

Turn in both a copy of your program and a Mars session record. In this session, you should enter the following integers:

Your TA may also ask you to demonstrate your program in lab.

References