我和我的同学正在研究MIPS中操作字符串的赋值,我们想知道是否有可能改变字符串中的字符而不单独遍历每个字节?也就是说,我们可以直接转到字符串中的索引吗?这似乎不可能,但我不知道mips那么好,所以也许是。
#assume I have a function that returns the length of the string with a newline
#the purpose of this is to find the newline and take it out
li $s0,0 # Set index to 0
subi $s2,$v0,1 # set loop limit to length - 1(where the new line character is)
loop2:
#iterate through string until we reach the newline character
beq $s0,$s2, done
la $a0, 1($a0) # moves byte address of input along with loop counter to allow for correct placement of null terminator
add $s0,$s0,1 # loop counter
j loop2
done:
#returns to first call of _readString
sb $zero, ($a0) #replaces newline with null terminator
jr $ra