I have this MIPS Assembly program that checks for palindrome

I have this MIPS Assembly program that checks for palindrome but I dont really understand the steps. If someone could comment some of the code and help me understand that would be greatly appreciated.

Code

.text
main:# SPIM starts by jumping to main.
## read the string S:
la $a0, startPrompt #prompt user to enter a phrase
li $v0, 4
syscall
la $a0, string_space #store phrase
li $a1, 1024 #size of string
li $v0, 8 # load code into $v0.
syscall

la $t1, string_space   # A = S.
  
la $t4, string_space   ## we need to move B to the end
  
length_loop:
lb $t5, ($a0)
beq $t5, 10, checkPali #checking for palindrome/checking if equal
bgt $t5, 47, t1
b negative
  
t1: blt $t5, 58, positive #ascii
bgt $t5, 64, t2
b negative
  
  
t2: blt $t5, 91, positive # less then?
bgt $t5, 96, t3 #greater then? //ascii
b negative
t3: blt $t7, 123, positive #ascii
b negative
  
positive: #input x is what we store the sanitized string in. Char by Char
   bgt $t5, 96, makeLow #ascii
b notUp
makeLow: addi $t5, $t5, +32 #dealing with case sensitivity.
notUp:
sb $t5, ($t1)
addi $a0, $a0, 1 #increment
addi $t1, $t1, 1
b length_loop

negative:
addi $a0, $a0, 1 #increment user input
b length_loop
  

checkPali:
#t1 = back
#li $a3, 127
sb $zero, ($a0)
addi $t1, $t1, -1 #decrement

loop:   
lb $t3, ($t4)
lb $t2, ($t1)   
beq $t3, $t2, next
b not_palin
next: jal test_loop #jump and link to test loop.
addi $t4, $t4, 1 # incrememnt
addi $t1, $t1, -1 # decrememnt
b loop # continue to loop
b not_palin

test_loop:
beq $t4, $t1, is_palin
addi $t1, $t1, -1
beq $t4, $t1, is_palin #test to see if we need to check more characters
addi $t1, $t1, 1 #when at the middle, address\'s are going to be either equal
jr $ra #or a == (b-1)

is_palin: ## print the is_palin_msg, and exit.
la $a0, string_space
li $v0, 4
la $a0 is_palin_msg
syscall
    b exit

not_palin:
la $a0, not_palin_msg ## print the is_palin_msg, and exit.
li $v0, 4
syscall
b exit
  
exit: ## exit the program:
li $v0, 10 # load \"exit\" into $v0.
syscall # make the system call.


## Here’s where the data for this program is stored
.data
string_space: .space 1024 # reserve 1024 bytes for the string.
is_palin_msg: .asciiz \"The string is a palindrome.\ \"
not_palin_msg: .asciiz \"The string is not a palindrome.\ \"
startPrompt: .asciiz \"Enter a string to see if it is a palindrome: \"

## end of palindrome.asm

Solution

## In the code following registers, we are assuming.

## $t1 – Register for X.

## $t2 – Register for Y.

## $t3 - the character at address X.

## $t4 - the character at address Y.

## $v0 - syscall parameter / return values.

## $a0 - syscall parameters.

## $a1 - syscall parameters.

.text

main:     # SPIM starts by jumping to main.

# read the string S:

    la $a0, startPrompt      #prompt user to enter string to check for palindrome

    li $v0, 4                             #print string since 4 is the syscall code for printing strings

    syscall

    la $a0, string_space     #store the user input string at the specified location

    li $a1, 1024                      #define the maximum size of string to be 1024 bytes

    li $v0, 8                            #read the user input as 8 is the syscall code for reading string

    syscall

    la $t1, string_space    #load the address of string_space into the register, $t1.

                                                # i.e. X = S

   

    la $t4, string_space    ## we need to move Y to the end of string

   

length_loop:

        lb $t5, ($a0) # load the byte at address, $a0, into the register, $t5

        beq $t5, 10, checkPali #invoke the checkPali process for each character till end of string.

                                                      # 10 is the syscall for exit, here it will be the end of the string

        bgt $t5, 47, t1   `#check if the $t5 > 47 then move to t1

        b negative

       

t1: blt $t5, 58, positive

        bgt $t5, 64, t2

        b negative

       

       

t2: blt $t5, 91, positive # less than 91 go to positive

        bgt $t5, 96, t3 #greater than 96 go to t3

        b negative

t3: blt $t7, 123, positive

        b negative

   

positive: #input x is what we store the sanitized string in. Char by Char

    bgt $t5, 96, makeLow

        b notUp

        makeLow: addi $t5, $t5, +32 #dealing with case sensitivity.

        notUp:

        sb $t5, ($t1)

        addi $a0, $a0, 1 #increment each memory location

        addi $t1, $t1, 1

        b length_loop

negative: #do not add char to new sanitized string

        addi $a0, $a0, 1 #increment user input

        b length_loop

checkPali:

    #t1 = back

    #li $a3, 127

    sb $zero, ($a0)

    addi $t1, $t1, -1   # #decrement from end of $t1, increment from beginning of $t4

loop:         #loop to check whether the strings are same just the strings start from opposite ends

    lb $t3, ($t4)

    lb $t2, ($t1)  

    beq $t3, $t2, next #continue the check if bytes are equal

    b not_palin

    next: jal test_loop #jump and link to test loop. Ensure we are not at last byte

    addi $t4, $t4, 1               # move t4 closer to middle

    addi $t1, $t1, -1             # move t1 closer to middle but from reverse side

    b loop   # continue to loop

    b not_palin

test_loop:

        beq $t4, $t1, is_palin

        addi $t1, $t1, -1

        beq $t4, $t1, is_palin   #test to see if we need to check more characters

        addi $t1, $t1, 1    #when at the middle, address\'s are going to be either equal

        jr $ra          #or a == (b-1)

is_palin:    ## print the is_palin_msg, and exit.

        la $a0, string_space

        li $v0, 4

        la $a0 is_palin_msg

        syscall

         b exit

not_palin:

        la $a0, not_palin_msg ## print the is_palin_msg, and exit.

        li $v0, 4

        syscall

        b exit     

       

exit: ## exit the program:

li $v0, 10 # load \"exit\" into $v0.

syscall # make the system call.

## Here’s where the data for this program is stored

.data

string_space: .space 1024    # reserve 1024 bytes for the string.

is_palin_msg: .asciiz \"The string is a palindrome.\ \"

not_palin_msg: .asciiz \"The string is not a palindrome.\ \"

startPrompt: .asciiz \"Enter a string to see if it is a palindrome: \"

## end of palindrome.asm

I have this MIPS Assembly program that checks for palindrome but I dont really understand the steps. If someone could comment some of the code and help me under
I have this MIPS Assembly program that checks for palindrome but I dont really understand the steps. If someone could comment some of the code and help me under
I have this MIPS Assembly program that checks for palindrome but I dont really understand the steps. If someone could comment some of the code and help me under
I have this MIPS Assembly program that checks for palindrome but I dont really understand the steps. If someone could comment some of the code and help me under
I have this MIPS Assembly program that checks for palindrome but I dont really understand the steps. If someone could comment some of the code and help me under

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site