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 src, dest | GAS Syntax |
| shr dest, src | Intel syntax |
Logical shift arg to the right, src bits.
| shl src, dest | GAS Syntax |
| shl dest, src | Intel syntax |
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 src, dest | GAS Syntax |
| sar dest, src | Intel syntax |
Arithmetic shift arg to the right. Spaces are filled with sign bit (to maintain sign of original value).
| sal src, dest | GAS Syntax |
| sal dest, src | Intel syntax |
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 src, dest | GAS Syntax |
| scr dest, src | Intel syntax |
Shift with carry the arg to the right.
| scl src, dest | GAS Syntax |
| scl dest, src | Intel syntax |
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 src, dest | GAS Syntax |
| ror dest, src | Intel syntax |
Rotate arg to the right.
| rol src, dest | GAS Syntax |
| rol dest, src | Intel syntax |
Rotate arg to the left.
[edit] Number of arguments
Unless stated, these instructions can take either one or two arguments. If only one is supplied, it is assumed to be a register or memory location and the instruction is repeated once. shrl $1, %eax is equivalent to shrl %eax (GAS syntax).