我需要输入一个用户输入的字符,并使用递归将它插入正确字母位置的字符串中,然后请求另一个字符。一次只能输入一个字符。使用递归插入字符到字符串? (MIPS)
我认为一个好方法是扫描字符串,当输入的字符匹配字母串中的字符时,然后插入它。但是我很难将其转化为代码。我也必须使用递归(堆栈,$ jal,$ jr $ ra),但我并不完全熟悉如何使用它。
这是到目前为止我的代码:
.data
str: .asciiz "abcdefghijklmnopqrstuvwxyz .space 30"
input: .asciiz "Please input a letter: "
error1: .asciiz "Error! Please only enter one lowercase letter!"
print1: .asciiz "The string is: "
.text
main:
la $s0,0
li $v0,4
la $a0,input
syscall
loop:
li $v0,8
syscall
move $t0,$v0
blt $t0,61,error #if less than a
bgt $t0,80,error #if more than z
bge $t0,0x50,print #when user enters P, print
loop2:
lb $t2,str($s0) #load first byte of string
bge $t0,$t2,store #if input char=string char, store it
addi $s0,$s0,1 #next char in string
j loop
store:
sw $t0,str($s0) #store char in string
addi $s0,$s0,1 #next char in string
j loop
error:
li $v0,4
la $a0,error1 #print error message
syscall
j loop #jump back to loop
print:
li $v0,4
la $a0,print1 #print dialogue
syscall
li $v0,4
la $a0,str #print string
syscall