我正在尝试编写一个 MIPS 代码,要求用户为两个不同的 3x3 矩阵输入 9 个整数。输出应提供矩阵的乘积。我对 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 个整数,但它不提供任何输出。