title: Pointer Deferencing in x86 Assembly Code
body: <p>I'm reading my textbook and it has code for a swap function:</p>
  
  <p>In C:</p>
  
  <pre><code>int exchange(int *xp, int y) {
   int x = *xp;
   *xp = y;
   return x; 
  }
  </code></pre>
  
  <p>In x86 Assembly with annotations:</p>
  
  <pre><code>// xp is at %ebp + 8, y at %ebp + 12
  movl 8(%ebp), %edx      // get xp
  movl (%edx), %eax       // get x at xp
  movl 12(%ebp), %ecx     // get y
  movl %ecx, (%edx)       // store y at xp
  </code></pre>
  
  <p>So from my understanding, if the int* xp pointed to an int I at address A, then the first line of assembly code stores A at %edx. Then it gets dereferenced in the second line and stored at %eax.</p>
  
  <p>If this is true, I'm wondering why line 1's "8(%ebp)" doesn't dereference the pointer, storing the int I in %edx instead of the address A? Isn't that what parentheses do in assembly? </p>
  
  <p>Or does that mean that when pointers are pushed onto the stack, the address of the pointer is pushed on rather than the value it holds so 8(%ebp) technically holds &amp;xp?</p>
  
  <p>Just wanted to clarify if my understanding was correct. </p>