【问题标题】:how to copy string a string to another one in mips assemply language如何在mips汇编语言中将字符串复制到另一个字符串
【发布时间】: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


【解决方案1】:

我在这里也有一些 cmets 来解释我在做什么,我只是去了 128 个字节以获得一些空间,我还打印出了复制的字符串

.data

    str1: .asciiz "My Name is Suliman." # Store initial string
    str2: .space 128            # Allocate memory space for the new string

.text

main:
    la $t0, str1            # Load address of first character
    la $t1, str2            # Load the address of second string

loop:                      # do {
    lbu  $t2, 0($t0)        # Load the first byte of $t0 (str1) into $t2

    sb   $t2, 0($t1)        # Save the value in $t2 at the same byte in $t1 (str2)

    addi $t0, $t0, 1        # Increment both pointers by 1 byte
    addi $t1, $t1, 1
    bne  $t2, $zero, loop  # }while(c != 0)

#    j done        # execution falls through to the next instruction by itself
#done:
    li $v0, 4
    la $a0, str2
    syscall                 # Print the copied string to the console

    li $v0, 10              # Program end syscall
    syscall

【讨论】:

  • 非常感谢
  • 嘿,我的朋友。感谢您为制作此代码所做的努力,但不幸的是,我刚刚发现我需要使用 persecute 调用来制作它,我将使用额外的信息编辑问题 rn。如果您能帮助我,我将不胜感激!
猜你喜欢
  • 2023-03-14
  • 2020-10-07
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 2015-02-05
相关资源
最近更新 更多