2012-10-14 82 views
0

因此,我輸入兩個數組並打印其中一個(現在),但是當我打印其中一個數組時,它會打印一個數值和一個其他陣列。我不知道爲什麼會發生這種情況。請幫忙。這裏是我的代碼,然後輸出示例:MIPS:數組打印不正確

.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

還有用,使得它無法運行該代碼一對夫婦的問題:

  • 沒有分配給兩個矩陣的空間 - 使用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