Мне нужно умножить матрицу A и массив из 3 измерений X. Наконец, мне нужно добавить массив из 5 измерений B
Я думаю, что мой код ухудшается, и он не работает хорошо
main: la $s0, matrixA #s0 set to base address of matrixA
la $s1, vectorX #s1 set to base address of matrixB
la $s2, sizeA #s2 set to base address of sizeA
nop
lw $s3, 4($s2) #s3 set to second val in sizeA (col #)
nop
lw $s2, 0($s2) #s2 set to first val in sizeA (row #)
nop
li $s5, 0 #s5 set to second val in sizeB (col #)
nop
lw $s4, 4($s2) #s4 set to first val in sizeB (row #)
la $s6, result #s6 set to base adress of result
add $s7, $s5, $zero #s7 set to col # in result matrix
add $t0, $zero, $zero #Set t0 to zero. i = 0
add $t1, $zero, $zero #Set t1 to zero. j = 0
add $t2, $zero, $zero #Set t2 to zero. k = 0
li $t3, 0 #Result position set to zero
i_loop: beq $t0, $s2, i_end #End i_loop if i = rowsA
nop
j_loop: beq $t1, $s5, j_end #End j_loop if j = colsB
nop
k_loop: beq $t2, $s4, k_end #End k_loop if k = rowsB
nop
#loop body
li $t4, 0
li $t5, 0
li $t6, 0
#i * M + k - 1
mul $t4, $t0, $s3 #i * #col in matrixA
add $t4, $t4, $t2 #t4 + k
addi $t4,$t4,-1 # t4 - 1
sll $t4,$t4,2 # convert index to byte offset
addu $t4,$t4,$s0 # Now points to value at matrixA[i][k]
lw $t4,0($t4) # Loads value at matrixA[i][k]
#k * M + j - 1
mul $t5, $t2, $s5 #k * #col in matrixB
add $t5, $t5, $t1 #t5 + j
addi $t5, $t5, -4 #t5 -1
add $t5, $t5, $s1 #t5 now points to value at matrixB[k][j]
lw $t5, 0($t5) #t5 loads value at matrixB[k][j]
#i * M + j - 1
mul $t6, $t0, $s7 #i * #col in result
add $t6, $t6, $t1 #t6 + j
addi $t6, $t6, -4 #t6 -1
add $t6, $t6, $s6 #t6 now points to value at result[i][j]
lw $t8, 0($t6) #t6 loads value at result[i][j]
mul $t7, $t4, $t5 #t7 = matrixA[i][k]*matrixB[k][j]
add $t9, $t8, $t7 #t8 = result[i][j] + matrixA[i][k]*matrixB[k][j]
sw $t9, 0($t6)
#end loop body
addi $t2, $t2, 1 #k++
j k_loop #Return to start of k_loop
k_end:
addi $t1, $t1, 1 #j++
li $t2, 0 #Resets k counter to 0
j j_loop #Return to start of j_loop
j_end:
addi $t0, $t0, 1 #i++
li $t1, 0 #Resets j counter to 0
j i_loop #Return to start of i_loop
i_end: #print
# print the values of y,
# where y = max(0, matrixA * vectorX + vectorB)
li $v0, 4 # system call code for printing string = 4
la $a0, str1
syscall # call operating system to perform operation
# printing shape
li $v0, 1
la $s0, shapeA
lw $a0, 0($s0)
syscall
li $v0, 4 # system call code for printing string = 4
la $a0, space
syscall # call operating system to perform operation
li $v0, 1
lw $a0, 4($s0)
syscall
li $v0, 4 # system call code for printing string = 4
la $a0, str2
syscall # call operating system to perform operation
# printing the first two elements
li $v0, 2
la $s1, matrixA
lwc1 $f12, 0($s1)
syscall
li $v0, 4 # system call code for printing string = 4
la $a0, space
syscall # call operating system to perform operation
li $v0, 2
lwc1 $f12, 4($s1)
syscall