GEORGE MASON UNIVERSITY
Computer Science Department

CS 365 - Computer Architecture

Spring 2002
Assignment 2

MIPS Assembly and SPIM
DUE DATE - Exercise 1: Feb 13; Exercise 2: Feb 20

    The project has two goals - (i) introduce you to the SPIM simulator for the MIPS instruction set, (ii) give you some practice writing MIPS assembly programs.

Exercise 1 - Introduction to SPIM (10 points)

This exercise involves executing a MIPS assembly program on the SPIM simulator. Before beginning this exercise you should have read Appendix A in the textbook (specifically, the part of the appendix related to SPIM). Download the program sort.s from the class home page into your home directory. Make sure you read the information on the web page for Assignment 2 regarding the project.

There are two ways to use the SPIM simulator on UNIX based systems: (1) via a command line interface, or (2) using a X windows interface. The command line interface is invoked through the command "spim" whereas the X windows interface is invoked via the command "xspim". The version of SPIM for PCs (PCSpim) is window-based. The description below pertains to XSPIM. However, it is equally applicable to PCSpim.

On your UNIX workstation, bring up XSPIM by typing:

xspim &
The XSPIM X-Window Interface should come up (XSPIM is SPIM with a jazzy interface). Refer to figure A.16 in the text book. The window panes that come up are described in the appendix. Now load the assembly program sort.s using the ``load'' button. You can run the program using the ``run'' button, or step through the program using the ``step'' button. You can also set breakpoints as described in Appendix A. Test out all these features of the simulator. (Everything you need to know about the simulator is in appendix A. Spend some time playing around with the simulator before you proceed with the rest of the assignment.)

Notice the following points while using XSPIM: 

1. Note how the contents of the PC change after each step (the contents of registers also change depending on what the instruction does). When do you think the contents of memory change? (HINT: step through your program until you come to a sw instruction. Examine the contents of memory before and after executing that instruction). Also, step through the program until you come to (i)any branch instruction (ii) a jal instruction. Note how the PC (and $31) in the case of jal change after SPIM executes the instruction.


2.
Note how SPIM translates some instructions from your assembly program into several instructions. This is because these instructions are pseudo-instructions that are translated by the assembler into several instructions. What are the instruction sequences corresponding to the following pseudo-instructions (i) la (ii) li (iii) move (iv) mulou.


3.
Note how you can do output (i.e., write to screen) in an assembly program using XSPIM. Input/Output is described on Page A-48 of your text and is illustrated in the program sort.s.


4.
How often is the following instruction on line 18 executed

 	addi   $t1,$t1,-1
(HINT: You should print out a listing of your program, and examine what is happening in the program. You can come up with a count of how often each instruction is executed based on analysis, not using SPIM.)

Submit your answers to question 2 and question 4 above. 

Exercise 2 (90 points)

Write and test a MIPS assembly program that given two matices, M1 and M2, first transposes M2, and then multiplies it with the second matrix. You should implement four routines:

·main() Invoke the matrix_transpose procedure to transpose a given matrix. Then multiply the transposed matrix with another given matrix using the matrix_multiply procedure. Finally, print out the product matrix.

·Matrix_multiply(r1,c1,A,r2,c2,B,C)

oThis procedure computes the product of two matrices A and B and stores the result in the matrix C.

oMatrix A has r1 rows and c1 columns and matrix B has r2 rows and c2 columns. 

oHere A, B, and C are the memory addresses at which the matrices A, B, and C are stored. Assume that matrices are stored in memory in row-major fashion.

oNote that matrix C will have r1 rows and c2 columns

oMatrix_multiply should invoke the procedure Inner_product() while computing the product of A and B.

·Inner_product(r,num_columns1,M,c,num_rows,num_columns2,N)

oThis procedure multiplies row r of matrix M with column c of matrix N. num_columns1 is the number of columns in each row of M, whereas num_rows is the number of rows in each column of N and num_columns2is the number of columns in N 

oIf num_rows is not equal to num_columns1 this program should print an error message and exit.

oThis procedure returns an integer (the inner product) to the calling procedure.

The assignment web page contains the following additional information:

·The two matrices to be multiplied

·How to declare these matrices in your assembly program

·The result matrix. You can compare your result against this matrix to verify the correctness of your program.

·A C program matrix.c corresponding to a high-level language implementation of this program.

Test your program by running it using SPIM/XSPIM. To get full credit, you must invoke the procedures correctly within your program, i.e., you must pass arguments to the procedures matrix_multiply and inner_product, save and restore registers at the procedure entry and/or exit points, and return the results of inner_product to the matrix_multiply procedure correctly. Explain what procedure call convention you're following in your code, i.e., whether it is caller save or callee save or another convention (e.g., the MIPS convention).

Submit (1) your assembly program. Your program must be well commented (2) A brief description of the procedure call conventions you are following (3) A hardcopy output showing that your program works correctly. In Windows, you can save the results of your session using the "Save Log File" menu option. In UNIX, to capture the output of your program (assuming it's called matrix.s) type

spim -file matrix.s
You can use the UNIX command script for capturing what you see on your terminal screen in a file.

 

NOTE: spim and xspim are installed on both the Mason cluster and the machines in the IT&E lab. To obtain PCSpim follow the link from the class web page.

Preparation for Assignment 3

For this exercise you will have to obtain a count of how often each instruction in the programs matrix.s and sort.s is executed. You will have to produce a table similar to the table below for each program.

 

Instruction category 
MIPS examples 
Frequency 
Arithmetic 1
add,sub,addi
Arithmetic 2
mult, multu
Aritmetic 3
div, divu
Data Transfer
lw, sw, lui
Conditional Branch
beq, bne, slt, slti
Jump
j, jr, jal
Others
syscall, mflo, mfhi

How do you think you would come up with these counts? (HINT: There is a brute force method using SPIM and also a ``smart'' method. You are expected to use the smart method.)

NOTE:

You do not have to submit the answer to this question. This is only to give you a preview of what you will have to do for the next assignment, and to give you something to think about.

LATE SUBMISSION POLICY No late projects will be accepted unless prearranged with the instructor.