Question
MIPS PROGRAM MIPS - IT MUST BE SOLVED IN MIPS ASSEMBLY LANGUAGE For this program, you will write a matrix multiply routine capable of multiplying
MIPS PROGRAM
MIPS - IT MUST BE SOLVED IN MIPS ASSEMBLY LANGUAGE
For this program, you will write a matrix multiply routine capable of multiplying non-square matrices. The size of matrices are expressed as row-column. So the matrix:
a11 a12 a13
a21 a22 a23
Is a 2 x 3 matrix. If matrix a is as above and we are multiplying A * B = C, then the B matrix must be a 3 x 2 matrix. The result is always a square matrix with the number of rows and columns equal to the number of rows in the first matrix.
The product of two matrices is based on the dot products of the rows of the first matrix with the columns of the second matrix. In the example A * B = C, C11 = (a11*b11) + (a12*b21) + (a13*b31) or A row 1 B col 1 or Ar1Bc1. Thus
a11 a12 a13 b11 b12 Ar1Bc1 Ar1Bc2
a21 a22 a23 * b21 b22 = Ar2Bc1 Ar2Bc2
b31 b32
Realize that the data in the program shows six elements in each array. The elements are stored in row-major order in memory. This is the case with most memory layouts. In order to interpret the six linear elements, we must know the rows and columns. For this reason, the number or rows and columns of the first matrix are passed to the matrix multiply routine. The dimensions of the second matrix can be inferred from the dimensions of first matrix.
*****************DONT CHANGE THIS BOILERPOINT CODE JUST ADD AFTER "YOUR CODE HERE" AT THE BOTTOM OF CODE**************************************************
THIS IS THE OUTPUT:
The answer matrices are:
334 904 727 3966 2636
2642 2600 3944 and 6677 3937
3010 4664 5169
Here is the boilerplate code **********DO NOT CHANGE THIS CODE***************
.data
Size32:
.word 3, 2
Size23:
.word 2, 3
Matrix1:
.word 1, 20, 68, 22, 55, 76
Matrix2:
.word 34, 24, 47, 15, 44, 34
Matrix3:
.space 36
Answer3x3:
.word 334, 904, 727, 2642, 2600, 3944, 3010, 4664, 5169
Answer2x2:
.word 3966, 2636, 6677, 3937
LabelA:
.asciiz "Matrix A: "
LabelB:
.asciiz "Matrix B: "
LabelC:
.asciiz "Matrix C: "
Separator:
.asciiz "\t"
crlf:
.asciiz " "
.text
la $a0, Matrix1
la $a1, Size32
la $a2, Matrix2
la $a3, Matrix3
jal MatrixMult
la $a0, LabelA
addi $v0, $0, 4
syscall
la $a0, Matrix1
addi $a1, $0, 3
addi $a2, $0, 2
jal MatrixPrint
la $a0, LabelB
addi $v0, $0, 4
syscall
la $a0, Matrix2
addi $a1, $0, 2
addi $a2, $0, 3
jal MatrixPrint
la $a0, LabelC
addi $v0, $0, 4
syscall
la $a0, Matrix3
addi $a1, $0, 3
addi $a2, $0, 3
jal MatrixPrint
la $a0, Matrix1
la $a1, Size23
la $a2, Matrix2
la $a3, Matrix3
jal MatrixMult
la $a0, LabelA
addi $v0, $0, 4
syscall
la $a0, Matrix1
addi $a1, $0, 2
addi $a2, $0, 3
jal MatrixPrint
la $a0, LabelB
addi $v0, $0, 4
syscall
la $a0, Matrix2
addi $a1, $0, 3
addi $a2, $0, 2
jal MatrixPrint
la $a0, LabelC
addi $v0, $0, 4
syscall
la $a0, Matrix3
addi $a1, $0, 2
addi $a2, $0, 2
jal MatrixPrint
addi $v0, $0, 10
syscall
MatrixPrint:
# $a0 - Matrix
# $a1 - Rows
# $a2 - Columns
# $t0 - Manipulable address of Matrix
# $t1 - Row count
# $t2 - Column count
# $t3 - save address for $a0 (changed in syscalls)
addi $sp, $sp, -4
sw $ra, 0($sp)
addi $t0, $a0, 0
addi $t1, $0, 0
addi $t2, $0, 0
addi $t3, $a0, 0
PrintLoop:
lw $a0, 0($t0)
jal PrintValue
addi $t0, $t0, 4
addi $t2, $t2, 1
bne $t2, $a2, PrintLoop
addi $t2, $0, 0
addi $t1, $t1, 1
la $a0, crlf
addi $v0, $0, 4
syscall
bne $t1, $a1, PrintLoop
la $a0, crlf
addi $v0, $0, 4
syscall
addi $a0, $t3, 0
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
PrintValue:
# $a0 - value
addi $v0, $0, 1
syscall
la $a0, Separator
addi $v0, $0, 4
syscall
jr $ra
MatrixMult:
# $a0 - Matrix 1
# $a1 - Size of Matrix 1
# $a2 - Matrix 2
# $a3 - Matrix 3
# your code goes here
******************************THIS IS WHERE THE CODE IS ENTERED AND THE LOOPS ARE CREATED***************************
The arguments supplied to MatrixMult are the addresses of the three matrices plus the size of the first. The size of a matrix is expressed as two numbers, so the data in $a1 is the address of a two-word array which has the number of rows in the first word and the number of columns in the second.
The answer matrices are:
334 904 727 3966 2636
2642 2600 3944 and 6677 3937
3010 4664 5169
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started