title: Array addressing in AT&T GAS assembly
body: <p>I'm building a compiler for a subset of C, and now I'm dealing with arrays. Suppose I have the following:</p>
  
  <pre><code>int main() {
      int x[3];
      x[0] = 1;
      x[1] = 3;
      x[2] = 4;
  }
  </code></pre>
  
  <p>In assembly, the asignments would be like this:</p>
  
  <pre><code>movl    $1, -12(%ebp)
  movl    $3, -8(%ebp)
  movl    $4, -4(%ebp)
  </code></pre>
  
  <p>So far, so good. Now if I wanted to access an arbitrary position, stored in <code>%ebx</code>, how should I do it? I've been reading <a href="http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Address_operand_syntax" rel="nofollow">this wikibook</a> but I don't quite get the syntax. </p>
  
  <p>I've tried the following but it does not seem to work:</p>
  
  <pre><code># Suppose I want to do x[1] = 5
  movl $1, %ebx
  
  movl $5, -4(%ebp, %ebx, 4)
  </code></pre>
  
  <p>What am I missing?</p>