2013-03-05 69 views
0

我的代碼存在問題。我正在創建一個程序,它將讀取一個字符串,將其保存到一個數組中,然後輸出每個字母在字符串中使用的次數。目前,我只是將字符串的輸出返回到屏幕上。字符串被輸出,但循環永不退出,$ t2值可能永遠不會設置爲一個值?以循環播放的Mips彙編中的數組

.data 
intro: .asciiz "Andrew Lofgren, Letter Checker Program" 
question: .asciiz "\nPlease enter a string for evaluation: " 
alphabet: .ascii "ABCDEFGHIJKLMONOPQRSTUVWXYZ" 
results: .space 104 
string: .space 1024 

.text 

main: 
jal setup 
jal analyze 
#jal results 

li $v0, 10 
syscall 

setup: 
li $v0, 4 # outputing name and program information 
la $a0, intro 
syscall 

li $v0, 4 # asksing for string input 
la $a0, question 
syscall 

li $v0, 8 
la $a0, string 
li $a1, 1024 
syscall 

jr $ra  # return 

analyze: 
la $t0, string # taking string and saving into a tmp 
move $t2, $t0 # backup of orignal address 
find: 
beq $t1, 0, print 
addi $t0, $t0, 1 
j find 

print: 
blt $t0, $t2, end #PROBLEM HERE 
li $v0, 11 
lb $a0, 0($t0) 
syscall 
addi $t0, $t0, 1 
j print 
end: 
jr $ra 
+0

你的退出條件是t0 Michael 2013-03-05 06:37:04

回答

1

$t0絕不會小於$t2,因爲$t0不斷增加,而$t2保持不變。爲了解決這個問題,你需要第三個寄存器,比如$t7,它存儲你的字符串的最終索引。要計算最後一個索引,只需將字符串的基地址添加到您定義爲1024的長度。一旦$t0不再小於1024 +字符串,字符串中就沒有字符,因此我們分支到end:。這是在下面的代碼段中完成並進一步解釋的。

print: 
    li  $t7 , 1024   # Load total byte length of the string 
    add  $t7 , $t7 , $t2  # add it to the base address to get the end 
    slt  $t6 , $t0 , $t7  # check to see if cur index < end 
    beq  $t6 , $0 , end  # if we hit the end, branch to end 

... 

有關MIPS指令的更多信息,請參閱visit this page