CSE140 Computer Architecture (Fall semester 2007): project 1
This projects tests your understanding of assembly language programming with the MIPS instruction set architecture (ch. 2-3 in the textbook). You may do in in groups of up to 2 students. You will:
- Write a well-commented C program project1.c that:
- Reads an integer n from the terminal.
- Reads n more integers from the terminal and saves them in an array a in memory.
- Computes the two functions process_array1 and process_array2, saving the results in a1 and a2 in memory.
- Prints a1, a2 to the terminal.
- Terminates.
- Translate it into a well-commented MIPS assembly language. You may need to use several system calls (for terminal I/O, memory allocation, etc.); see appendix A of the textbook.
- Debug it with SPIM to ensure it runs correctly.
- Compare your MIPS program with the one obtained by the MIPS C cross-compiler: mips-gcc -mips32 -mrnames -S project1.c. Try the different levels of optimisation -O0 (no optimisation, the default), -O1, -O2, -O3, -Os. See the optimisation options and the MIPS options in the gcc manual. See also section 2.11 of the textbook.
Your program must contain at least the following functions:
- *int readints(int n), which reads n integers (positive or negative) from the terminal, stores them in an array in memory and returns its address.
- *float process_array1(int *a), which creates an array a1 of floats (single precision) where a1[i] = sqrt(a[i]/(a[i]-a[n-1-i])).
- float sqrt(float a), which computes (to the highest precision) the square root. The MIPS ISA does have a sqrt.s instruction, but your sqrt function may not use it. However, for debugging purposes, you should compare the result of sqrt.s with your sqrt function's. To implement it, use Newton's method (an iterative method with second-order convergence); you need to provide a starting value for the iteration and a stopping criterion.
- int process_array2(int *a), which returns an integer a2 equal to the sum a[0]^5 + ... + a[n-1]^5.
- void print_int_array(int *a), void print_float_array(float *a), which print to the terminal a list of numbers (if overflow or infinity they should print a suitable message).
Give these constraints, you have some freedom as to how to implement the program (in a reasonable way), but you have to explain your decisions as to how you deal with:
- Terminal I/O (SPIM offers several options).
- Memory management (use a fixed-size array, or allocate memory).
- The sqrt function.
- Type conversions between integer and single precision.
- Saving registers across procedure calls.
- Overflow, NaN, infinities, exceptions.
We have several example programs in C and their translation into assembly language (by hand rather than by a compiler) that you can use to test things. Also, if you find it useful you can use the following Matlab (or Octave) code to compute test values of a1, a2:
a=[12 6 8];
a0=a./(a-a(end:-1:1)); a0(a0<0)=NaN; a1=sqrt(a0), a2=sum(a.^5)
Some examples of inputs and outputs:
- n <= 0: program should just stop
- n = 3: [12 6 8] -> a1 = [1.7321 Inf NaN], a2 = 289376
- n = 4: [12 6 8 -15] -> a1 = [0.66667 NaN 2.00000 0.74536], a2 = -469999
- n = 1: [1000] -> a1 = [Inf], a2 = overflow
- n = 1: [-1000] -> a1 = [NaN], a2 = overflow
- n = 3: [69 70 73] -> a1 = [NaN Inf 4.2720], a2 = overflow
What you have to submit is a single file project1.tar.gz, by email to the TA, containing:
- Your file project1.c, containing the C code.
- Your file project1.s, containing the MIPS assembly code, which must be well commented and run correctly with SPIM.
- 5 files project1-O0.s, project1-O1.s, project1-O2.s, project1-O3.s, project1-Os.s containing the output of the MIPS C cross-compiler ran on your project1.c with the different levels of optimisation (none, -O1, -O2, -O3, -Os).
- A file project1.pdf containing: the source code for project1.c and project1.s (for easy reference); your explanation of your decisions as to the issues above (terminal I/O, etc.) when writing the code; and an insightful comparison with the code produced by the MIPS C cross-compiler.
Miguel A. Carreira-Perpinan
Last modified: Tue Nov 27 23:48:00 PST 2007
UC Merced |
EECS |
MACP's Home Page