0

I'm a begginer at MIPS and I'm having trouble with a concatenation program I have. The code I wrote is here

# _strConcat
#
# Concatenate the second string to the end of the first string
#
# Arguments:
#   - $a0: The address of the first string
#   - $a1: The address of the second string
# Return Value:
#   - None
_strConcat:
    move $t0, $a0 #string in buffer 1
    move $t1, $a1 #string in buffer3
    j _strCopy #copies buffer1 into buffer2 at address $a1
    move $t2, $a1 #saves buffer1 string to buffer2
    #add string inbuffer3 to end of string in buffer 1
    # $t0 contains destination, $t1 and $t2 contain strings to concatenate
first:
    lb $t4, ($t2)
    beqz $t0, endFirst
    sb $t4, ($t0)
    addi $t2, $t2, 1
    addi $t0, $t0, 1
    j first
endFirst:
    beqz $t0, endSecond
    addi $t1, $t1, 1
    addi $t0, $t0, 1
    j endFirst
endSecond:
    jr $ra

It will only print out the first string instead of the second or the concatenated string; My train of though was since a0 contains the first string at buffer1 and a1 contains the second string at buffer 3 and I need to return a concatenated string back in buffer1. So I copied the string into buffer2 from buffer1 and tried putting the buffer2 and buffer3 together in buffer1. I don't necessarily have to use strCopy if it's not needed.

ELlama
  • 33
  • 7

1 Answers1

0

You wanted jal (jump-and-link), not j, when you called _strcpy. You jumped directly to _strcpy, and it then returned on your behalf to your caller, not to your function, since the return address was still unchanged. Revised code below:

jal _strCopy    # copies buffer1 into buffer2 at address $a1

I can't speak to the rest of your function, but that's definitely why you're only getting one string back.

Sean Werkema
  • 5,810
  • 2
  • 38
  • 42