Assembly Can';在mips中,是否不将字符串从一个目的地复制到另一个目的地?

Assembly Can';在mips中,是否不将字符串从一个目的地复制到另一个目的地?,assembly,mips,Assembly,Mips,我似乎在将用户给定的字符串从一个目的地复制到另一个目的地时遇到了一些问题。在我的代码中,我从用户那里获取字符串,计算该字符串中有多少个字符,然后尝试将其从一个地方复制到另一个地方。然而,它不起作用了,它一直给我一个惊喜 Exception occurred at PC=0x00400110 Bad address in data/stack read: 0x0000006f Exception 7 [Bad data address] occurred and ignored Mem

我似乎在将用户给定的字符串从一个目的地复制到另一个目的地时遇到了一些问题。在我的代码中,我从用户那里获取字符串,计算该字符串中有多少个字符,然后尝试将其从一个地方复制到另一个地方。然而,它不起作用了,它一直给我一个惊喜

Exception occurred at PC=0x00400110
  Bad address in data/stack read: 0x0000006f
  Exception 7  [Bad data address]  occurred and ignored
Memory address out of bounds
错误。这是我的密码:

.data

    prompt1: .asciiz "Please enter a string> "
    prompt2: .asciiz "The length of the string "
    prompt3: .asciiz "is: "
    string: .space 255
    newstring: .space 255
    nl: .asciiz "\n"

.text

main:

    li $v0, 4
    la $a0, prompt1
    syscall #print the first prompt

    la $a0, string
    li $a1, 255
    li $v0, 8
    syscall #get the string and put it in "array"

    la $a0, string
    li $a1, 255
    addi $s0, $s0, 0
    jal strlen #jump to strlen

    move $t0, $v0 #move whatever strlen got and put in t0
    li $v0, 4
    la $a0, prompt2
    syscall #print prompt2

    li $v0, 4
    la $a0, string
    syscall #print array

    li $v0, 4
    la $a0, prompt3
    syscall #print prompt3

    li $v0, 1
 move $a0, $t0
    syscall #print number of characters

    li $v0, 4
    la $a0, nl
    syscall #new line!

    li $v0, 4
    la $a0, nl
    syscall #againnnn!

    la $a0, newstring
    la $a1, string
    move $a2, $t0
    jal strncpy


    move $a0, $v0
    li $v0, 4
    syscall

j exit


strlen:
    lb   $s1, 0($a0)     #load next byte
    beqz $s1, firfin     #if byte is '\0', go to firfin
    addi $s0, $s0, 1     #increments character count
    addi $a0, $a0, 1     #move on to next byte
    j    strlen          #jump back and keep going

firfin:
    addi $v0, $s0, -1 #return number of chars - 1 to account for \n
    li $s0, 0 #clean up
    li $s1, 0 #sweepy sweepy
    jr   $ra

strncpy:
    lb   $s1, 0($a1)      #load next byte
    bge  $s0, $a2, secfin #if bigger than string length, jump to secfin
    sb   $a0, 0($s1)
    addi $s0, $s0, 1
    addi $a1, $a1, 1
    j strncpy
secfin:
    move $v0, $a0
    li   $s2, 0
    li   $s1, 0
    li   $s0, 0
    jr   $ra

exit:
    li $v0, 10
    syscall

strncpy
中存在多个问题:

  • 在开始使用之前,您不需要将$s0设置为零
  • 在测试字符串结尾之前加载一个字节
  • 您将
    $a0
    存储到
    0($s1)
    中,而不是将
    $s1
    存储到
    0($a0)
  • 您不需要增加
    $a0
  • 通过修改
    s
    寄存器,您没有遵循调用约定
  • 您已经编码了
    memcpy
    ,而不是
    strncpy

  • 学习使用调试器/模拟器逐步检查代码并查看错误所在。
    strncpy
    中的多个问题:

  • 在开始使用之前,您不需要将$s0设置为零
  • 在测试字符串结尾之前加载一个字节
  • 您将
    $a0
    存储到
    0($s1)
    中,而不是将
    $s1
    存储到
    0($a0)
  • 您不需要增加
    $a0
  • 通过修改
    s
    寄存器,您没有遵循调用约定
  • 您已经编码了
    memcpy
    ,而不是
    strncpy

  • 学习使用调试器/模拟器逐步检查代码并查看错误所在。

    第3和第4项真的解决了我的问题,非常感谢!^^数字3和4真的解决了我的问题,非常感谢^