问题描述
我正在用 mips 编写一个程序,它接受一个字符串并将其中的一个特定字符替换为另一个。 起初,我没有检查输入字符串是否合法(但我检查了输入字符是否合法)并且工作正常。然后我尝试对字符串进行合法性检查,但结果是我存储在 $a1(我用来存储字符串的寄存器)中的内容将被后续输入字符覆盖。然而,我什至没有在后面的代码中使用 $a1 所以我现在真的很困惑。
这是我的代码:
.data
inputa: .asciiz"Input string in register A (press enter to finish): "
inputb: .asciiz"Input character in register B (press enter to finish): "
inputc: .asciiz"Input character in register C (press enter to finish): "
outputa: .asciiz"Output string in register A: "
addr: .space 0x0000000
.text
.globl main
main:
input_a: la $a0,inputa
li $v0,4
syscall #Prompt "Input string in register A (press enter to finish): "
la $a0,addr #Load another address to $a0 so that the following code won't change the content in inputa
li $v0,8
syscall #Read the input
move $s0,$a0 #Make a copy of the string and store it in $s0
li $t1,-1 #Use $t1 as a counter for the length of the input and initialize it as -1
count_a: addi $t1,$t1,1 #Add the counter by 1 each round
lb $t0,0($a0) #Store the first character of the current string in $t0
addi $a0,$a0,1 #Move to the next character of the string
beq $t0,$0,count_a_finish #If the first character is null (i.e. the string reaches the end),jump out of count_a
j count_a #Otherwise jump back to count_a
count_a_finish: beq $t1,1,input_a #If the input is of length 1(i.e. "\n") then the input is legal,otherwise ask the user to input once again
move $a1,$s0 #Store the string in $a1
input_b: la $a0,inputb
li $v0,4
syscall #Prompt "Input character in register B (press enter to finish): "
la $a0,addr #Load another address to $a0 so that the following code won't change the content in inputb
li $v0,8
syscall #Read the input
lb $s0,0($a0) #Make a copy of the first character of the input and store it in $s0
li $t1,-1 #Use $t1 as a counter for the length of the input and initialize it as -1
count_b: addi $t1,count_b_finish #If the first character is null (i.e. the string reaches the end),jump out of count_b
j count_b #Otherwise jump back to count_b
count_b_finish: bne $t1,2,input_b #If the input is of length 2(i.e. a character and '\n') then the input is legal,otherwise ask the user to input once again
move $a2,$s0 #Store the character to be modified in $a2
input_c: la $a0,inputc
li $v0,8
syscall #Read the input
lb $s0,-1 #Use $t1 as a counter for the length of the input and initialize it as -1
count_c: addi $t1,count_c_finish #If the character is null (i.e. the input reaches the end) jump out of count_c
j count_c
count_c_finish: bne $t1,input_c #If the input is of length 2(i.e. a character and '\n') then the input is legal,otherwise ask the user to input once again
move $a3,$s0 #Store the character to be modified in $a3
la $a0,outputa
li $v0,4
syscall #Prompt "Output string in register A: "
jal replace #Call "replace"
print: addi $sp,$sp,-8 #Space for two words
lb $a0,0($sp) #Load the stored character to $a0
li $v0,11
syscall #Print the character
beq $a0,exit #If the character is '\0',goto "exit"
j print #Jump back to "print" unconditionally
exit: li $v0,10
syscall #Terminate the program
replace: lb $t0,0($a1) #Load the first character in $a1 to $t0
beq $t0,replace_return #If the character is '\0',goto "exit"
addi $sp,-8 #Space for two words
sw $ra,4($sp) #Store return address
beq $t0,$a2,use_new #If the character is equal to the character to be modified,goto "use_new"
sb $t0,0($sp) #Store the original character
j next_char #Jump to "next_char" unconditionally
use_new: sb $a3,0($sp) #Store the new character
next_char: addi $a1,$a1,1 #Store the address of the next character
jal replace #Call "replace"
replace_return: lw $ra,4($sp) #Load return address
addi $sp,8 #Restore $sp
jr $ra #Back to caller
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)