// Code I'll be working with
int shift_left2_rightn(int x, int n)
{
x <<= 2;
x >>= n;
return x;
}
Question 1. Left shift = SAL and SHL. My book says they have the same effect. Then why are there two shift operations?
For example:
movl 8(%ebp), %eax //Get x
_______ //x <<= 2
My book gives an answer of
sall $2, %eax
would
shll $2, %eax
also been the correct answer?
Question 2:
In layman’s terms what is the difference between SHR and SAR? My book says one is a logical shift (fills with zeroes) and other is arithmetic shift (fills with copies of the sign bit).
Fills what with 0’s/sign bits?
For example:
How would I know which one to use with the following assembly instructions?
movl 12(%ebp), %ecx //Get n
______ //x >>=n
Back of the book has the answer
sarl %cl, %eax
Please explain to me what would happen if we used shrl.
Thanks for your help with understanding this!!
salandshlare synonymous, they have identical opcodes.As
sardoes signed division by 2 rounding numbers towards negative infinity (-Inf), andshrdoes unsigned division by 2, probably engineers at Intel have decided to incorporate also bothsalandshleven if they are synonymous.Here’s an example what
shrandsardo:So
shrfills with zeroes, andsarfills with the sign bit.shris for unsigned divisions or bit shifts in general, andsarfor signed divisions.