0

我需要它来做作业。用户输入他想要擦除的字符的字符串和索引。我应该返回没有这个字符且没有空格的字符串。我尝试过的最后一件事是将字符从一个字符串一个接一个地复制到另一个字符串,然后跳过我要删除的字符。示例:收到字符串“asdfghjkl”,我想删除 char No3,所以我应该打印字符串“asfghjkl”,但它看起来像“ssfggggggggggggggggggggggggggggggggggggggg.......”。我尝试了不同的组合并使用了计数器,尝试了 .space 128 而不是 n 等,但没有成功。代码在 QtSpim 中运行 谢谢

MIPS 代码是:

.data
# $s0   str1
# $s7   str2

str1: .space n
str2: .space n
msg1:   .asciiz "Input your string\n\n"
msg2:   .asciiz "\nNumber of character to erase "
err:    .asciiz "error in number"
result: .asciiz "\nthe new string is\n\n"

.text

main:   
    la $a0, str1
add $s0, $a0, $0
la $a0, str2
add $s7, $a0, $0
la $a0, msg1    #Input your string
li $v0, 4
syscall

li $v0, 8   #Read input string to $s0
syscall
move $s0, $a0

la $a0, msg2    #Input your string
li $v0, 4
syscall 

li $v0, 5   #Read Number of character to erase to $s1
syscall
add $s1, $v0, $0    #Number of character to erase $s1==
addi $t0, $s1,-1    #Number of character to erase $t0
addi $t1, $0, 127   #$t1=127 counter

findchar:

ret:
lbu $t5, 0($s0) #copy chars from string1 to string1
sb $t5, 0($s7)

addi $t1, $t1, -1   #counter of whole string1
addi $t0, $t0, -1   #counter of the character we want to erise

addi $s0, $s0, 1    #move to next char
addi $s7, $s0, 1    #move to next char
beqz $t0, shift     #if we at char we want to erise, 
                            #we skip it in string1 and continue
beqz $t1, print     #if string is finished, print result
j findchar



print:  addiu $s7, $s7, -127    #return to first char of new string
la $a0, 0($s7)      #and print it
li $v0, 4
syscall



end:    li $v0, 10
syscall
shift:  addi $s0, $s0, 1    #skip erised char in string1
addi $t1, $t1, -1

j ret           #continue copy chars from string1 to string2

&&&&&&&&&&&&&&&&&&&

谢谢你。得到答案后,我更改了代码,但从 $a1 获得的“长度”值为 0x7ffff550。这是我的主要问题 - 我没有成功获得正确的长度。

.data
# $s0   str1
# $s7   str2

    str1: .space n
    str2: .space n
    msg1:   .asciiz "Input your string\n\n"
    msg2:   .asciiz "\nNumber of character to erase "
    err:    .asciiz "error in number"
    result: .asciiz "\nthe new string is\n\n"
.text

main:
    la $a0, str1
    add $s0, $a0, $0
    la $a0, str2
    add $s7, $a0, $0
    la $a0, msg1    #Input your string
    li $v0, 4
    syscall

    li $v0, 8   #Read input string to $s0
    syscall
    move $s0, $a0
    move $s6, $a1

    la $a0, msg2    #Input your Number of character to erase
    li $v0, 4
    syscall 
    li $v0, 5   #Read Number of character to erase to $s1
    syscall
    add $s1, $v0, $0    #Number of character to erase $s1==

    addi $t0, $s1,-1    #Number of character to erase $t0
    add $t1, $s6, $0    #$t1= counter of length

findchar:

ret:
    lb $t5, 0($s0)  #copy chars from string1 to string1
    sb $t5, 0($s7)

    addi $t1, $t1, -1   #counter of whole string1
    addi $t0, $t0, -1   #counter of the character we want to erise

    addi $s0, $s0, 1    #move to next char
    addi $s7, $s0, 1    #move to next char
    beqz $t0, shift     #if we at char we want erise, we skip it in string 1 and continue
    beqz $t1, print     #if string is finished, print result
j findchar

print:  
    la $a0, str2
    #sub $s7, $s7, $s6  #return to first char of new string
    #addi $t1, $t1, -1
    #sub $s7, $s7, $t6
    la $a0, 0($s7)      #and print reult string
    li $v0, 4
    syscall


end:    li $v0, 10
    syscall
shift:  addi $s0, $s0, 1    #skip erised char in string1
    addi $t1, $t1, -1

    j ret           #continue copy chars from string1 to string2
4

1 回答 1

0

我发现您的代码存在一些问题。

  • 首先是您似乎没有在复制的字符串中添加任何 NULL 终止符,这可能会导致您在尝试打印字符串时打印出大量垃圾。

  • 第二个是您总是循环 127 次,但输入字符串不太可能是 127 个字符长。当您到达输入字符串的末尾(NULL 终止符)时,您应该停止循环。

  • 第三是如果用户想要删除第一个字符,您的实现将失败,因为您的跳过检查是在复制字符后执行的。

然后还有不必要的复杂做事方式的问题。我在代码中看到了一些地方,您可以根据一些较早的值计算常量值,而不是仅仅再次加载常量。例如,addiu $s7, $s7, -127/la $a0, 0($s7)可以被一个简单的. 代替la $a0, str2。当你的代码中断时,这类东西只会让你头疼,因为它依赖于不再有效的东西,因为你在代码的其他地方更改了一些东西。

于 2013-04-01T09:29:41.783 回答