0

For instance, I have a program with this string:

str: .asciiz "abcdefghijklmnopqrstuvwxyz"

Then I put another letter at the end of the string by doing:

la $t0, str
sb $t1, 26($t0) # the letter a  is stored into $t1

This makes the editted str to be:

str: .asciiz "abcdefghijklmnopqrstuvwxyza"

My question is how do I increment that number 26, to 27 because I have a loop that keeps accepting user inputted characters and adding it to the end of the string, but I keep replacing the last letter of the string with the new letter. I want to increment the 26 by 1 every time it loops once so that the string keeps lengthening, not being replaced by other letters. The only thing I can think of is

sb $t1, $t2($t0)
add $t2, $t2, 1

which doesn't work.

1 Answers1

0

This makes the editted str to be:

str: .asciiz "abcdefghijklmnopqrstuvwxyza"

That's not entirely true (see my answer for your earlier question). .asciiz implies a NULL-terminated string, but you just overwrote the NULL terminator with an 'a'.


add $t2, $t2, 1

which doesn't work.

The add instruction adds registers together. Use addi or addiu if you want to add an immediate value to a register. For example:

addiu $t2,$t2,1  # add 1 to $t2, ignoring potential overflows
Community
  • 1
  • 1
Michael
  • 57,169
  • 9
  • 80
  • 125