Mips程序没有运行

时间:2014-06-19 10:50:01

标签: string mips spim

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

我是Mips的新手,我将此代码作为一项任务,我不知道要添加什么来让它在Qtspim上运行,请帮忙。

1 个答案:

答案 0 :(得分:0)

您错过了在第二个循环结束时返回到 string2 的跳转。

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!

为了完整起见,下面是一个基于您的代码的完整工作程序。它将字符串:"Bull""Dozer" 连接成:"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