Shift Operations
Shifting the contents of a word or byte left or right can also
be a useful operation. With binary numbers, left shifts are the
the same as multiplying by two for each position shifted left.
Likewise, shifting right is like dividing by two (assuming it
is an unsigned integer!)
Logical Shifts
The simplest type of shift instruction is to shift zeroes into the
vacated positions and to throw away the bits shifted out the other
end. (However, a copy of the thrown away bit is stored in
the Carry Flag!)
The instructions are:
The last bit shifted out is stored in the Carry Flag so that it
can be tested. The form is:
shl/shr mem/reg, count/CL ; unitl the 80386, the only constant was a 1
To shift more than one position, you had to use the CL register:
mov CL, 4
shl AX, CL
Examples
1010 1010 1010 1010
SHR 1
0101 0101 0101 0101 => 0 in Carry Flag
0001 0010 0011 0100
SHR 4 ; Remember to use the CL register!!!
0000 0001 0010 0011 => 0 in Carry Flag ; the 0 came from bit 3
Shift Arithmetic
The is also a arithmetic shift, where the only difference
is during a right shift, inside of the high order bit becoming
zero, it keeps the value that it had, which is useful with signed
integers.
Examples
1010 1010 1010 1010
SAR 1
0101 0101 0101 0101 => 0 in Carry Flag
0001 0010 0011 0100
SAR 4 ; Remember to use the CL register!!!
0000 0001 0010 0011 => 0 in Carry Flag ; the 0 came from bit 3
Rotates
The last shifts are called rotates. In this type of shift,
the bit the comes out one side is put back into the other size.
Examples
1010 1010 1010 1010
ROR 1
0101 0101 0101 0101 => 0 in Carry Flag and bit 15
0001 0010 0011 0100
ROR 4 ; Remember to use the CL register!!!
0100 0001 0010 0011 => 0 in Carry Flag ; the 0 came from bit 3
Previous |
Next
©2004, Gary L. Burt