如何在MIPS中连接字符串?

如何在MIPS中连接字符串?,mips,Mips,如何在MIPS中连接字符串?我想我得知道绳子的长度了?又快又脏: # 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

如何在MIPS中连接字符串?我想我得知道绳子的长度了?

又快又脏:

# 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
如果你不明白什么,尽管问


玩得开心:)

那,或者你可能有一个字符串终止符,比如C中的
\0
。我想如果我使用
.asciiz
,它将使用
\0
终止字符串,然后我检查
beq$t0,$0,Exit
,其中
$t0
是从字符串加载的字节,我想是这样的。我已经有一段时间没有看到任何MIPS程序集了。从算法上讲,您可以跨过目标字符串(如果您直接连接到目标字符串),直到找到
\0
,然后从源字符串的该位置开始添加字节,直到在源字符串中找到
\0
,再添加该字节,就完成了。