0

我有一个大学练习,我必须读取 3 个数组 (3x3) 的值并与它们相加和相乘。D=A+B*C。我已经编写了这段代码,对于屏幕上的结果,我得到 000000000。这是下面的代码。ps我是新手。

n:      .word
        .align 4
array1:         .space 36
        .text
        .globl main
main:
        li       $t2, 1 
        li       $t3, 1
        li       $t4, 1
        li       $t6, 0
        addi     $sp, $sp, -144
    and      $t0, $0, $0       # i = 0
    and      $t1, $0, $0
    and      $t5, $0, $0


loopA:
    addi    $t0, $t0, 1          # add 1 to t0


li      $v0, 4
la      $a0, amatrix        #print a(
syscall   

li      $v0, 1
move    $a0, $t2
syscall                    


li      $v0, 4
la      $a0, amatrix2       #print , for the a( , )
syscall

li      $v0, 1
move    $a0, $t0
syscall                     #print out actual sum

li      $v0, 4
la      $a0, amatrix3
syscall  

li $v0, 5                   # Syscall to read an integer.
syscall                     # Result returned in $v0.
move $t9, $v0               # n stored in $t9.
beq      $t0, 1, saverowA
beq      $t0, 2, saverowA
beq      $t0, 3, saverowA

loopB:
    addi    $t1, $t1, 1          # add 1 to t0


li      $v0, 4
la      $a0, bmatrix        #print b(
syscall   

li      $v0, 1
move    $a0, $t3
syscall             


li      $v0, 4
la      $a0, bmatrix2       #print , for the b( , )
syscall

li      $v0, 1
move    $a0, $t1
syscall                     #print out actual sum

li      $v0, 4
la      $a0, bmatrix3
syscall  

li $v0, 5                   # Syscall to read an integer.
syscall                     # Result returned in $v0.
move $t8, $v0               # n stored in $t8.

beq      $t1, 1, saverowB
beq      $t1, 2, saverowB
beq      $t1, 3, saverowB

loopC:
    addi    $t5, $t5, 1          # add 1 to t5


li      $v0, 4
la      $a0, cmatrix        #print a(
syscall   

li      $v0, 1
move    $a0, $t4
syscall                     


li      $v0, 4
la      $a0, cmatrix2       #print , for the a( , )
syscall

li      $v0, 1
move    $a0, $t5
syscall                     #print out actual sum

li      $v0, 4
la      $a0, cmatrix3
syscall  

li $v0, 5                   # Syscall to read an integer.
syscall                     # Result returned in $v0.
move $t7, $v0               # n stored in $t7.

beq      $t5, 1, saverowC
beq      $t5, 2, saverowC
beq      $t5, 3, saverowC

checkA: 
    bne      $t0, 3, loopA       # until t0 reached 3 then go to loopA.
    addi     $t2, $t2, 1
    li   $t0, 0
    bne  $t2, 4, loopA
    beq      $t2, 4, loopB

checkB:      
    bne      $t1, 3, loopB       # until t0 reached 3 then go to loop.
    addi     $t3, $t3, 1
    li   $t1, 0
    bne      $t3, 4, loopB
    beq      $t3, 4, loopC

checkC:
    bne  $t5, 3, loopC
    addi     $t4, $t4, 1
    li   $t5, 0
    bne      $t4 ,4, loopC
    beq  $t4, 4, calc

saverowA: 
         sw $t9, 0($sp)  
             addi $sp, $sp, 4
         j checkA

saverowB:    
             sw $t8, 0($sp)
         addi $sp, $sp, 4
         j checkB

saverowC:    
             sw $t7, 0($sp)
         addi $sp, $sp, 4
         j checkC

calc:       
             beq $t6, 9, reset
         addi $sp, $sp, -108
         lw $t0, 36($sp)   # b(1,1)
         lw $t1, 72($sp)   # c(1,1)
         lw $t2, 0($sp)  # a(1,1)
         mul $t3, $t0, $t1
         add $t4, $t2, $t3
         sw $t4 , 108($sp)
         addi $sp, $sp, 4
         addi $t6, $t6, 1
         j calc

reset:
    li $t6, 0
        j print



print:  
         addi $t6, $t6 ,1
     addi $sp, $sp, -36
     lw  $a0, 0($sp)       # load sum to print
         li  $v0, 1              # print sum
         syscall
     addi $sp, $sp, 4
     bne $t6, 9, print



  exit:
    li      $v0, 10              # terminate program run and
    syscall                      # Exit 


                .data
result:         .asciiz "Sum = "
amatrix:        .asciiz "a("
amatrix2:       .asciiz ","     
amatrix3:       .asciiz ")="
bmatrix:        .asciiz "b("
bmatrix2:       .asciiz ","
bmatrix3:       .asciiz ")="
cmatrix:        .asciiz "c("
cmatrix2:       .asciiz ","
cmatrix3:       .asciiz ")="
dmatrix:        .asciiz "d("
dmatrix2:       .asciiz ","
dmatrix3:       .asciiz ")="
4

1 回答 1

1

您的打印代码不正确:每次迭代都会递减堆栈指针。它应该是:

print:
addi $sp, $sp, -36
print2: 
addi $t6, $t6 ,1

bne $t6, 9, print2

同样的情况发生在 calc 中:

calc:
addi $sp, $sp, -108
calc2: 
beq $t6, 9, reset

j calc2
于 2013-05-28T20:48:59.550 回答