【发布时间】:2021-06-13 00:54:01
【问题描述】:
我一直在尝试将一个字符串复制到另一个字符串,但找不到方法,有人可以帮忙吗? 这是问题: 编写一个程序,使用过程调用将字符串 S 复制到 T。假设字符串为空终止。
这是有人在堆栈溢出中制作的代码,正如我的导师告诉我的那样,它很好,但只需要修改为过程调用
.data
str1: .asciiz "My Name is Suliman." # Store initial string
str2: .space 128 # Allocate memory space for the new string
.text
main:
la $s0, str1 # Load address of first character
la $s1, str2 # Load the address of second string
loop:
lbu $t2, 0($s0) # Load the first byte of $s0 (str1) into $t2
sb $t2, 0($s1) # Save the value in $t2 at the same byte in $s1 (str2)
addi $s0, $s0, 1 # Increment both memory locations by 1 byte
addi $s1, $s1, 1
bne $t2, $zero, loop # Check if at the zero delimiter character, if so jump to
j done
done:
li $v0, 4
la $a0, str2
syscall # Print the copied string to the console
li $v0, 10 # Program end syscall
syscall
【问题讨论】:
-
搜索 MIPS strcpy 你会发现很多例子。 C 风格的字符串被逐字节复制,直到 nul 字符终止符被复制。您需要为目标保留足够的空间:尝试
.space 80或类似的(您只为.asciiz ""保留1 个字节)。
标签: assembly mips strcpy mars-simulator mips64