2017-03-01 48 views
0

我是MIPS的一名開始人員,並且遇到了一個串接程序問題。我寫的代碼是在這裏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中。所以我將字符串複製到緩衝區1中的緩衝區2,並嘗試將緩衝區2和緩衝區3一起放入緩衝區1中。如果不需要,我不一定非要使用strCopy。

回答

0

當您撥打_strcpy時,您想要jal(跳轉鏈接),而不是j。您直接跳到_strcpy,然後它代表您返回您的調用方,而不是您的函數,因爲返回地址保持不變。以下修改後的代碼:

jal _strCopy # copies buffer1 into buffer2 at address $a1 

,我不能給你的函數的其餘說話,但是這絕對是你爲什麼只得到一個字符串返回。