title: How to understand atomic test_and_set in assembly level?
body: <p>Hi I'm new to the gcc's built-in atomic functions. And I'm using the <code>test-and-set</code> one. You may find the reference <a href="https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html" rel="nofollow">here</a></p>
  
  <p>Here's the question:</p>
  
  <p>I've done this code:</p>
  
  <pre><code>#define bool int
  #define true 1
  #define false 0
  
  int main() {
      bool lock = true;
      bool val = __sync_lock_test_and_set(&amp;lock, true);
  
      return 0;
  }
  </code></pre>
  
  <p>What I intend to do is to check the <strong>assembly instruction</strong> of <code>__sync_lock_test_and_set</code>. I've used:</p>
  
  <pre><code>gcc -S [filename].c
  </code></pre>
  
  <p>And the result is:</p>
  
  <pre><code>        .file   "test_and_set.c"
          .file   "test_and_set.c"
          .text
          .globl  main
          .type   main, @function
  main:
  .LFB0:
          .cfi_startproc
          pushl   %ebp
          .cfi_def_cfa_offset 8
          .cfi_offset 5, -8
          movl    %esp, %ebp
          .cfi_def_cfa_register 5
          subl    $16, %esp
          movl    $1, -8(%ebp)
          movl    $1, %eax
          xchgl   -8(%ebp), %eax
          movl    %eax, -4(%ebp)
          movl    $0, %eax
          leave
          .cfi_restore 5
          .cfi_def_cfa 4, 4
          ret
          .cfi_endproc
  .LFE0:
          .size   main, .-main
          .ident  "GCC: (GNU) 4.8.1"
  </code></pre>
  
  <p>However, I can't find where the test_and_set instruction is...</p>
  
  <p>As you can see, I'm using gcc-4.8.1, and the environment is MAC OSX 10.10(I'm sure that this gcc is not what Apple provides. I've compiled this by myself)</p>
  
  <p>Thanks!</p>