如何在MIPS中连接字符串?

时间:2011-10-23 11:33:54

标签: mips

如何在MIPS中连接字符串?我想我会以某种方式知道字符串的长度?

1 个答案:

答案 0 :(得分:1)

又快又脏:

# String concatenate

.text

# Copy first string to result buffer
la $a0, str1
la $a1, result
jal strcopier
nop

# Concatenate second string on result buffer
la $a0, str2
or $a1, $v0, $zero
jal strcopier
nop
j finish
nop

# String copier function
strcopier:
or $t0, $a0, $zero # Source
or $t1, $a1, $zero # Destination

loop:
lb $t2, 0($t0)
beq $t2, $zero, end
addiu $t0, $t0, 1
sb $t2, 0($t1)
addiu $t1, $t1, 1
b loop
nop

end:
or $v0, $t1, $zero # Return last position on result buffer
jr $ra
nop

finish:
j finish
nop

.data
str1:
.asciiz "Hello "
str2:
.asciiz "world"
result:
.space 200

如果您不明白,请不要犹豫。

玩得开心:)