Assignment 4

Due: 9:10am, at the start of class, Tue Oct 3rd, 2006

Note: Make reasonable assumptions where necessary and clearly state them. Feel free to discuss problems with classmates, but the only written material that you may consult while writing your solutions are the textbook and lecture slides.

For this assignment, you will have to hand all solutions in electronically. Log in to one of the CADE Lab machines (for example ssh to lab1-10.eng.utah.edu or replace 10 with any number between 1-48). Place your solution in a text file called "homework4.s" (if you use a different file name, our print script may not find it -- also, do not include a space in your filename) and use the following command:
handin cs3810 hw4 homework4.s

Grading: Your program will be graded on readability and user friendliness, as well as correctness. That means LOTS OF COMMENTS!! We won't test it with data that causes overflow, or with non-numerical input data. As with other homework assignments, the TA will print out and test it on a Unix machine in the CADE Lab. You should test your program on that platform before handing it in. The two versions of SPIM (Windows and Unix) are essentially the same, but the text file is the issue. A common problem is that some Windows editors insert strange characters in files, and a Unix machine sometimes doesn't understand them.

The previous assignment required you to write a simple MIPS program. See the list of resources for help getting started with SPIM. In this assignment, you'll write a more complex program that requires values to be saved and restored on the stack across procedure calls.

Problem description: The program must prompt the user and then accept a number N as input. It should confirm that N is greater than 0 and less than 10 (else, print an error message and quit). It should then call function "recursion" with the number N as argument. The function recursion can be represented by the following pseudo-code:

int recursion (int N) 
{
  int i, j, k;

  if (N equals 10) {
    print "Reached the end\n";
    return 0;
  }

  print "In recursion depth ";
  print N;
  print ":";
  for (i=0 ; i less than N ; i=i+1)
    print "x";
  print "\n";

  i = N + 5;
  k = N - 6;
  j = N + 1;
  j = recursion (j);
  j = j + i + k;
  
  print "In recursion depth ";
  print N;
  print ":";
  for (i=0 ; i less than N ; i=i+1)
    print "x";
  print ":";
  print j;
  print "\n";

  return j;
}

Expected output: If this program is given the input N=1, the output should be as follows:

In recursion depth 1:x
In recursion depth 2:xx
In recursion depth 3:xxx
In recursion depth 4:xxxx
In recursion depth 5:xxxxx
In recursion depth 6:xxxxxx
In recursion depth 7:xxxxxxx
In recursion depth 8:xxxxxxxx
In recursion depth 9:xxxxxxxxx
Reached the end
In recursion depth 9:xxxxxxxxx:17
In recursion depth 8:xxxxxxxx:32
In recursion depth 7:xxxxxxx:45
In recursion depth 6:xxxxxx:56
In recursion depth 5:xxxxx:65
In recursion depth 4:xxxx:72
In recursion depth 3:xxx:77
In recursion depth 2:xx:80
In recursion depth 1:x:81

Constraints: The value i is stored in register $s0, the value k is stored in register $t0. Use any register you like for value j. Your assembly instructions must be produced in the same order as that in the pseudo-code. In other words, don't try optimizations to reduce the number of instructions/registers/saves/restores (assume that there is some more code in the function that prevents such optimizations -- this code has not been shown to simplify the problem).

Tutorial: To fully understand the process of saving and restoring values on the stack, read this tutorial.

You may use other subroutines if you wish. If you decide to declare any integer constants or variables in the data section of your program, be sure to put the .word and .space directives before the .asciiz directives. The reason for doing this is that strings are generally not multiples of 4 bytes in length. Hence, the memory allocated for them may not end on a word boundary. If you put .word or .space directives after strings, the assembler may complain that you are trying to allocate memory for word-length data (i.e., integers) on a non-word boundary. (Another way to do that is to put a ".align 2" command before your .space or .word declarations. See page A-47 for an explanation.) You may assume that we won't test your code with input data that doesn't make sense.