在MIPS中迭代并修改字符串

时间:2015-02-18 02:12:35

标签: string loops assembly mips

我试图编写一个方法来对MIPS汇编语言中的一串文本进行凯撒转换。我的加密方法如下:

encryptMessage:
    la $s0, message     #s0 will hold message that will be iterated through
    lw $t1, key     #s1 will hold the key to shift by
    li $t0, 0       #t0 will be iterator, starting at 0

encryptionLoop:
    add $s1, $s0, $t0   #$s1 = message[i]
    lb $s2, 0($s1)      #Loading char to shift into $s2
    beq $s2, $zero, exit    #Breaking the loop if we've reached the end: http://stackoverflow.com/questions/12739463/how-to-iterate-a-string-in-mips-assembly
    add $s2, $s2, $t1   #Shifting the character by the key amount
    la $s1, ($s2)       #Changing the character in message to the shifted character
    addi $t0, $t0, 1    #i++
    j encryptionLoop    #Going back to the beginning of the loop

但是,在我打印出所谓加密消息的退出方法中,它只打印出最初输入的消息。我的代码不是"记住"我改变了角色,但我无法弄清楚如何让它记住。我怀疑这一行

la $s1, ($s2)       #Changing the character in message to the shifted character

与它有关,但我不知道如何解决它。

1 个答案:

答案 0 :(得分:1)

想出来。我怀疑的这条线应该是

sb $s2 ($s1)