title: MIPS: Printing vowel and consonant count of a string
body: <p>I have the program working, my only issue is the loop runs one extra time than it should which increments the consonant count one too many times. I can of course get around this by just subtracting 1 from the count at the end but I am curious why it is running an extra time. I've used the step feature to try and troubleshoot in PCSpim and I can see that $a0 = 0000000a instead of 00000000 on what should be the last loop. Why is this happening?</p>
  <pre><code>#################################################
  #                       #
  #               text segment            #
  #                       #
  #################################################
  
      .text
      .globl __start
  __start:                # execution starts here
  
      la $a0,prompt1  # print prompt on terminal
      li $v0,4        # system call to print
      syscall         # out a string
  
      la $a0,string
      li $a1,20
      li $v0, 8   # syscall 8 reads string/letter
      syscall
      
      li $t1, 0   # register used to keep track of number of vowels
      li $t2, 0   # register used to keep track of number of consonances
  
      la $a1, string  # prepare for passing a letter to the procedure
      
  
  loop:   
      lb $a0, 0($a1)  # load one character from the string
      beqz $a0, End   # if no more characters then go to End
      addi $a1,$a1,1  # increment address position to print next character in string
  
      jal vowelp  # procedure call
  
      move $t0,$v0    # save the value returned from the procedure call
  
      beqz $t0, NotVowel
      
      addi $t1, $t1, 1
  
      j loop
  
  NotVowel:
      addi $t2, $t2, 1
      
      j loop
  End:    
      la $a0, answer1
      li $v0, 4
      syscall
      
      move $a0, $t1
      li $v0, 1
      syscall     # print number of vowels
  
      la $a0,endl # syscall to print out
      li $v0,4    # a new line
      syscall     
  
      la $a0, answer2
      li $v0, 4
      syscall
      
      addi $t2, $t2, -1   
  
      move $a0, $t2
      li $v0, 1
      syscall     # print number of consonants
  
      la $a0,endl # syscall to print out
      li $v0,4    # a new line
      syscall 
  
      li $v0,10
      syscall     # Bye!
  
  ##############################################################
  ## Define the procedure vowelp
  ##############################################################
  
  vowelp:
  
      li $v0, 0   # initialize $v0 to 0
      beq $a0,' ',loop
      beq $a0,'A',yes 
      beq $a0,'a',yes
      beq $a0,'E',yes
      beq $a0,'e',yes
      beq $a0,'I',yes
      beq $a0,'i',yes
      beq $a0,'O',yes
      beq $a0,'o',yes
      beq $a0,'U',yes
      beq $a0,'u',yes
      jr $ra
  yes:    li $v0,1
      jr $ra
      
  
  #################################################
  #                       #
  #               data segment            #
  #                       #
  #################################################
  
          .data
      string:     .space 20
      prompt1:    .asciiz &quot;Enter a string: &quot;
      
      answer1:    .asciiz &quot;The number of vowels in the string is: \n&quot;
      answer2:    .asciiz &quot;The number of consonants is: \n&quot;
      message:    .asciiz &quot;The character entered is not a vowel.&quot;
      ans1:       .asciiz &quot;The character entered is a vowel. &quot;
      endl:       .asciiz &quot;\n&quot;       
  
  ##
  </code></pre>