title: MIPS, using a while loop to calculatethe sum of odd integers 1-9
body: <p>The following is my code in MIPS to calculate the sum of odd integers using a while loop.</p>
  
  <pre><code>.data
      num: .space 4
  
  
  .text
  .globl main
  main:
      li $t1, 1
      li $t2, 9   # make $t2 9 to break the loop
      li $t3, 1   
  
  loop:
          beq     $t3, 11, Exit   # check to see if $t3 = 11 if so exit
          addi    $t3, $t3, 2 # change $t3 to next odd number by adding 2   
          add $t1, $t1, $t3   # add $t3 to $t1 (1+3,...3+5...etc...)
  
      j loop  #jump back to the start of the loop
  Exit:
          li $v0, 1   # system call code to print an int
          lw $a0, num # address of int to print
          syscall     # print the int
      jr $ra  #exit
  </code></pre>
  
  <p>This is my first real experience with MIPS and I'm not sure what is going wrong in this code. I put the print inside the while loop to see if it was ever calculating, but the result is always 1.
  So, my result at the end is just 111111.</p>
  
  <p>Edit: Removed the prints inside of the loop with the same result.</p>
  
  <p>And OS is Windows 7 64x</p>
  
  <p>Update: Having num as a variable was over complicating things. Code has been revised as follow and works. Thank you for the help!</p>
  
  <pre><code>enter code here
  .data   
  .text
  .globl main
  main:
      addi $t1, $0, 1
      addi $t2, $0, 3 
  
  loop:   bge     $t2, 11, Exit   # check to see if $t3 &gt;= 11 if so exit
          add $t1, $t1, $t2   # add $t2 to $t1 (1+3,...3+5...etc...)    
          addi    $t2, $t2, 2 # change $t2 to next odd number by adding 2
  
      j loop  #jump back to the start of the loop
  Exit:
          li $v0, 1   # system call code to print an int
          move $a0,$t1    # address of int to print
          syscall     # print the int
  
      jr $ra  #exit
  </code></pre>