2014-02-25 50 views
1

有點背景故事: 這是一個應用程序,設計用於獲取字符串,併爲每個字符添加一個鍵(1-26)到ascii值並將其放回到字符串中。唯一的問題是我的結束字符總是被操縱,即使我的程序設計爲終止空字符(beqz)。MIPS故障檢測空終止字符

encrypt: 
# store string address in $t0 
la $t0, ($a0) 
#store key in $t1 
move $t1, $a1 
# initialize index, $t2 to 0 
add $t2, $zero, $zero 
li $t4, 26 

encrypt_loop: 
# load the byte at index in $t3 
lb $t3, ($t0) 

# check if it's the end of the string 
beqz $t3, encrypt_end 
# also check if it's a space 
beq $t3, 32, incr 

# subtract to make a = 0 etc 
addi $t3, $t3, -97 
# add key 
add $t3, $t3, $t1 
# modulo to make sure that it isn't over 26 
div $t3, $t4 
mfhi $t3 
# add 97 back to get it back to its position 
addi $t3, $t3, 97 

# store byte back where you found it 
sb $t3, ($t0) 

#la $a0, ($t3) 
#jal _put_char 

incr: 
# increment address 
la $t0, 1($t0) 

#jump back to beginning of the loop 
j encrypt_loop 

示例 - >

輸入消息:超開心

輸入鍵:

加密消息:xzujw mfuud]

燦任何人都會發現這個原因代碼會操縱最後一個字符並將其更改爲結束括號?謝謝。

+1

你怎麼指定一個字符串?它適用於我,所以我懷疑你使用的字符串不是null終止。 –

回答

0

原來,字符串正確終止,但我沒有看到回車在輸入到控制檯的字符串的末尾。一旦我解釋了這一點,我就能擺脫這個神祕的角色。