In the third chapter of Computer System: A Programmer's Prespective, an example program is given when talking about shift operations:
long shift_left4_rightn(long x, long n)
{
x <<= 4;
x >>= n;
return x;
}
And its assembly code is as follows (reproducible with GCC10.2 -O1
for x86-64 on the Godbolt compiler explorer. -O2
schedules the instructions in a different order but still uses movl
to ECX):
shift_left4_rightn:
endbr64
movq %rdi, %rax Get x
salq $4, %rax x <<= 4
movl %esi, %ecx Get n
sarq %cl, %rax x >>= n
ret
I wonder why the assembly code of getting n is movl %esi, %ecx
instead of movq %rsi, %rcx
since n
is a quad-word.
On the other hand, movb %sil, %cl
might be more suitable if the optimation is considered, since the shift amount only use the single-byte register element %cl
and those higher bits are all ignored.
As a result, I really fail to figure out the reason for using "movl %esi, %ecx" when dealing with long integer.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…