mips汇编程序 - 从字符串中按索引删除字符

时间:2013-04-01 02:55:53

标签: string assembly mips

我的作业需要它。用户输入他想要删除的字符串和char索引。 我应该返回没有这个char而没有空格的字符串。我尝试过的最后一件事就是将字符从一个字符串逐个复制到另一个字符串并跳过我要删除的字符。示例:收到字符串“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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

然后还有不必要的复杂做事方式的问题。我在代码中看到了一些基于某些早期值计算常量值的位置,而不是再次加载常量。例如,addiu $s7, $s7, -127 / la $a0, 0($s7)可能已被简单的la $a0, str2取代。当你的代码中断时,这种东西会让你头疼,因为它依赖于某些不再有效的东西,因为你在代码中的其他地方改变了某些东西。