title: How to write clobber for inline assembly?
body: <p>I just learned clobber usage through these docs(<a href="https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Extended-Asm.html" rel="nofollow noreferrer">https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Extended-Asm.html</a>) as well as previous questions.</p>
  <p>I also read another introduction post.
  The description in the post is:</p>
  <blockquote>
  <p>Its purpose is to inform the compiler that the registers in the
  Clobbers list will be implicitly modified by the assembly code in the
  asm statement.  Since the registers in Clobbers will be implicitly
  modified by the assembly code in the asm statement, the compiler will
  not use the registers specified in Clobbers when selecting registers
  for input operands and output operands, thus avoiding logical errors
  such as data overwriting.</p>
  </blockquote>
  <p>In actual use, I still have some confusion.</p>
  <ol>
  <li>If I specify the registers used by input and output, do I still need to write clobber?  I saw an example introducing clobber.</li>
  </ol>
  <pre><code>
      #include &lt;stdio.h&gt;
      
   
      int inc2(int src) {
        int dst;
      
        asm(&quot;mov %1, %0\n\t&quot;
            &quot;mov $3, %%eax\n\t&quot;
            &quot;add $1, %0&quot;
            : &quot;=r&quot;(dst)
            : &quot;r&quot;(src));
      
        return dst;
      }
      
      int inc3(int src) {
        int dst;
      
        asm(&quot;mov %1, %0\n\t&quot;
            &quot;mov $3, %%eax\n\t&quot;
            &quot;add $1, %0&quot;
            : &quot;=r&quot;(dst)
            : &quot;r&quot;(src)
            : &quot;%eax&quot;);
      
        return dst;
      }
      
      int main(int argc, char *argv[]) {
        printf(&quot;inc2: %d\n&quot;, inc2(1));
        printf(&quot;inc3: %d\n&quot;, inc3(1));
      }
  </code></pre>
  <p>The above example outputs are 2 and 4 respectively. The reason is that the eax register is used in <code>mov $3, %eax</code> in the <code>src2</code> function, and <code>eax</code> is also selected as the input register.</p>
  <p>If clobber is used to indicate which registers should not be selected as input registers, if I explicitly instruct, is there no need to write clobber?
  For example
  int inc4(int src) {
  int dst;</p>
  <pre><code>  asm(&quot;mov %%rcx, %%rax\n\t&quot;
        &quot;add $1, %%rax&quot;
        : &quot;=a&quot;(dst)
        : &quot;c&quot;(src)
        : );
  
    return dst;
  }
  </code></pre>
  <p>In the above example, the input register is explicitly ecx and the output is explicitly eax. Is it possible that by this method I can avoid writing clobber altogether?  Or should the registers modified in assembly be marked?</p>
  <ol start="2">
  <li>I have seen some use of inline assembly in some open source code, but it is not consistent with my understanding. But these codes are written by very professional people and widely used by many people. I think it's very precise. I would like to analyze some of how to write inline assembly canonically through these examples.</li>
  </ol>
  <p>(1) Why does the two functions below clobber include edx and ecx? Is it because the output register (=a) selects eax, so the clobber does not need to be full of eax? There are no input parameters in this function, only one output. What is the significance of marking these two registers?</p>
  <pre><code>static inline uint32_t rdtscp() {
    uint32_t rv;
    asm volatile (&quot;rdtscp&quot;: &quot;=a&quot; (rv) :: &quot;edx&quot;, &quot;ecx&quot;);
    return rv;
  }
  
  
  
  
  
   static inline uint32_t memaccesstime(void *v) {
        uint32_t rv;
        asm volatile (
            &quot;mfence\n&quot;
            &quot;lfence\n&quot;
            &quot;rdtscp\n&quot;
            &quot;mov %%eax, %%esi\n&quot;
            &quot;mov (%1), %%eax\n&quot;
            &quot;rdtscp\n&quot;
            &quot;sub %%esi, %%eax\n&quot;
            : &quot;=&amp;a&quot; (rv): &quot;r&quot; (v): &quot;ecx&quot;, &quot;edx&quot;, &quot;esi&quot;);
        return rv;
      }
  </code></pre>
  <p>Thanks!</p>