title: MIPS Palindrome Check
body: <p>I am trying to write a MIPS program that checks if an input string is a palindrome. I tested the string "HelllleH" and when stepping through the program, I saw that during the first loop of PAL_CHECK t0 = 0 but t1 = 104. Logically, t0 = 0 and t1 = 0 also in the first loop. Can someone tell what's wrong in this program?</p>
  
  <pre><code># a0 is input
  # a1 is current character we are looking at 
  # a2 is length of string
  # t0 is character at beginning of string
  # t1 is character at end of string
  # v0 stores whether string is palindrome or not (0 for false, 1 for true)
  
  ispalindrome:
      addi    $a2, $0, 0  # length counter
  
  FIND_LEN:
      lbu         $a1, 0($a0) # load character into $a1
      beq         $a1, $0, PAL_CHECK  # Break if string is at the end
      addi    $a2, $a2, 1 # increment counter
      addi    $a0, $a0, 1 # increment str address
      j       FIND_LEN
  
  PAL_CHECK:
      # Is palindrome if length is less than 2
      slti    $t0, $a2, 2
      bne         $t0, $0, RETURN_TRUE
  
      # Check first and last chars to see if they are the same
      lbu         $t0, 0($a0) # first char is t0
      add         $t1, $a2, $a0 # last char is t1
      lbu         $t1, 0($t1)
      bne         $t0, $t1, RETURN_FALSE # if they are not equal, return false
  
      # continue recursion
      addi    $a2, $a2, -2
      addi    $a0, $a0, 1
      j       PAL_CHECK
  
  RETURN_FALSE:
      addi    $v0, $0, 0
      jr          $ra
  
  RETURN_TRUE:
      addi    $v0, $0, 1
      jr          $ra
  </code></pre>