Question: Implement this code to multiply two matricies without using the multiplication instruction : .text .globl main main: #Loading the Block Size and Matrix Size into
Implement this code to multiply two matricies without using the multiplication instruction :
.text
.globl main
main:
#Loading the Block Size and Matrix Size into the Registers
la $s0, bs
lw $s0, 0($s0)
la $s1, n
lw $s1, 0($s1)
la $s2, Matrix_A
la $s3, Matrix_B
la $s4, Matrix_C
# Load the addresses of Matrix_A, Matrix_B, and Matrix_C into registers
la $a0, msga
la $a1, Matrix_A
jal PRINT_MAT
la $a0, msgb
la $a1, Matrix_B
jal PRINT_MAT
#The Algorithm for Matrix Multiplication
la $a0,Matrix_A
la $a1,Matrix_B
la $a2,Matrix_C
lw $t8, n
li $s0,0
#Creating Outer_Loop and loading the value 0 into the register $s1.
#$s1 will be used to iterate over the columns of matrix B.
Outer_Loop:
li $s1,0
Inner_Loop:
li $s2,0
mul $t0,$t8,4
mul $t0,$t0,$s0 # i
mul $t1,$s1,4 # j
add $t1, $t1, $t0
add $t3, $a2, $t1
sw $0, ($t3)
Dot_Product_Loop:
# Loading in a[i][k]
# Multiplies the value in register $t8 by 4 and stores the result in $t0.
mul $t0,$t8,4
# Multiplies the value in $t0 by the current value of $s0
# Representing the current row of matrix A.
mul $t0,$t0,$s0 # i
mul $t1,$s2,4 # k
add $t1, $t1, $t0
add $t4, $a0, $t1
# Loading in b[k][j]
mul $t0,$t8,4
# loading in k
mul $t0,$t0,$s2
# loading in j
mul $t1,$s1,4
add $t1, $t1, $t0
add $t5, $a1, $t1
lw $t4, ($t4)
lw $t5, ($t5)
add $t4, $t4, $t5
# Loading in c[i][j]
mul $t0,$t8,4
# loaing in i
mul $t0,$t0,$s0
# loading in j
mul $t1,$s1,4
add $t1, $t1, $t0
add $t3, $a2, $t1
lw $t5, ($t3)
add $t5, $t5, $t4
# storeing the final result
# c[i][j] += a[i][k] + b[k][j]
sw $t5, ($t3)
addi $s2, $s2, 1
blt $s2,$t8,Dot_Product_Loop
addi $s1, $s1, 1
blt $s1,$t8,Inner_Loop
addi $s0, $s0, 1
blt $s0,$t8,Outer_Loop
la $a0, msgc
la $a1, Matrix_C
jal PRINT_MAT
li $v0,10
syscall
#end of the code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
