title: What does the PIC register (%ebx) do?
body: <p>I have written a "dangerous" program in C++ that jumps back and forth from one stack frame to another. The goal is to be jump from the lowest level of a call stack to a caller, do something, and then jump back down again, each time skipping all the calls inbetween.</p>
  
  <p>I do this by manually changing the stack base address (setting <code>%ebp</code>) and jumping to a label address. It totally works, with gcc and icc both, without any stack corruption at all. The day this worked was a cool day.</p>
  
  <p>Now I'm taking the same program and re-writing it in C, and it doesn't work. Specifically, it doesn't work with gcc v4.0.1 (Mac OS). Once I jump to the new stack frame (with the stack base pointer set correctly), the following instructions execute, being just before a call to <code>fprintf</code>. The last instruction listed here crashes, dereferencing NULL:</p>
  
  <pre><code>lea    0x18b8(%ebx), %eax
  mov    (%eax), %eax
  mov    (%eax), %eax
  </code></pre>
  
  <p>I've done some debugging, and I've figured out that by setting the <code>%ebx</code> register manually when I switch stack frames (using a value I observed before leaving the function in the first place), I fix the bug. I've read that this register deals with "position independent code" in gcc.</p>
  
  <p><em>What is position independent code? How does position independent code work? To what is this register pointing?</em></p>