title: Decoding BNE MIPS Instruction
body: <p>I am running the following MIPS code in MARS simulator:</p>
  
  <pre><code>add $t0, $zero, $zero        # i = 0
          add $t4, $zero, $zero        # initialize the sum to zero
          add $t5, $zero, $zero        # initialize temporary register to zero
  
          la $a0, array                 # load address of array
          la $a1, array_size            # load address of array_size
          lw $a1, 0($a1)                # load value of array_size variable
  
  loop:
          sll  $t1, $t0, 2              # t1 = (i * 4)
          add $t2, $a0, $t1             # t2 contains address of array[i]
          sw $t0, 0($t2)                # array[i] = i
  
          addi $t0, $t0, 1              # i = i+1
          add $t4, $t4, $t0             # sum($t4) = ($t4 + array[i])
  
          slt $t3, $t0, $a1             # $t3 = ( i &lt; array_size)
          bne $t3, $zero, loop          # if ( i &lt; array_size ) then loop
  
          add $t5, $a0, $zero           # save contents of $a0 in temporary reg $t5
          nop                           # done.
  </code></pre>
  
  <p>In machine code, the <code>bne</code> instruction is the following: <code>00010101011000001111111111111001</code>. In this case, the immediate is: <code>1111111111111001</code> which equals to: <code>0xFFF9</code>. MIPS will take that, bit shift it to the left by 2 (multiplies it by 4) and takes its program counter to that number. However, <code>0xFFF9</code> multiplied by 4 is <code>0x3FFE4</code>. How is that possible? The program counter at <code>SLL</code> should be 0x18 and not <code>0x3FFE4</code>. What am I missing here?</p>
  
  <p>Thank you,</p>