MIPS Trouble Detecting Null Terminating Character
-
20-12-2019 - |
Question
A little backstory: This is an application designed to take a string and for each character, add a key (1-26) to the ascii value and place it back into the string. The only problem is that my end character is always manipulated as well, even when I have my program designed to terminate on null characters (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
Example ->
Enter a message: super happy
Enter a key: 5
Encrypted message: xzujw mfuud]
Can anyone spot a reason that this code would manipulate that last character and change it into an ending bracket? Thanks.
Solution
Turns out that the string was terminating correctly, but what I did not see was the carriage return at the end of a string that is entered into the console. Once I accounted for that, I was able to get rid of the mysterious character.