title: MIPS Assembly - Arrays?
body: <p>I am absolutely brand new to assembly programming and am trying to implement the following function (in C) in MIPS:</p>
  
  <pre><code>int main()
  {
    int A[5]; // Empty memory region for 5 elements
    int B[5] = {1,2,4,8,16};
    int i;
  
    for(i=0; i&lt;5; i++) {
      A[i] = B[i] - 1;
    }
  
    i--;
    while(i &gt;= 0) {
      A[i]=(A[i]+B[i]) * 2;
      i--;
    }
  }
  </code></pre>
  
  <p>So far what I have is this:</p>
  
  <pre><code>main:
  #Register Map
  #i --&gt; $s0
  #A[0] --&gt; $s1   address of start of A
  #B[0] --&gt; $s2   address of start of B   
  #A[i] --&gt; $t0
  #B[i] --&gt; $t1
  #(A[i] + B[i]) --&gt; $t2
  #((A[i] + B[i]) * 2) --&gt; $t3
  
      li  $s0, 0      #Load immediate value 0 into i
  
  #Begin for loop:
  #for(i=0; i&lt;5; i++){ A[i] = B[i] - 1; }
  
  FOR_LOOP:   
      beq     $s1, $t0, END_FOR #Branch if i == 5, go to END_FOR
  
      addi    $s0, $s0, 1 #Add immediate value 1 to i (i++)
      j FOR_LOOP          #Jump back to the top to loop again
  
  END_FOR:                #End for loop
      addi    $s0, $s0, -1    #Add immediate value -1 to i (i--)
  
  #Begin while loop:  
  #while(i &gt;= 0) { A[i] = (A[i] + B[i]) * 2; i--; }
  
  WHILE_LOOP:
      blt     $s1, 0, END_WHILE #Branch END_WHILE when !(i &gt; 0)
  
      addi    $s0, $s0, -1    #Add immediate value -1 to i (i--)
      j WHILE_LOOP        #Branch back to the while loop
  
  END_WHILE:
      sw      $s0, i      #Copy value in register to memory i
      li      $v0, 10     #Read integer from user, returned in $v0 (10=exit)
  
      syscall         #End program
  
  #Store A, B in memory
      .data
  
  A:  .word   0:5         #Allocate 20 consecutive bytes for 5-element integer word array A
  B:  .word   1, 2, 4, 8, 16  #Integer words stored in 5-element array B
  i:  .word   1       #Initial Value 1
  </code></pre>
  
  <p><strong>My main questions are</strong>:</p>
  
  <ol>
  <li><p>How would I access the elements of the array, or where do I begin with <code>la</code>?</p></li>
  <li><p>Did I declare the arrays correctly?</p></li>
  <li><p>Does my register map make sense for what is trying to be accomplished?</p></li>
  </ol>
  
  <p><em>Thank you for any and all help.</em></p>