I wrote 3x3 matrix multiplier in MIPS instructions. When I run my instructions o
ID: 651330 • Letter: I
Question
I wrote 3x3 matrix multiplier in MIPS instructions. When I run my instructions on SPIM, I get "000," which is not the correct value. Why am I getting this? Can anyone help me fix this problem? .data A: .word 8, 1, 4, 6, -1, -2, 3, -2, 5 B: .word 8, 3, 10, 3, 1, 2, 10, 8, 6 C: .space 36 A_row: .word 3 A_col: .word 3 B_row: .word 3 B_col: .word 3 .text main: la $s0, A la $s1, B la $s2, C lw $t0, A_row lw $t1, A_col lw $t2, B_row lw $t3, B_col li $s3, -1 #i li $s4, -1 #j li $s5, -1 #k j LOOP1 LOOP1: addi $s3, $s3, 1 # i++ bge $s3, $t0, END j LOOP2 LOOP2: addi $s4, $s4, 1 # j++ bge $s4, $t1, LOOP1 j LOOP3 LOOP3: addi $s5, $s5, 1 # k++ bge $s5, $t2, Show_C mul $s6, $t1, $s3 # s6 = A_col * i add $s6, $s6, $s5 # s6 = s6 + k mul $s6, $s6, 4 # s6 = s6 * 4 add $s6, $s6, $s0 # s6 = s6 + A lw $t4, 0($s6) # load s6 to t4 mul $s7, $t3, $s5 # s7 = B_col * k add $s7, $s7, $s4 # s7 = s7 + j mul $s7, $s7, 4 # s7 = s7 * 4 add $s7, $s7, $s1 # s7 = s7 + B lw $t5, 0($s7) # load s7 to t5 mul $s8, $t3, $s3 # s8 = B_col * i add $s8, $s8, $s4 # s8 = s8 + j mul $s8, $s8, 4 # s8 = s8 * 4 add $s8, $s8, $s2 # s8 = s8 + C mul $t4, $t4, $t5 # t4 = t4 * t5 add $t6, $t4, $t6 # t6 = t4 + t6 sw $t6, 0($s8) # store s8 to t6 j LOOP3 Show_C: li $v0, 1 lw $s0, 0($s8) syscall j LOOP2 END: li $v0, 10 lw $s0, 0($s8) syscall
Explanation / Answer
My understanding is that a 3x3 matrix multiplied to another 3x3 matrix results in a 3x3 matrix. So the result should be nine digits. I cannot find where the inner loops are reset to -1 at the beginning of each loop. Instead of: j LOOP2, how about li $s4, -1 and li $s5, -1 for j LOOP3.