6.3 Logic, Shift, and Rotate Instructions
Arithmetic treats a register as a number. Logic instructions can instead treat it as a row of independent switches. This is essential for hardware control registers, where one bit may enable a timer while another selects a device mode.
AND, OR, XOR, NOT, and TEST
Logic instructions apply a truth-table rule independently at every bit position:
ANDproduces 1 only when both input bits are 1.
ORproduces 1 when either input bit is 1.
XORproduces 1 when the input bits differ.
NOTinverts every bit and has only one operand.
TESTcalculates an AND result for flags but does not store that result.
A mask is a bit pattern chosen to change some positions while preserving others. The preserve value depends on the operation: AND preserves a bit with mask 1, while OR and XOR preserve it with mask 0.
Suppose AL=A5H, binary 10100101:
AND AL,0FHgives05H, clearing the high nibble.
OR AL,0FHgivesAFH, setting the low nibble.
XOR AL,0FHgivesAAH, toggling the low nibble.
AND, OR, XOR, and TEST clear CF and OF, then set SF, ZF, and PF from the logical result. AF becomes undefined. NOT changes no flags.
Shifts move bits; rotates return them
A shift moves every bit left or right. The last bit leaving the destination is copied into CF.
SHL(also namedSAL) shifts left, inserts 0 at bit 0, and can multiply an unsigned value by 2 when no significant bit is lost.
SHRshifts right and inserts 0 at the highest bit. It performs an unsigned division by 2 with the remainder bit in CF.
SARshifts right while repeating the old sign bit. It preserves the sign of a two’s-complement value and rounds negative odd values toward negative infinity.
A rotate keeps all register bits by returning the outgoing bit at the opposite side. ROL and ROR rotate within the register. RCL and RCR include CF as an extra ninth or seventeenth bit.
On the original 8086, a variable shift count comes from CL; an instruction may also shift by one directly. For a count greater than one, the operation repeats one-bit steps. CF contains the final outgoing bit. OF has a defined interpretation for a one-bit shift or rotate, but should not be relied on for larger counts.
Compare F0H under two right shifts:
SHR F0H,1inserts 0 and produces78H, which is unsigned 120.
SAR F0H,1repeats the sign bit and producesF8H, which is signed −8.
Both instructions expose the old low bit in CF, but they answer different numeric questions.
Logic and shifts prepare values, but programs must also choose which instruction executes next. Section 6.4 connects flags to comparisons, branches, and counted loops.