CS 366 - Third SPIM Programming Assignment

Due: Wednesday, March 5, 2003 at 11:59 pm

For this assignment, you will write functions in SPIM. These functions are to follow the MIPS Convention for register usage. You are to write functions that will:

You program may have other functions, but the above must be included.

Your program is to have a data section that contains an array and a integer variable. The array should be able to store 100 integer values and the integer variable is to store the actual amount of value stored in the array. The values in these two elements may not be accessed directly (by a label) in any function. Instead, you are to pass this information to the functions by parameters.

Your program is to perform the following steps

  1. Fill the array with values from input using a function.
  2. Print out the values in the array (five values per line) using a function.
  3. Sort the array using a function.
  4. Print out the values in the newly sort array using the same function from step 2.
  5. For each value in the array compute the Fibonacci value twice Your program is to output the original value and the result from each version of the the Fibonacci function.

The function to read in input is to read integers values until a negative value is entered by or the array is filled. The negative value should not be stored in the array. This function should print a prompt to inform the user of what to enter. A possible function prototype in C could be:

     void readInput (int arr[], int maxSize, int* actualSize);

The function to output the values in the array should be called twice: after all of the values are stored into the array and after the array is sorted. This code should print 5 integer values per line. A possible function prototype in C could be:

     void printArray (int arr[], int actualSize);

The function to sort the integer array can use any sorting algorithm you wish. There is one given in the book, but it assumes the array has 50 elements in the array. A possible function prototype in C could be:

     void sortArray (int arr[], int actualSize);

The Fibonacci functions are to be called for each value in the array. You are to print out the original value and the result for each call to the Fibonacci functions. Your program is to use the Fibonacci sequence defined as follows:

      Fib (0) = 0
      Fib (1) = 1
      Fib (n) = Fib(n-1) + Fib(n-2); when n > 1
A possible function in C for the recursive Fibonacci function could be:
     int fibr (int n)
     {
      if ((n == 0) || (n == 1))
         return n;
      else
         return fibr(n-1) + fibr(n-2);
     }
A possible function in C for the non-recursive Fibonacci function could be:
     int fibnr (int n)
     {
      int m1, m2, c;
      int i;

      if ((n == 0) || (n == 1))
         return n;
      
      m1 = 0;
      m2 = 1;

      for (i = 2; i <= n; i++)
          {
           c = m1 + m2;
           m1 = m2;
           m2 = c;
          }
      return c;
     }

Note: since the Fibonacci function is doublely recursive it has an exponential run time, you can write your code so that the recursive function only runs with values of 30 or less. The recursive function with a value of 50 takes a couple of days to complete.

The programs will be graded based on how they execute on xspim running in the CS Department computers. You are to submit your program electronically using the the turnin command with the project name of mp3. Your program must be well commented, use blank lines to separate logical sections of your code and align labels, opcodes, operand and comments in neat columns.

Alternative Register Names in SPIM

This is from Figure 9.9 in the Goodman&Miller text
Register NameAlternative NameDescription
$0the value 0
$1$atreserved by the assembler
$2 - $3$v0 - $v1expression evaluation and function results
$4 - $7$a0 - $a3the first four parameters -
  not preserved across procedure calls
$8 - $15$t0 - $t7temporaries -
  not preserved across procedure calls
$16 - $23$s0 - $s7saved values -
  preserved across procedure calls
$24 - $25$t8 - $t9temporaries -
  not preserved across procedure calls
$26 - $27$k0 - $kreserved for use by the operating system
$28$gpglobal pointer
$29$spstack pointer
$30$s8saved value -
  preserved across procedure calls
$31$rareturn address
$f0 - $f2floating point function results
$f4 - $f10temporaries -
  not preserved across procedure calls
$f12 - $f14the first two floating point parameters -
  not preserved across procedure calls
$f16 - $18temporaries -
  not preserved across procedure calls
$f20 - $f30saved values -
  preserved across procedure calls

Input and Output in SPIM

I/O is done in SPIM using the syscall opcode. For each I/O operation, a specific value is placed in the $2 register (also referred to as $v0). Depending on the I/O operation to be performed, other registers may be involved. The following table lists the possible syscall services.
ServiceSystem Call Code
placed in $2/$v0
Arguments Results
print_int1$a0 = integer
print_float2$f12 = float
print_double3$f12 = double
print_string4$a0 = address of string
read_int5integer (in $v0)
read_float6float (in $f0)
read_double7double (in $f0)
read_string8$a0 = address of string buffer
$a1 = length of string buffer
sbrk9$a0 = amountaddress (in $v0)
exit10
Spim Input/Output Example Program

The following special characters are used with character strings in SPIM. The special characters follow the C language convention:
CharacterEncoding Sequence
Newline\n
Tab\t
Double quote\"