COMP1521 18s1 COMP1521 18s1 Final Exam Comp Sys Fundamentals
[Instructions] [Documentation]
[Q1] [Q2] [Q3] [Q4] [Q5] [Q6] [Q7] [Q8] [Q9] [Q10]

Question 1 (11 marks)

An identity matrix is a square ( N x N ) matrix where each element on the leading diagonal has the value one, and all other elements are zero. In this question, you must write a MIPS function that checks whether a supplied matrix is an identity matrix.

In the q1 directory are three files:

main.s

A MIPS code file that includes three functions:

main()

a main function that calls the isIdent() function, then shows the matrix and says whether or not it is an identity matrix

showMatrix(m,N)

a function that displays an NxN matrix m, with one row per line

matrix.s

A MIPS data definition file, containing MIPS data equivalent to the following:

#define N 3
int matrix[N][N] = {{1,0,0},{0,1,0},{0,0,1}};
q1.s

A skeleton definition of a function isIdent(m,N) that determines whether or not an NxN matrix m is an identity matrix.

There is also a tests directory containing data files (called m?.s), test scripts, and expected outputs.

Exercise

Your task for this question is to implement the isIdent function in MIPS assembler.

The isIdent function takes two parameters:

The function returns 1 if the matrix is an identity matrix and 0 if it is not. It should not modify the matrix in any way.

Examples:

The isIdent() function should behave like this pseudo-C version:

int isIdent(int m[N][N], int N)
{
   for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
         if ((i == j && m[i][j] != 1)
             || ((i != j && m[i][j] != 0)))
            return 0;
      }
   }
   return 1;
}

You can assume that the m matrix is defined to contain NxN integer values using one .word declaration per row.

You can execute the program on the supplied matrix using the command:

$ exe  matrix.s

Once your function is working correctly, the above command should produce the following output:

The matrix
 1 0 0
 0 1 0
 0 0 1
is an identity matrix

Note: the fact that it produces the above result even before implementing the function is a lucky coincidence.

The exe command executes the program by concatenating the matrix file, the main program file and the q1.s file into a file called exe.s, which it then invokes spim on.

There are more matrix files in the tests directory and you can test your program on these by e.g.

$ exe  tests/m2.s

You can test your q1 program with all of the data files in the tests directory using the command:

$ check q1   # run tests on the q1 program

Once you are satisfied with your program, submit it using the command:

$ submit q1

This will make a copy of the q1.s file as your answer for this question. You can run the submit command as many times as you like, but make sure that your final submission runs without any errors or warnings. Test your program thoroughly, possibly using test cases additional to those supplied. Your program will be tested using inputs which are different to the examples in the q1/tests directory.

If, at some stage, you need to "re-install" the files (although you should not need to), you can copy all of the original files into the q1 directory by running the command:

$ re-start q1

Beware: this will overwrite all of your exsting files for this question, so only do it if you seriously mess things up.