title: MIPS find end of string
body: <p>I have the following mips code (running it in QTSPIM), that is supposed to count the number of characters in a string and print them. </p>
  
  <p>The logic behind it is very simple but is does not work as it should. Everything goes well until it reaches the end of the string and then it continues counting even though I compare each element to $zero to find the end of string (\0).</p>
  
  <p>Is there something wrong with my condition to exit the loop, or <em>my_string</em> does not contain \0 in the end so it won't exit?</p>
  
  <pre><code>    .data
  endl: .asciiz "\n"
  my_string: .asciiz "thisisastring"
  star: .asciiz "*"
  str_end: .word 0
  space: .asciiz " "
  
      .text
      .globl main
  
  main:
      la $a0, my_string
      li $v0, 4
      syscall
  
      la $a0, endl
      li $v0, 4
      syscall
  
      la $t0, my_string   #   load mystring to $t0
      li $t1, 0           #   make $t1 = 0, character counter
      lb $t2, ($t0)       #   make $t2 point to the first character of "my_string"
      li $t3, 1           #   $t3 is the ++ register to go to the next character  
      li $t4, 0           #   character counter
      la $t5, str_end
  
  cont:
      beqz $t0, print #   if \0 is found print and exit
      addi $t4, $t4, 1        #   increase the counter
  
      lbu $a0, ($t0)          #   print current character
      li $v0, 11
      syscall
  
      addi $t0, $t0, 1        #   go to next char
      #move $t2, $t0
  
  
      j cont
  
  print:
      move $a0, $t4
      li $v0, 1
      syscall
      j exit
  
  exit:
      li $v0, 10
      syscall
  </code></pre>