1

一点背景故事:这是一个应用程序,旨在获取一个字符串,并为每个字符添加一个键 (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]

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

4

1 回答 1

0

结果证明字符串正确终止,但我没有看到输入控制台的字符串末尾的回车符。一旦我考虑到这一点,我就能摆脱神秘人物。

于 2014-02-27T04:24:00.350 回答