Question: Explain this MIPS code PLEASE. I dont understand how it is used. Please explain as much as possible and the purpose of each register. Its
Explain this MIPS code PLEASE. I dont understand how it is used. Please explain as much as possible and the purpose of each register.
Its supposed to find the matrix multiplication of two matrices.
.data matrixA: .word 1,3,2,1,3,2,1,3,2 matrixB: .word 0,1,2,0,1,2,0,1,2 output: .word 0:9 #9 elements all initialized to 0 msg: .asciiz "The output matrix elements are : " comma: .asciiz ", "
.text
#multiply the 2 matrices using the logic #for(int i = 0 ; i < 3; i ++) # for( int j = 0; j < 3; j++) # { # sum = 0 # for(int k = 0; k < 3; k++) # { # sum += A[i][k] * B[k][j]; # } # output[i][j] = sum # }
li $t0, 0 #row number, i
OuterLoop:
bge $t0, 3, EndOuterLoop #if row >= 3 li $t1, 0 #col number , j
InnerLoop1:
bge $t1, 3, EndInnerLoop1 #if col >= 3 li $t2, 0 # k li $t6, 0 #sum
InnerLoop2:
bge $t2, 3, EndInnerLoop2 #get the element A[i][k] by calculating offset i * 3 + k, store t4 = A[i][k] mul $t3, $t0, 3 add $t3, $t3, $t2 mul $t3, $t3, 4 #multiply by 4 since each int takes 4 bytes lw $t4, matrixA($t3)
#get the element B[k][j] by calculating offset k * 3 + j, store t5 = B[k][j] mul $t3, $t2, 3 add $t3, $t3, $t1 mul $t3, $t3, 4 #multiply by 4 since each int takes 4 bytes lw $t5, matrixB($t3)
#t4 = A[i][k] * B[k][j] #sum = sum + t4
mul $t4, $t4, $t5 add $t6, $t6, $t4 addi $t2, $t2, 1
b InnerLoop2
EndInnerLoop2:
#calculate offset for output element and store ans output[i][j] = sum mul $t3, $t0, 3 add $t3, $t3, $t1 mul $t3, $t3, 4 #multiply by 4 since each int takes 4 bytes sw $t6, output($t3) #store sum into output
#incremetn col addi $t1, $t1, 1 b InnerLoop1
EndInnerLoop1: #increment row addi $t0, $t0,1 b OuterLoop
EndOuterLoop:
#display the output matrix la $t0, output #base address of output li $t1, 1 #counter
DisplayLoop: bgt $t1, 9, Exit #displayed 9 elements? #display the element from output matrix li $v0, 1 lw $a0, 0($t0) syscall #show a comma li $v0, 4 la $a0, comma syscall #increment counter addi $t1, $t1, 1 add $t0, $t0, 4 b DisplayLoop Exit: #exit li $v0, 10 syscall
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
