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