
Chapter 3: General-Purpose Programming
63
24593—Rev. 3.09—September 2003
AMD 64-Bit Technology
SHR—Shift Right
SHLD—Shift Left Double
SHRD—Shift Right Double
The SH
x
instructions (including SHxD) perform shift
operations on unsigned operands. The SA
x
instructions operate
with signed operands.
SHL and SAL instructions effectively perform multiplication of
an operand by a power of 2, in which case they work as more-
efficient alternatives to the MUL instruction. Similarly, SHR
and SAR instructions can be used to divide an operand (signed
or unsigned, depending on the instruction used) by a power of
2.
Although the SAR instruction divides the operand by a power
of 2, the behavior is different from the IDIV instruction. For
example, shifting –11 (FFFFFFF5h) by two bits to the right (i.e.
divide –11 by 4), gives a result of FFFFFFFDh, or –3, whereas
the IDIV instruction for dividing –11 by 4 gives a result of –2.
This is because the IDIV instruction rounds off the quotient to
zero, whereas the SAR instruction rounds off the remainder to
zero for positive dividends, and to negative infinity for negative
dividends. This means that, for positive operands, SAR behaves
like the corresponding IDIV instruction, and for negative
operands, it gives the same result if and only if all the shifted-
out bits are zeroes, and otherwise the result is smaller by 1.
The SAR instruction treats the most-significant bit (msb) of an
operand in a special way: the msb (the sign bit) is not changed,
but is copied to the next bit, preserving the sign of the result.
The least-significant bit (lsb) is shifted out to the CF flag. In the
SAL instruction, the msb is shifted out to CF flag, and the lsb is
cleared to 0.
The SH
x
instructions perform
logical shift
, i.e. without special
treatment of the sign bit. SHL is the same as SAL (in fact, their
opcodes are the same). SHR copies 0 into the most-significant
bit, and shifts the least-significant bit to the CF flag.
The SHxD instructions perform a double shift. These
instructions perform left and right shift of the destination
operand, taking the bits to copy into the most-significant bit
(for the SHRD instruction) or into the least-significant bit (for
the SHLD instruction) from the source operand. These
instructions behave like SHx, but use bits from the source