0
concat:
    lb $t0, 0($a0)                   # $t0 = string1[i]
    beq $t0, $0, string2             # if end of string1, go to string2
    sb $t0, 0($a2)                   # stringconcat[i] = string1[i]
    addi $a0, $a0, 1                 # increment index into string1
    addi $a2, $a2, 1                 # increment index into stringconcat
    j concat                         # loop back

string2:
    lb $t0, 0($a1)                   # $t0 = string2[j]
    beq $t0, $0, done                # if end of string2, return
    sb $t0, 0($a2)                   # stringconcat[j] = string2[j]
    addi $a1, $a1, 1                 # increment index into string2
    addi $a2, $a2, 1                 # increment index into stringconcat

done:
    sb $0, 0($a2)                    # append null to end of string
    jr $ra

I'm new to MIPS and I have this code as an assignment and I don't know what to add to make it run on QtSpim please help.

Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45

1 Answers1

0

You're missing a jump back to string2 at the end of your second loop.

string2:
  lb $t0, 0($a1)
  beq $t0, $0, done
  sb $t0, 0($a2)
  addi $a1, $a1, 1
  addi $a2, $a2, 1
  j string2                        # Missing line!

For the sake of completeness, bellow is a fully working program based on your code. It concatenates strings: "Bull" and "Dozer" into: "BullDozer":

.data
  first: .asciiz "Bull"
  second: .asciiz "Dozer"
  .align 2
  result: .space 10       #10 bytes for result: strlen("Bull") + strlen("Dozer") + 1
  .align 2

.globl main
.text
  main:
    la $a0, first
    la $a1, second
    la $a2, result

  concat:
    lb $t0, 0($a0)          # $t0 = string1[i]
    beq $t0, $0, string2    # if end of string1, go to string2
    sb $t0, 0($a2)          # stringconcat[i] = string1[i]
    addi $a0, $a0, 1        # increment index into string1
    addi $a2, $a2, 1        # increment index into stringconcat
    j concat                # loop back

  string2:
    lb $t0, 0($a1)          # $t0 = string2[j]
    beq $t0, $0, done       # if end of string2, return
    sb $t0, 0($a2)          # stringconcat[j] = string2[j]
    addi $a1, $a1, 1        # increment index into string2
    addi $a2, $a2, 1        # increment index into stringconcat
    j string2               # **Missing line!**

  done:
    sb $0, 0($a2)           # append null to end of string
    jr $ra
Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45