title: Translating C code to MIPS Assembly - Using Arrays
body: <p>I am working on a project and can't seem to figure out what I am doing wrong. I am translating a piece of C code into MIPS Assembly Language.</p>
  
  <p>The C Code is as follows:</p>
  
  <pre><code>int A[10];
  int sum = 0;
  int i = 0;
  
  while(i &lt; 10){
      sum += A[i++];
      sum *= 2;
  }
  </code></pre>
  
  <hr>
  
  <p>I have produced the following MIPS code</p>
  
  <pre><code>where $s1 is base address of A    
  $s2 is sum    
  $s3 is i    
  s4 is 10
  </code></pre>
  
  <p>
  
  <pre><code>loop:
   beq $s3, $s4, exit     #if s3 and s4 equal, go to else
   add $t0, $s3, $s3  #2i
   add $t0, $t0, $t0  #4i
   add $s1, $s1, $t0  #A[i]
   lw $t2, 0($s1)     #t2 = A[i]
   add $s2, $s2, $t2  #sum = sum + A[i]
   addi $s3, $s3, 1   #i = i + 1
   add $s2, $s2, $s2  #sum = sum * 2
   j loop
  </code></pre>
  
  <p>While my code produces a value, it doesn't match the expected value. I have been banging my head on the desk for hours trying to see what I am doing wrong and I can't figure it out. Any help is very much appreciated</p>