Question

In: Computer Science

Write a full MIPS assembly code for: Function Compare(int A[][], int B[][], int M, int N,...

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.

Solutions

Expert Solution

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:

  • Getting the data from an array cell, e.g, x = list[i];
  • Storing data into an array cell, e.g. list[i] = x;
  • Determining the length of an array, i.e. list.length.

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.


Related Solutions

Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to the follow java program: int day = (int)(Math.random() * 7); switch (day) { case 1: System.out.println(“Monday”); break case 2: System.out.println(“Tuesday”); break case 3: System.out.println(“Wednesday”); break case 4: System.out.println(“Thursday”); break case 5: System.out.println(“Friday”); break case 6: System.out.println(“Saturday”); break case 0: System.out.println(“Sunday”); break }
MIPS ASSEMBLY : 1) Code in MIPS a function that allows to store the text entered...
MIPS ASSEMBLY : 1) Code in MIPS a function that allows to store the text entered by the user in a 300-byte buffer. The function must return the actual size of the entered text in number of bytes. The text could contain lines, that is to say that the symbol '\ n' could be present. The end of reading is defined by both consecutive '\ n' '\ n' symbols. 2) Code in MIPS a function that helps identify if the...
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT