0

How would I add on a letter into a string. For instance, I have:

str: .asciiz "abcdefghijklmnopqrstuvwxyz"

I want to add the letter a to the end of the string to make it display "abcdefghijklmnopqrstuvwxyza" when I print out the string.

I've tried

la $t0, str
lb $t1, 0($t0)
add $t0, $t0, 25
sb $t2, 1($t0)    # assume that $t2 contains the character a

Any help is greatly appreciated!

1 Answers1

0

You can't just grow a string without having space for it to grow into. In this case you'd be overwriting the NULL terminator with an 'a', which can cause all sorts of problems later on.

You could allocate some extra space following the string:

str: .asciiz "abcdefghijklmnopqrstuvwxyz"
extra_space: .space 32

And then adding an 'a' at the end could be done like so:

la  $t0,str+26
li  $t1,'a'
sb  $t1,($t0)     # append an 'a' to the string
sb  $zero,1($t0)  # add a NULL terminator after the 'a'
Michael
  • 57,169
  • 9
  • 80
  • 125