title: Using AT&T inline assembler for GCC
body: <p>I'm writing a simple but a little specific program:</p>
  
  <p><b> Purpose: </b> calculate number from it's factorial <br>
  <b> Requirements: </b> all calculations must be done on gcc inline asm (at&amp;t syntax) </p>
  
  <p> Source code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
          unsigned n = 0, f = 0;
  
          std::cin &gt;&gt; n;
  
          asm
          (
                  "mov %0, %%eax \n"
                  "mov %%eax, %%ecx \n"
                  "mov 1, %%ebx \n"
                  "mov 1, %%eax \n"
                  "jmp cycle_start\n"
                  "cycle:\n"
                  "inc %%ebx\n"
                  "mul %%ebx\n"
                  "cycle_start:\n"
                  "cmp %%ecx, %%eax\n"
                  "jnz cycle\n"
                  "mov %%ebx, %1 \n":
  
                  "=r" (n):
                   "r" (f)
  
         );
  
         std::cout &lt;&lt; f;
  
         return 0;
  }
  </code></pre>
  
  <p>This code causes SIGSEV.</p>
  
  <p>Identic program on intel asm syntax (<a href="http://pastebin.com/2EqJmGAV" rel="nofollow">http://pastebin.com/2EqJmGAV</a>) works fine. Why my "AT&amp;T program" fails and how can i fix it?</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
     unsigned n = 0, f = 0;
  
     std::cin &gt;&gt; n;
  
     __asm
     {
        mov eax, n
  
        mov ecx, eax
        mov eax, 1
        mov ebx, 1
        jmp cycle_start
  
        cycle:
  
        inc ebx
        mul ebx
  
        cycle_start:
  
        cmp eax, ecx
        jnz cycle
  
        mov f, ebx
     };
     std::cout &lt;&lt; f;
  
     return 0;
  }
  </code></pre>
  
  <p>UPD: Pushing to stack and restoring back used registers gives the same result: SIGSEV</p>