title: Read flag register from C program
body: <p>For the sake of curiosity I'm trying to read the flag register and print it out in a nice way. </p>
  
  <p>I've tried reading it using gcc's <strong>asm</strong> keyword, but i can't get it to work. Any hints how to do it? I'm running a Intel Core 2 Duo and Mac OS X. The following code is what I have. I hoped it would tell me if an overflow happened:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main (void){
    int a=10, b=0, bold=0;
    printf("%d\n",b);
    while(1){
      a++;
    __asm__ ("pushf\n\t"
     "movl 4(%%esp), %%eax\n\t"
     "movl %%eax , %0\n\t"
     :"=r"(b)      
     :         
     :"%eax"        
     ); 
    if(b!=bold){ 
      printf("register changed \n %d\t to\t %d",bold , b);
    }
    bold = b;
    }
  }
  </code></pre>
  
  <p>This gives a segmentation fault. When I run gdb on it I get this:</p>
  
  <pre><code>Program received signal EXC_BAD_ACCESS, Could not access memory.
  Reason: KERN_INVALID_ADDRESS at address: 0x000000005fbfee5c
  0x0000000100000eaf in main () at asm.c:9
  9       asm ("pushf \n\t"
  </code></pre>