0

I posted a question here 4 days ago about writing a code in MIPS that's gonna check if a given-by-user string is a palindrome or not.

The answer was more than welcome, but it just made a medium-to-small brain like mine wrinkle due to lack of experience (and stress, maybe). Anyway.

I called a friend for help and he sent me a code from his other friend that does exact that! Works like charm and all that, but I can't follow the logic behind it. He himself couldn't help me and his friend is "kind of a quirk".

That was the code:

.data
str1: .asciiz "Give me a string\n"
str2: .asciiz "Palindromic\n"
str3: .asciiz "Not Palindromic\n"
txt: .space 25

.text
reading:
la $a0, str1
li $v0, 4
syscall

li $v0, 8
la $a0, txt
la $a1, 20
syscall

la $s0, 0($a0)
la $s4, 0($a0)      

loop:
add $t0, $t0, 1     
lb $t1, ($s0)       
add $s0, $s0, 1     
beq $t1, 32, reading    
bne $t1, 10, loop   
sub $t0, $t0, 1     
sub $s0, $s0, 2     
rem $s6, $t0, 2     

beq $s6, 0, calc_counter    
sub $t0, $t0, 1         

calc_counter:
divu $t0, $t0, 2

loop1:
lb $a0, 0($s0)      
lb $t8, 0($s4)      
sub $s0, $s0, 1     
add $s4, $s4, 1     
sub $t0, $t0, 1     
beq $t0, -1, print  
beq $a0, $t8, loop

j print2

print:
la $a0, str2
li $v0, 4
syscall

j finish_process

print2:
la $a0, str3
li $v0, 4
syscall

finish_process:

li $v0, 10
syscall

I can't just "accept" the fact that this is working. I need to understand it, because of the odd nature of it. There's no string length measuring here, and the labels kinda boggle together.

What does the loop segment do?

What does the loop1 segment do?

Thanks for everything.

Community
  • 1
  • 1
Coursal
  • 1,387
  • 4
  • 17
  • 32

0 Answers0