【发布时间】:2021-01-04 01:46:17
【问题描述】:
.data
org_str: .space 256
rev_str: .space 256
str: .asciiz "Enter the Line: "
pal: .asciiz "palindrome"
not_pali: .asciiz "Not palindrome"
.text
.globl main
main:
li $v0, 4
la $a0, str
syscall
# Taking String from the user
li $v0, 8
la $a0, org_str
li $a1, 256
syscall
# Initilize $t0 and $t2
li $t0, 0
li $t1, 0
loop_len:
add $t1, $a0, $t0
lb $t2, 0($t1) # Load the data in the byte in $t2
beqz $t2, len_exit # Loop till $t2 reaches zero
addiu $t0, $t0, 1 # increment of the counter
j loop_len
len_exit:
#Return $t0 to the last Charater
subi $t0, $t0, 1
li $s0, 0 # Initialize variable
addi $s0, $t0, 0 # Save the length of the String
# Load the original string in $t2
la $t2, org_str
# Intialize i and j
li $t1, 0 # i
li $t3, 0 # j
reverse_loop:
add $t3, $t2, $t0 # $t2 is the base address
lb $t4, 0($t3) # load a byte
beqz $t4, exit # go to the exit if null was found
sb $t4, rev_str($t1) # Overwrite the byte
addi $t0, $t0, -1 # decrement of j by 1 (j--)
addi $t1, $t1, 1 # increment of i by one (i++)
j reverse_loop # Loop until we reach the length of the String
exit:
li $t0, 0
li $t4, 0
li $t7, 0
CheckChar_loop:
lb $t4, org_str($t0)
lb $t7, rev_str($t0)
beq $t4, $zero, exit_1 # go to the exit if null was found
bne $t4, $t7, not_pal # if $t7 and $t4 not equal
addi $t0, $t0, 1 # increment of i by 1 (i++)
j CheckChar_loop
exit_1:
#li $v0, 1
#addi $a0, $t0, 0
#syscall
li $v0, 4
la $a0, pal
syscall
#Exit the program
li $v0, 10
syscall
not_pal:
#li $v0, 1
#addi $a0, $t0, 0
#syscall
li $v0, 4
la $a0, not_pali
syscall
#Exit the program
li $v0, 10
syscall
.end main
我正在尝试检查 回文。因此,想法是修改字符串并在每个索引处检查反向字符串中的字符是否与原始字符相同。 我正在尝试检查 org_str 是否等于 rev_str 但它每次都以 $t7 和 $t4 的形式存在,即使它们相等。 rev_str 是 org_str 的反转字符串。
【问题讨论】:
-
1) 请不要发布代码图像 2) 这不是 minimal reproducible example 3) 您不需要反转字符串,您可以向后迭代 4) 逻辑似乎很好一眼。使用调试器/模拟器查看发生了什么。
-
我们看不到整个代码。我们应该猜出问题所在吗?
-
@ErikEidt 问题是我反转了字符串,然后我尝试检查反转后的字符串中的每个字符是否与原始字符相同