2013-10-14 34 views
0

我想做一個循環,將用戶輸入的整數添加到數組,直到它填充數組。每次我鍵入一個值時,QTSPIM吐出268501016,我假設它是存儲在寄存器中的一些隨機值。For循環MIPS使用。空間

爲了測試我的程序是否經歷了整個循環,當程序到達我的beq的分支部分時,我添加了一個對ascii行的調用。即使價值觀不是(至少在我的理解中)是相等的,該計劃似乎也是分支的。

.data 
array1: .space 24 
str1: .ascii "Type in numbers:" 
str2: .ascii "Reached Terminate" 

.text 

main: 

li $t2, 5 
li $t3, 0 

loop1: 
beq $t3, $t2, terminate #branch if equal 
la $a0, str1 
syscall 

ori $v0, $0, 5 #instruction to store user input in v0 
syscall  #get user input and store it in v0 

la $t4, array1 #load the address of the array 
addu $t0, $0, $v0 #add v0 (our user input) to $t0 
sw 0($t4), t0 #stores the value in $t4 to our array 
addi $t3, $t3, 1 #add 1 to t3 (incrementing the counter) 
addi $t4, $t4, 4 $add 4 to increment the array 4 bits to the next array slot 
jal loop1 

terminate: 

la $a2, str2 #load the string to check when the program reaches terminate 
syscall 
ori $v0, $0, 10 # end the program 
syscall 

我能想到的唯一的事情是,我跳的呼叫是不會回LOOP1,但如果是這樣的話,我不知道如何解決。

這是32位MIPS代碼。

回答

0

您在syscalls之前沒有正確設置寄存器。

這裏應該有一個li $v0, 4syscall

la $a0, str1 
syscall 

如果我們假設你想在這裏打str2,應該有syscall之前li $v0, 4$a0應使用而不是$a2

la $a2, str2 #load the string to check when the program reaches terminate 
syscall 

這應該是sw $t0, 0($t4),而不是周圍的其他方式:

sw 0($t4), t0 

這應該是j,不jaljal用於函數調用):

jal loop1