Матрицы в MIPS

Я пытаюсь написать код MIPS, который просит пользователя ввести 9 целых чисел для двух разных матриц 3x3. На выходе должно быть произведение матриц. Я новичок в MIPS, поэтому любая помощь в том, где я могу ошибиться с кодом, была бы чрезвычайно полезной!

.data
promptA: .asciiz "\n\nEnter a number for Matrix A: "
promptB: .asciiz "\n\nEnter a number for Matrix B: "
promptC: .asciiz "\n\nThis is Matrix C: "
space:   .asciiz " "
newline: .asciiz "\n"
matrixA: .word 1, 2, 3
matrixB: .word 4, 5, 6
matrixC: .word 7, 8, 9

.text
main:

li $t0, 0                               # initialize counter to keep track
li $t1, 8                               # loading 9 to keep track

la $t2, matrixA                         # load base address of MatrixA into $t2
la $t3, matrixB                         # load base address of MatrixB into $t3

# reading in matrixA
read_int:
bgt $t0, $t1, to_Multiply               # if counter is greater than 8, stop reading values into arrayA

li $v0, 4                               # system call code for printing a string
la $a0, promptA                         # loading address of string into $a0
syscall

li $v0, 5                               # code for reading an integer
syscall                                 # reads integer into $v0
move $t5, $v0                           #integer gets moved to $t5
sw $t5, 0($t2)                          # saves integer into MatrixA[$t2]

# reading in matrixB
li $v0, 4                               #system call code for printing a string
la $a0, promptB                         #loading address of string into $a0
syscall

li $v0, 5                               # code for reading an integer
syscall                                 # reads integer into $v0
move $t6, $v0                           #integers gets moved to $t6
sw $t6, 0($t3)                          # saves integer into MatrixB[$t3]


addi $t2, $t2, 4                        # incrementing address of MatrixA
addi $t3, $t3, 4                        # incrementing address of MatrixB
addi $t0, $t0, 1                        # incrementing looper counter
j read_int

to_Multiply:
addi $t0, $zero, 8
addi $t2, $zero, -32
addi $t3, $zero, -32
j Multiply

Multiply:
mulu $s1, $t2, $t3
addi $t2, $t2, 4
addi $t3, $t3, 12
mulu $s2, $t2, $t3
addi $t2, $t2, 4
addi $t3, $t3, 12
mulu $s3, $t2, $t3
addu $s1, $s1, $s2
addu $t4, $s1, $s3
beq $t4, $t0, to_RowB           #If MatrixC is at the 3rd element jump to_RowB
beq $t4, $t0, to_RowC           #If MatrixC is at the 6th element jump to_RowC
beq $t4, $t0, print_MatrixC             #If MatrixC is at the 9th element jump print_row1
j to_RowA

to_RowA:
addi $t2, $t2, -8                       #Go back to first element in MatrixA
addi $t3, $t3, -20                      #Go to first element in second column of MatrixB
addi $t4, $t4, 4                        #Increment to next element in MatrixC
j Multiply

to_RowB:
addi $t2, $t2, 4                        #Increment to next element in MatrixA
addi $t3, $t3, -32                      #Increment to next element in MatrixB
addi $t4, $t4, 4                        #Increment to next element in MatrixC
j Multiply

to_RowC:
addi $t2, $t2, 4                        #Increment to next element in MatrixA
addi $t3, $t3, -20                      #Increment to next element in MatrixB
addi $t4, $t4, 4                        #Increment to next element in MatrixC
j Multiply


#Print MatrixC
print_MatrixC:
li $v0, 4                               #system call code for printing a string
la $a0, promptC                         #loading address of string into $a0
syscall

la $a0, matrixC                         #Load address of MatrixC into $a0
li $t0, 0                               #Initialize counter to 1st element of MatrixC
li $t1, 2                               #Initialize to 3rd element of MatrixC  to keep track of counter
j print_row1


print_row1:
beq $t0, $t1, to_row2                   #If counter equals 2 jump to_row2
lw $a0, 0($t4)                          #a0 = c1-c3
li $v0, 1                               #print integer c1-c3
syscall

li $v0, 4                               #system call to print string
la $a0, space                           #print a space between each integer
syscall

addi $t4, $t4, 4                        #Increment address of MatrixC to next element
addi $t0, $t0, 1                        #Increment counter
j print_row1


to_row2:
li $t0, 3                               #Initialize counter to 3rd element
li $t1, 5                               #Initialize counter to 5th element to keep track
addi $t4, $t4, 4                        #Increment address of MatrixC to next element

li $v0, 4                               #System call to print string
la $a0, newline                         #Go to next line
syscall
j print_row2

print_row2:
beq $t0, $t1, to_row3                   #If counter equals 5 jump to_row3
lw $a0, 0($t4)                          #a0 = c4-c6
li $v0, 1                               #print integer c4-c6
syscall

li $v0, 4                               #system call to print string
la $a0, space                           #print a space between each integer
syscall

addi $t4, $t4, 4                        #Increment address of MatrixC
addi $t0, $t0, 1                        #Increment counter
j print_row2


to_row3:
li $t0, 6                               #Initialize counter to 7th element of MatrixC
li $t1, 8                               #Initialize to 9th element of MatrixC to keep track of counter
addi $t4, $t4, 4                        #Increment address of MatrixC

li $v0, 4                               #System call to print string
la $a0, newline                         #Go to next line
syscall
j print_row3

print_row3:
beq $t0, $t1, exit                      #If counter equals the 9th element of MatrixC go to exit
lw $a0, 0($t4)                          #a0 = c7-c9
li $v0, 1                               #print integer c7-c9
syscall

li $v0, 4                               #system call to print string
la $a0, space                   #print a space between each integer
syscall

addi $t4, $t4, 4                        #Increment address of MatrixC
addi $t0, $t0, 1                        #Increment counter
j print_row3


exit:
li $v0,10
syscall

Пока что код позволяет пользователю вводить 9 целых чисел для обеих матриц, но не дает никаких выходных данных.


person kourtney b    schedule 30.07.2013    source источник
comment
Не могли бы вы объяснить, почему t0, t2, t3 равны 8, -32 и -32, когда вы впервые достигаете Multiply:? И почему три условных перехода с одинаковыми условиями следуют друг за другом (beq $t4,$t0)? Я также не могу найти нигде в вашем коде, где вы фактически умножаете элементы двух матриц.   -  person Michael    schedule 31.07.2013
comment
Я добавляю 8 к t0, потому что процедура умножения применяется к первой строке матрицы C, которая является адресным элементом 0, 4, 8. Как только матрица C достигает третьего элемента с адресом 8, она переходит к следующей строке матрицы C. В первой строке матрицы C элементы матрицы A с адресами 0, 4 и 8 умножаются на первый столбец матрицы B с адресами 0, 12 и 24. Например, C1=A1xB1 + A2xB4 + A3xB7.   -  person kourtney b    schedule 31.07.2013
comment
Итак, я установил t0=8, чтобы отслеживать, какой элемент matrixC включен. Затем мне пришлось установить t2 и t3 обратно в первые элементы матрицы A и матрицы B после того, как матрицы были заполнены пользователем. Я предполагаю, что после заполнения матриц адреса матрицы A и матрицы B находятся в последнем элементе (A [9] и B [9]).   -  person kourtney b    schedule 31.07.2013
comment
как только t4=8=t0 первая строка готова, она переходит к корректировке адресов всех матриц для работы со второй строкой: C4 = A4xB1 + A5xB4 + A6xB7. Мне пришлось настроить адреса матриц, чтобы следующая строка могла быть завершена. Умножение происходит в процедуре умножения.   -  person kourtney b    schedule 31.07.2013
comment
Затем мне пришлось установить t2 и t3 обратно в первые элементы матрицы A и матрицы B. Однако вы не вычитаете 32 из $t2 и $t3; вы даете им значение -32 ($zero + -32). И я до сих пор не вижу, где вы умножаете элементы A и B. Для меня это больше похоже на то, что вы умножаете адреса/смещения элементов. И снова у вас есть эти три beq инструкции, которые не имеют никакого смысла, так как последние две из них никогда не приведут к прыжку.   -  person Michael    schedule 31.07.2013
comment
Мне нужно было бы изменить это, чтобы добавить $t2, $t2, -32, чтобы снова сделать t2 адресом первого элемента, и затем сделать то же самое для t3, верно? Кроме того, мне нужно загрузить элементы адреса в начале процедуры умножения, чтобы умножить фактический элемент по адресам вместо умножения адресов? Что касается инструкций beq, я пытаюсь сказать, что когда t4(MatrixC) находится на 3-м элементе (t4=адрес 8=t0), то он должен перейти к следующей строке, а когда t4=адрес 20 перейти к следующему ряду и т. д. и т. д.   -  person kourtney b    schedule 31.07.2013
comment
Я понимаю, насколько я ошибаюсь с этим кодом. Я путаюсь с адресами регистров и фактическим содержимым регистров. Я делаю шаг назад и смотрю на программу с совершенно другой точки зрения. Спасибо за помощь, Михаил!   -  person kourtney b    schedule 31.07.2013
comment
Проверьте этот вопрос: stackoverflow.com/questions/4881233/ 2d-массив в mips/   -  person Node.JS    schedule 17.08.2013