title: How to iterate a string in mips assembly
body: <p>So I am working on a project right now where we have to take in a string and convert it to all uppercase letters and then to lowercase letters if either the toUpper or toLower byte is anything but 0.  Right now I am still working on the uppercase portion (guessing the lowercase will be almost the same) but I am getting stuck either when I am going through the string to find the letters that are uppercase or when I am calling the syscall to print.  Any help would be great.  Here's what I have so far:</p>
  
  <pre><code>    .data
  
      toUpper: .byte   1
      toLower: .byte   0
  
      string:
          .asciiz "   A long time ago in a Galaxy far, far away...."
  
  # Your code goes below this line
  
      origString:
          .asciiz "Original string:\n"
  
      toUpForwardStr:
          .asciiz "\nConverted to upper-case:\nForward:\n"
  
      toLowerForwardStr:
          .asciiz "\nConverted to lower-case:\nForward:\n"
  
      backwardStr:
          .asciiz "Backward:"
  
  .text
  
  
  
  main:   
      # Function prologue
      subu    $sp, $sp, 24                    # allocate stack space -- default of 24 here
      sw      $fp, 0($sp)                     # save caller's frame pointer
      sw      $ra, 4($sp)                     # save return address
      addiu   $fp, $sp, 20                    # setup main's frame pointer
  
      la      $a0, origString             #Print "Original String:"
      addi    $v0, $zero, 4
      syscall
  
      la      $a0, string                 #Print string
      addi    $v0, $zero, 4
      syscall
  
      la      $s0, toUpper
      sb      $s1, 0($s0)                 #toUpper stored in $s1
  
      la      $s0, toLower
      sb      $s2, 0($s0)                 #toLower stored in $s2
  
      bne     $s1, $zero, toUpperCase     #Jump toUpperCase if toUpper ≠ 0
  
  
  
      toUpperCase:
          la      $a0, toUpForwardStr     #Print "Converted to upper-case:"
          addi    $v0, $zero, 4               #      "Forward:"
          syscall
  
          la      $s3, string             #$s3 holds address to string
  
          addi    $s1, $zero, 0               #$s1 = i = 0
  
          j   upperCaseLoop                   #Goto upperCaseLoop
  
          upperCaseLoop:
  
              # Compute address of string[i]
  
              add     $t2, $s3, $s1           # $t2 = address of string[i]
              lb      $t3, 8($t2)             # $t3 = elements[i]
  
              beq     $t3, $zero, upperDone   # test if for loop is done
  
              addi    $t6, $zero, 96          #$t6 = 96 (lowercase letters)
              bgt     $t3, $t6, isLowercase1#If letter is lowercase goto isLowercase1
  
              comeBackFromLowercaseIfs:
  
              move    $t3, $a0
              addi    $v0, $zero, 11
              syscall
  
  
  
  
              addi    $s1, $s1, 1             # i++
              j       upperCaseLoop
  
      upperDone:
  
      bne     $s2, $zero, toLowerCase     #Jump toLowerCase if toLower ≠ 0
  
  
      toLowerCase:
  
          la      $a0, toLowerForwardStr  #Print "Converted to lower-case:"
          addi    $v0, $zero, 4               #      "Forward:"
          syscall
          j       done                        #The END!!
  
  
  
  
  
  
  
      isLowercase1:
          addi    $t7, $zero, 123         #$t7 = 123
          blt     $t3, $t7, isLowercase2  #Goto isLowercase2
          j       comeBackFromLowercaseIfs    #Go back to uppercaseLoop
  
      isLowercase2:
          addi    $t3, $zero, -30         #changes letter to lowercase
          j       comeBackFromLowercaseIfs    #Go back to uppercaseLoop
  
  
  
  done:       
      # Epilogue for main -- restore stack &amp; frame pointers and return
      lw    $ra, 4($sp)     # get return address from stack
      lw    $fp, 0($sp)     # restore the caller's frame pointer
      addiu $sp, $sp, 24    # restore the caller's stack pointer
      jr    $ra             # return to caller's code
  </code></pre>