我正在尝试检查一组字符串是否是回文。我已经确保检查字符串的长度,以便如果它命中null,它将返回到字符串中的最后一个字母。我不确定我是否做对了。然后,我比较字符串的最后一个字符和第一个字符,并继续进行直到它到达中间为止。这是我目前拥有的:
CheckPalindrome:
addi $t0, $a0, 0
addi $t1, $t0, 0
StrLen:
lbu $t2, 0($t1)
beq $t2, $0, End #If equal to 0 end loop
addi $t1, $t1, 1
j StrLen #Loop
End:
addi $t1, $t1, -1
Compare:
#If string is less than 2 then is palindrome
slti $t0, $t0, 2
bne $t0, $0, True
#Compare first and last characters
lbu $t0, 0($a0) #load first character to t0
addi $t1, $t1, -1 #t1 points to the last letter in string
lbu $t1, 0($t1) #load last character into t1
add $t1, $t1, $a0
bne $t0, $t1, False #If first and last char not equal, return false
addi $a2, $a2, -2
addi $a0, $a0, 1
j Compare
True:
addi $v0, $0, 1
jr $ra
False:
addi $v0, $0, 0
jr $ra
当前输出为:
is not a palindrome.
a is not a palindrome.
b is not a palindrome.
aba is not a palindrome.
abba is not a palindrome.
baab is not a palindrome.
ababa is not a palindrome.
aabba is not a palindrome.
abbab is not a palindrome.
这显然是不正确的,因此有关如何更正此问题的任何输入都是很好的。