【问题标题】:MIPS Concatenating String Code not working as intendedMIPS 连接字符串代码未按预期工作
【发布时间】:2017-07-21 10:58:22
【问题描述】:

我是 MIPS 的初学者,但我在使用连接程序时遇到了问题。我写的代码在这里

# _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

它只会打印出第一个字符串而不是第二个或连接的字符串;我的训练是因为 a0 在缓冲区 1 包含第一个字符串,而 a1 在缓冲区 3 包含第二个字符串,我需要在缓冲区 1 中返回一个连接的字符串。所以我将字符串从buffer1复制到buffer2中,并尝试将buffer2和buffer3放在buffer1中。如果不需要,我不一定非得使用 strCopy。

【问题讨论】:

    标签: assembly mips strcpy


    【解决方案1】:

    当您调用 _strcpy 时,您想要的是 jal(跳转和链接),而不是 j。你直接跳转到_strcpy,然后它代表你返回给你的调用者,而不是你的函数,因为返回地址仍然没有改变。修改后的代码如下:

    jal _strCopy    # copies buffer1 into buffer2 at address $a1
    

    我无法与您的其他功能交谈,但这绝对是您只得到一个字符串的原因。

    【讨论】:

      猜你喜欢
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多