In: Computer Science
Write a full MIPS assembly code for: Function Compare(int A[][], int B[][], int M, int N, int i, int j, int k, int l). This function compares the elements of the array A placed between (i,j) and (k,l) with the corresponding elements of the array B placed between the same indexes. The function returns the number of elements of both matrices that coincide in the same position (c,d). That is, it is necessary to check each element A[c,d] with the element B[c,d], with (c,d) inside the corresponding indexes to (i,j) and (k,l), both included. The function accepts 8 arguments:
A: starting address of an integer matrix of MxN dimension.
B: starting address of an integer matrix of dimension MxN.
M: number of rows of the A and B matrix.
N: number of columns of the A and B matrix.
i: an integer corresponding to the row of the first element of the matrix to be considered.
j: a whole number corresponding to the column of the first element of the matrix to be considered. o
k: a whole number corresponding to the row of the last element of the matrix to be considered.
l: a whole number corresponding to the column of the last element of the matrix to be considered.
The function returns two values. In case of error, it simply returns a result with value -1. The possible errors are: o M or N are negative or zero. o The indexes passed as arguments (i,j,k, and l) are out of range. o The element (k,l) is previous in the matrix to the element (i,j). It will not be considered an error if (i,j) and (k,l) have the same value and refer to the same element. If no error is detected, the function returns as a first result the value 0 and as second the number of elements that coincide in the same position in the arrays A and B.
Since arrays can store lots of data, and since we have only a small (~32) number of registers, it is infeasible to use the registers for long-term storage of the array data. Hence, arrays are stored in the Data Segment of a MIPS program. Fundamentally, there are three operations which one can perform on an array:
Function Compare(int A[][], int B[][], int M, int N, int i, int j, int k, int l). This function compares the elements of the array A placed between (i,j) and (k,l) with the corresponding elements of the array B placed between the same indexes. The function returns the number of elements of both matrices that coincide in the same position (c,d). That is, it is necessary to check each element A[c,d] with the element B[c,d], with (c,d) inside the corresponding indexes to (i,j) and (k,l), both included.
Array Expression and Inter-Statement Optimizations:
.data
space: .asciiz " "
newline: .asciiz "\n"
prompt_row: .asciiz "\nInput row #: "
result_row: .asciiz "\nRow #: "
matrix_A: .word
.space 500
matrix_B: .word
.space 500
matrix_C: .word
.space 5000
.text
main:
# t9 holds the col/row size for all of the matrices
li $t9, 2 # matriz size
# setup matrix_A
la $a0, matrix_A # starting address of an integer matrix of MxN
dimension of A
jal matrix_input # build matrix A
# setup matrix_B
la $a0, matrix_B # starting address of an integer matrix of MxN
dimension of B
jal matrix_input # build matrix B
li $t0,M*N # $t0 = number of rows
li $t1,M*N # $t1 = number of columns
lw $t4, 0($t0) #integer corresponding to the row of the first element of the matrix to be considered.
lw $t4, 1($t1) # a whole number corresponding to the column of the first element of the matrix to be considered.
lw $t5, 0($t0) #integer corresponding to the row of the last element of the matrix to be considered.
lw $t5, 1($t1) # a whole number corresponding to the column of the last element of the matrix to be considered.