2013-04-18 54 views
1

以下是我的MIPS彙編代碼由於某種原因而拒絕工作。有人請救我,我已經被困了3周!我應該讀入一個int n,將其平方,然後將n(平方)的值讀入矩陣,然後計算矩陣的軌跡。我讀了一個堆棧,但在某處,我必須有一個簡單的邏輯錯誤,我無法識別。在此先感謝:MIPS彙編程序在整數輸入後計算矩陣的軌跡

.data       #string variable declarations 
get_int: .asciiz "Please enter an int value n: " #prompts user to enter a number 
newline: .asciiz "\n" 
.text 

.globl main 

main: 

addi $sp, $sp, -12      #create a stack (stack frame..) 
sw $ra, 0($sp)      #storage for stack elements 
sw $s0, 4($sp)  
sw $s1, 8($sp)  
sw $s2, 12($sp)    

li $v0, 5      #Read input 
syscall 
move $s0, $v0      #$s0 = $v0 

mult $s0 $s0       #square the entered int 
mflo $t2 
li $t0, $t2  #t0 receives the squared value from $t2 


#adding stuff to the stack 

li $t1, 0      #t1 is our counter (i) 

stackloop: 
beq $t1, $t0, endstackloop     #exit loop when t1== $t0 


li $v0, 5      #Read input 
syscall 
move $t5, $v0      #$t5 = $v0 

addi $sp, $sp, -4 
sw $t5, 0($sp) 

addi $t1, $t1, 1     #add 1 to t1 
j stackloop       #jump back to the top of loop 

stackpop: 

lw $t3, 0($sp)  #t3 = 1st value from stack 
add $s1, $s1, $t3 #adding that 1st value to sum 
addi $sp, $sp, 4 #moving to the next stack element 
sub $t1, $t1, 1  #decrementing counter by 1 
add $t4, $t4, $0 #setting comparison value to 0 

matrixcondition: 
beq $t1, $0, output 
lw $t3, 0($sp) 
sub $t1, $t1, 1   #decrementing counter by 1 
beq $s0, $t4, matrixtrace #jump to matrixtrace when equal 
add $t4, $t4, 1   #add 1 to t4 
j matrixcondition 

matrixtrace: 
add $s1, $s1, $t3 #adding that next value to sum 
add $t4, $0, $0  #setting comparison value to 0 
j matrixcondition 

output: 
li $v0, 4 
syscall 
j progterminate 

progterminate: 

lw $ra, 0($sp)      #restoring the stack pointer 

addi $sp, $sp, 8 

jr $ra       #return 
+2

你還沒有說明是什麼問題。預期的行爲是什麼,實際的行爲是什麼? – Michael

回答

0

這是我發現的錯誤:

  1. li $t0, $t2  #t0 receives the squared value from $t2 
    

    成爲

    move $t0, $t2  #t0 receives the squared value from $t2 
    
  2. endstackloop不存在。

  3. add $t4, $t4, $0 #setting comparison value to 0 
    

    成爲

    li $t4, 0 #setting comparison value to 0 
    

如果你提供什麼是不工作的更多信息,我們可以幫助更好。