title: Is it necessary to initialize all the used registers in inline assembly?
body: <p>I am testing simple inline assembly code using gcc. And I find the result of the following code unexpected:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(void) {
      unsigned x0 = 0, x1 = 1, x2 = 2;
      __asm__ volatile("movl %1, %0;\n\t"
                       "movl %2, %1"
                       :"=r"(x0), "+r"(x1)
                       :"r"(x2)
                       :);
      printf("%u, %u\n", x0, x1);
      return 0;
  }
  </code></pre>
  
  <p>The printed result is <code>1, 1</code>, rather than the expected <code>1, 2</code>. Then I compiled the code with <code>-S</code> option and found out gcc generated the code as</p>
  
  <pre><code>movl %eax, %edx;
  movl %edx, %eax;
  </code></pre>
  
  <p><code>%0</code> and <code>%2</code> are using the same register, why?</p>
  
  <p>I want gcc to generate, say,</p>
  
  <pre><code>movl %eax, %edx;
  movl %ecx, %eax;
  </code></pre>
  
  <p>If I add <code>"0"(x1)</code> to the input constraints, gcc will generate the code above. Does it mean that all registers need to be initialized before being used in inline assembly?</p>