Write a MIPS program to add two matrices and store the result in memory starting
ID: 3824020 • Letter: W
Question
Write a MIPS program to add two matrices and store the result in memory starting at address given in s4. The matrices are of the size 2 X 3 each. The two matrices are stored starting at two memory locations whose addresses are given in two registers, s2 and s3. Each number in the matrix is represented in signed 2’s complement form.
Deliverables:
The Code with detailed comments
Flowchart of the
Short description of the approach and the program flow
Simulation input(s) and results
Part 2: For two matrices that are stored at starting addresses given by s0 and s1 respectively, with M1 of size 2 X 3 and M2 is of size 3 X 2; perform the multiplication of M1 X M2 and store the result in memory starting at address given in s5. Each number in the matrix is represented in signed 2’s complement form.
Deliverables:
The Code with detailed comments
Flowchart of the
Short description of the approach and the program flow
Simulation input(s) and results
skeleton.s file
Explanation / Answer
Solution :
# `MAT` means
# 1, 2, 3
# 4, 5, 6
# 7, 8, 9
# `mat` means
# 9, 8, 7
# 6, 5, 4
# 3, 2, 1
.data
MAT: .word 1, 2, 3, 4, 5, 6, 7, 8, 9
mat: .word 9, 8, 7, 6, 5, 4, 3, 2, 1
size: .word 3
endl: .asciiz " "
tab: .asciiz " "
.text
main:
la $s0, MAT
la $s1, mat
lw $s2, size
jal MAT-sum
li $v0, 10
syscall
## Sums two matrixes $s0 and $s1, with size $s3, while printing resulting matrix.
# $s0, $s1 Address of the matrix's first word
# $s2 Matrixes' size
MAT-sum:
add $t0, $zero, $zero # i = 0
la $t2, 0($a0)
MAT-sum-while1:
add $t7, $zero, $zero
slt $t7, $t0, $s2
beq $t7, $zero, mat_sum_end
add $t1, $zero, $zero # j = 0
MAT-sum-while2:
add $t7, $zero, $zero
slt $t7, $t1, $s2
beq $t7, $zero, mat_sum_end_line
mul $t7, $t0, $s2
add $t7, $t7, $t1 # c = i * n + j
sll $t7, $t7, 2 # address format
add $t6, $t7, $t2 # d = `matrix-1`[c]
add $t5, $t7, $s1 # e = `matrix-2`[c]
add $s0, $t5, $t6 # print (d + e)
li $v0, 1
syscall
addi $t1, $t1, 1 # j++
li $v0, 4 # printf(" ")
la $s0, tab
addi $t1, $t1, 1
mat_sum_end_line:
addi $t0, $t0, 1 # i++
li $v0, 4 # printf(" ")
la $s0, endl
syscall
j MAT-sum-while1
mat_sum_end:
li $v0, 4 # printf(" ")
la $s0, endl
syscall
jr $ra
// ** Thank You ** //
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.