X86 Assembly/Shift and Rotate
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Logical Shift Instructions
In a logical shift instruction, the bits that slide off the end disappear, and the spaces are always filled with zeros. Logical shift is best used with unsigned numbers.
shr arg
Logical shift arg to the right.
shl arg
Logical shift arg to the left.
[edit] Arithmetic Shift Instructions
In an arithmetic shift, the bits that "slide off the end" disappear. The spaces are filled in such a way to preserve the sign of the number being slid. For this reason, Arithmetic Shifts are better suited for signed numbers in two's complement format.
sar arg
Arithmetic shift arg to the right. Spaces are filled with sign bit (to maintain sign of original value).
sal arg
Arithmetic shift arg to the left. Spaces are filled with zeros.
[edit] Shift With Carry Instructions
A Logical Shift, and the bit that slides off the end goes into the carry flag.
scr arg
Shift with carry the arg to the right.
scl arg
Shift with carry the arg to the left.
[edit] Rotate Instructions
In a rotate instruction, the bits that slide off the end of the register are fed back into the spaces.
ror arg
Rotate arg to the right.
rol arg
Rotate arg to the left.

