recursion - MIPS 在哪里将递归调用放在回文问题中
问题描述
我正在编写一个程序,它接受用户给定的字符串,如果是回文则输出 1,否则输出 0。它必须使用递归,但是我无法弄清楚如何格式化 jal 和 jr 语句以便递归正常工作。
main:
la $a0, askForString #simple text asking for user input
li $v0, 4
syscall
li $v0, 8
la $a0, userInput #points a0 to space allocated to get user input
li $a1, 41 #allows the user to enter input up to 40 characters
syscall
la $a0, userInput
li $v0, 4
syscall
lw $s0, userInput
j palindrome
palindrome:
la $a1, userInput
addu $a1,$a1,$s1
lbu $a0,($a1) # read the character in $s1 spot of the user inputted word
addi $sp, $sp, -4
sw $a0, 0($sp) #add the specific character to the stack so it can be looked at later
addi $s1, $s1, 1 #advance the count to look at the next character
jal palindrome
lw $t0, 0($sp) #load the last letter of the user given word from the stack
addi $sp, $sp, 4
la $a1, userInput
addu $a1,$a1,$s2
lbu $a0,($a1) # read the character in $s2 spot of the user inputted word
addi $s2, $s2, 1 #advance the count to look at the next character
bne $a0, $t0, fail #if the letters are not equal, print 0, not a palindrome
jr $ra
success:
la $a0, '1'
li $v0, 11
syscall
j end
fail:
la $a0, '0'
li $v0, 11
syscall
j end
end:
li $v0, 10 #op code to exit the program
syscall
我知道程序的回文部分几乎肯定不起作用,并且无法知道单词的结尾在哪里,但是如果有人可以简单地使用递归部分,那将非常有帮助。
解决方案
推荐阅读
- php - 在 ionic 3 和 MySQL 中发布 http://localhost:8100/api.php/ 404(未找到)
- python - 如何从字符串列表中删除所有包含“hs”之前的数字的字符串,例如“18hs”?
- scala - ChiSqSelector 选择了错误的特征?
- python - 如果我的对象长度不规则,我该如何设计我的数据库表
- python - Django ORM 通过查询集访问相关的多对多对象
- mysql - 如何在 SQL 中循环遍历这个指定关系模型?
- javascript - Angular 自定义脚本加载器
- javascript - TypeError:无法读取未定义的属性“方法”
- ruby-on-rails - 'libcurl.so.4.dll':参数不正确。Windows 上的 Rails + LibCurl
- javascript - 热图矩形内的标签