title: How to find the return address of a function in C?
body: <p>I'm trying to use a small amount of AT&amp;T style inline assembly in C and GCC by reading an article on CodeProject <a href="http://www.codeproject.com/Articles/15971/Using-Inline-Assembly-in-C-C" rel="nofollow">here</a>. The main reason I wish to do this is to find the old value of the EIP register to be able to have a reliable address of instructions in my code. I have written a simple example program to demonstrate my understanding of this concept thus far :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int mainReturnAddress = 0;
  
  int main()
  {
      asm volatile (
           "popl %%eax;"
           "pushl %%eax;"
           "movl %%eax, %0;"
           : "=r" ( mainReturnAddress )
         );
  
      printf( "Address : %d\n",  mainReturnAddress );
      return 0;
  }
  </code></pre>
  
  <p>The purpose of this particular example is to pop 4 bytes from the top of the stack representing the 32 bit return address saved from the EIP register, and then to push it back on the stack. Afterwards, I store it in the global <code>mainReturnAddress</code> variable. Finally, I print the value stored in mainReturnAddress.</p>
  
  <p>The output from I recieve from this code <code>4200560</code>.</p>
  
  <p>Does this code achieve the purpose aforementioned, and is this is cross processor on the Windows platform 32-bit?</p>