title: Why does GCC chose dword movl to copy a long shift count to CL?
body: <p>In the third chapter of <em>Computer System: A Programmer's Prespective</em>, an example program is given when talking about shift operations:</p>
  <pre class="lang-c prettyprint-override"><code>long shift_left4_rightn(long x, long n)
  {
      x &lt;&lt;= 4;
      x &gt;&gt;= n;
      return x;
  }
  </code></pre>
  <p>And its assembly code is as follows (reproducible with <a href="https://godbolt.org/z/q5s5fs" rel="nofollow noreferrer">GCC10.2 <code>-O1</code> for x86-64 on the Godbolt compiler explorer</a>.  <code>-O2</code> schedules the instructions in a different order but still uses <code>movl</code> to ECX):</p>
  <p><strong>shift_left4_rightn:</strong><br />
  　　<strong>endbr64</strong><br />
  　　<strong>movq　　　%rdi, %rax</strong>　　　　<em>Get x</em><br />
  　　<strong>salq　　　$4, %rax</strong>　　　　　　<em>x &lt;&lt;= 4</em><br />
  　　<strong>movl　　　%esi, %ecx</strong>　　　　<em>Get n</em><br />
  　　<strong>sarq　　　%cl, %rax</strong>　　　　　<em>x &gt;&gt;= n</em><br />
  　　<strong>ret</strong></p>
  <p>I wonder why the assembly code of getting n is <code>movl　%esi, %ecx</code> instead of <code>movq　%rsi, %rcx</code> since <code>n</code> is a quad-word.</p>
  <p>On the other hand, <code>movb　%sil, %cl</code> might be more suitable if the optimation is considered, since the shift amount only use the single-byte register element <code>%cl</code> and those higher bits are all ignored.</p>
  <p>As a result, I really fail to figure out the reason for using &quot;<strong>movl　%esi, %ecx</strong>&quot; when dealing with long integer.</p>