title: MIPS return value from memory address
body: <p>I have to program in Assembly (MIPS) a function to return the max value of an array.</p>
  
  <p>The C code is this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int MaxAssembly(int *ptr, int num_elements); 
  
  int main ( ) 
  { 
    int n=9; 
    int tab[] = {2, -8, 0, 25, 14, 2, 9, 15, -32}; 
    printf("The maximum is %d \n", MaxAssembly(tab,n)); 
  </code></pre>
  
  <p><code>MaxAssembly</code>is the function I have to program in Assembly.</p>
  
  <p>I'm not in the part to find the max. My problem in in reading the functions arguments. I have made this code to do some tests.</p>
  
  <pre><code>    .data
  
      .text
      .globl  MaxAssembly
  
  MaxAssembly:
      add $9,$5,$zero
      move    $2,$9
      jr  $ra
  </code></pre>
  
  <p>Doing this code I can see that I'm reading the second function argument as expected. It's printed on the screen <code>The maximum is 9</code>
  Changing the code to:</p>
  
  <pre><code>    .data
  
      .text
      .globl  MaxAssembly
  
  MaxAssembly:
      move    $2,$4
      jr  $ra
  </code></pre>
  
  <p>I can see that it's reading the first argument of the function as a memory address and it's printed on the screen <code>The maximum is 2143429780</code>. So far is working as expected.</p>
  
  <p>The problem is when I try to read the element stored at that memory address (the first element of the array). I'm getting one segmentation fault...
  I'm doing this:</p>
  
  <pre><code>    .data
  
      .text
      .globl  MaxAssembly
  
  MaxAssembly:
      lw      $16,0($4)
      move    $2,$16
      jr  $ra
  </code></pre>
  
  <p>What am I doing wrong? wasn't <code>lw</code>supposed to store at <code>$16</code> the first item of the array? Using <code>lb</code>is the same</p>