所以,我正在输入两个数组并打印其中一个(现在),但是当我去打印其中一个数组时,它会输出一个值和另一个数组的值。我不知道为什么会这样。请帮忙。这是我的代码,后跟一个示例输出:
.data
title: .asciiz "Find The Product of Two Matrices\n\n"
menuString: .asciiz "Menu\n\n"
option1: .asciiz "1. Enter values and find product\n"
option2: .asciiz "2. Exit program\n\n"
inputString: .asciiz "Enter selection: "
promptSize: .asciiz "\nEnter row size of square matrix: "
promptRow: .asciiz "Row: "
promptCol: .asciiz "Col: "
promptMatrix1Float: .asciiz "Enter a first matrix's float: "
promptMatrix2Float: .asciiz "Enter a second matrix's float: "
answer: .asciiz " Answer:"
printFloat: .asciiz "\nA Float: "
inputFirstMatrix: .asciiz "\n\nInput First Matrix(left-to-right and top-to-bottom):\n\n"
inputSecondMatrix: .asciiz "\n\nInput Second Matrix(left-to-right and top-to-bottom):\n\n"
.globl main
.text
################################### INPUT SIZE AND MATRICES ####################
size: li $v0, 4
la $a0, promptSize
syscall
li, $v0, 5
syscall
add $t1, $v0, $v0
li $t0, 0
li $v0, 4
la $a0, inputFirstMatrix
syscall
L1: beq $t0, $t1, L2IndexInit
li $v0, 4
la $a0, promptMatrix1Float
syscall
li $v0, 6 #6 is the syscall code for get float
syscall
sll $t3, $t0, 3
add $t3, $a2, $t3
s.d $f0, 0($t3) # Store $f0 at memory location .
addi $t0, $t0, 1
j L1
L2IndexInit:
li $v0, 4
la $a0, inputSecondMatrix
syscall
li $t0, 0
L2: beq $t0, $t1, PrintIndexInit
li $v0, 4
la $a0, promptMatrix2Float
syscall
li $v0, 6 #6 is the syscall code for get float
syscall
sll $t3, $t0, 3
add $t3, $a1, $t3
s.d $f0, 0($t3) # Store $f0 at memory location .
addi $t0, $t0, 1
j L2
############################## PRINT MATRIX #################################################
PrintIndexInit:
li $t0, 0
PrintMatrix: beq $t0, $t1, End
sll $t4, $t0, 3
add $t4, $a2, $t4
l.d $f12, 0($t4)
li $v0, 4
la $a0, printFloat
syscall
li $v0, 2 #2 is the syscall code for print float (arg. to print in f12)
syscall
addi $t0, $t0, 1
j PrintMatrix
####################################### MAIN FUNCTION AND END ######################
End:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
main:
li $v0, 4
la $a0, title
syscall
addi $sp, $sp, -4
sw $ra, 0($sp)
jal size
li $v0, 10
syscall
示例输出:
Find The Product of Two Matrices Enter row size of square matrix: 2 Input First Matrix(left-to-right and top-to-bottom): Enter a first matrix's float: 2.3 Enter a first matrix's float: 4.3 Enter a first matrix's float: 6.5 Enter a first matrix's float: 4.3 Input Second Matrix(left-to-right and top-to-bottom): Enter a second matrix's float: 3.2 Enter a second matrix's float: 6.5 Enter a second matrix's float: 8.9 Enter a second matrix's float: 3.2 A Float: 6.50000000 A Float: 8.89999962 A Float: 3.20000005 A Float: 4.30000019
答案 0 :(得分:0)
代码存在一些问题导致它无法运行:
syscall 9
在堆上分配空间End
中,您从堆栈中弹出返回地址,但您需要的地址已在$ra
修复这些以使其运行后,代码工作正常。这是我在size
中分配内存的方式:
size:
li $v0, 4
la $a0, promptSize
syscall
li $v0, 5
syscall
add $t1, $v0, $v0
sll $a0, $t1, 3
li $v0, 9
syscall
move $a2, $v0
li $v0, 9
syscall
move $a1, $v0
li $t0, 0
li $v0, 4
la $a0, inputFirstMatrix
syscall
End
和main
:
End:
jr $ra
main:
li $v0, 4
la $a0, title
syscall
addi $sp, $sp, -4
sw $ra, 0($sp)
jal size
lw $ra, 0($sp)
addi $sp, $sp, 4
li $v0, 10
syscall
输出:
Find The Product of Two Matrices
Enter row size of square matrix: 2
Input First Matrix(left-to-right and top-to-bottom):
Enter a first matrix's float: 1.2
Enter a first matrix's float: 3.4
Enter a first matrix's float: 5.6
Enter a first matrix's float: 7.8
Input Second Matrix(left-to-right and top-to-bottom):
Enter a second matrix's float: 2.1
Enter a second matrix's float: 4.3
Enter a second matrix's float: 6.5
Enter a second matrix's float: 8.7
A Float: 1.2
A Float: 3.4
A Float: 5.6
A Float: 7.8