MIPS故障检测空终止字符

时间:2014-02-25 01:46:51

标签: assembly mips null-terminated mars-simulator

一个小背景故事: 这是一个设计用于获取字符串的应用程序,并为每个字符添加一个键(1-26)到ascii值并将其放回到字符串中。唯一的问题是我的结束角色总是被操纵,即使我的程序被设计为终止于空字符(beqz)。

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

示例 - >

输入消息:超级快乐

输入密钥: 5

加密邮件: xzujw mfuud]

有人能发现这个代码会操纵最后一个字符并将其更改为结束括号的原因吗?感谢。

1 个答案:

答案 0 :(得分:0)

原来这个字符串正确终止,但我没看到的是在输入控制台的字符串末尾的回车符。一旦我解释了这一点,我就能摆脱这个神秘的角色。

相关问题