0

所以,我正在输入两个数组并打印其中一个(现在),但是当我去打印其中一个数组时,它会打印一个数组的一些值和另一个数组的一些值。我不知道为什么会这样。请帮忙。这是我的代码,然后是示例输出:

.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

示例输出:

求两个矩阵的乘积


输入方阵的行大小:2


输入第一个矩阵(从左到右和从上到下):

输入第一个矩阵的浮点数:2.3
输入第一个矩阵的浮点数:4.3
输入第一个矩阵的浮点数:6.5
输入第一个矩阵的浮点数:4.3


输入第二个矩阵(从左到右和从上到下):

输入第二个矩阵的浮点数:3.2
输入第二个矩阵的浮点数:6.5
输入第二个矩阵的浮点数:8.9
输入第二个矩阵的浮点数:3.2

一个浮点数:6.50000000
一个浮点数:8.89999962
一个浮点数:3.20000005
一个浮点数:4.30000019
4

1 回答 1

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

Endmain

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
于 2012-10-15T23:45:37.670 回答