5-Logic Instructions
5-Logic Instructions
5-Logic Instructions
Course Instructor
Mohammed Abdul kader
Lecturer, EEE, IIUC
Basic Logic Instructions
• AND
• OR
• Exclusive OR (XOR)
• NOT
• TEST
• NEG
AND Instruction
• AND operation performs logical Multiplication.
• The AND operation clears bits of a binary number. The task of clearing a bit in a binary
number is called masking.
• AND instruction uses any addressing mode except memory to memory and segment
register addressing.
2 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
Basic Logic Instructions (Continued)
OR Instruction
• OR operation performs logical addition and is often called the Inclusive-OR
function
Example:
OR AH, BL ; AH= AH or BL
OR DX, [BX] ; DX is Ored with the word contents of data
segment memory location addressed by BX
3 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
Basic Logic Instructions (Continued)
Exclusive-OR Instruction
• The exclusive-OR instruction produces 1 when input logics are different otherwise it
produces 0. Because of this Exclusive-OR is sometimes called a comparator.
• The exclusive-OR instruction is useful if some bits of a register or memory location must be
inverted without changing the other bits.
5 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
Basic Logic Instructions (Continued)
TEST Instruction
• TEST instruction performs the AND operation. The difference is that the AND
instruction changes the destination operand, whereas the TEST instruction does not.
• The TEST instruction functions in the manner as a CMP instruction. The difference is
that the TEST instruction normally a single bit, whereas the CMP instruction tests the
entire byte or, word.
• It only changes zero flag (Z) bit. The zero flag (Z) is a logic 1 (indicating a zero result)
if the bit under test is a zero and Z=0 (indicating a nonzero result) if the bit under test
is not zero.
• Usually the TEST instruction is followed by either the JZ (jump if result zero, Z=1) or,
JNZ (jump if result not zero, Z=0) instruction.
Problem: (a) Write an instruction set to test the rightmost and leftmost bit position of AL
register. The program should jumps to the operand address RIGHT if right most bit of AL is
set and jumps to the address LEFT if leftmost bit of AL is set.
6 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
NOT Basic Logic Instructions (Continued)
NOT instruction performs logical inversion (invert all bits of a byte or word) or find the
one’s complement.
NEG
NEG instruction performs arithmetic sign inversion or, the two’s complement. The
arithmetic sign of a signed number changes from positive to negative or from negative to
positive by NEG instruction.
7 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
SHIFT and ROTATE instructions
SHIFT and ROTATE instructions are used to shift or rotate any memory data or register. The
instructions mostly use to control I/O devices.
SHIFT Instruction
• Shift instructions position or move numbers to the left or right within a register or memory
location.
• They also perform simple arithmetic such as multiplication by powers of 2+𝑛 (𝑙𝑒𝑓𝑡 𝑠ℎ𝑖𝑓𝑡) and
division by powers of 2−𝑛 𝑟𝑖𝑔ℎ𝑡 𝑠ℎ𝑖𝑓𝑡 .
• The microprocessor instruction set contains four different shift instruction.
Two logical shift : (a) Shift operand bits left (SHL)
(b) Shift operand bits right (SHR)
Two arithmetic shift: (c) Shift arithmetic Left (SAL)
(d) Shift arithmetic Right (SAR)
• Logical shifts (SHL and SHR): The logical shifts move a zero into the rightmost bit position
for a logical left shift and a 0 into the leftmost bit position for a logical right shift. MSB is shifted
to CF in case of SHL and LSB shifted to CF in case of SHR.
8 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
SHIFT and ROTATE instructions (continued)
• Arithmetic shifts (SAL and SAR): The arithmetic left shift and logical left shift are
identical. The arithmetic right shift and logical right shift are different because the arithmetic
right shift copies the sign bit through the numbers, where as logical right shift copies a 0
through the numbers.
• LogicalVS arithmetic shift: Logical shift operations function with unsigned numbers
and arithmetic shifts function with signed numbers. Logical shifts multiply or divide
unsigned data, and arithmetic shifts multiply or divide signed data.
9 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
SHIFT and ROTATE instructions (continued)
• Two different modes used in shift counting: One modes uses an immediate shift count
and other uses register CL to hold the shift count. Note that CL must hold the shift
count.
10 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
SHIFT and ROTATE instructions (continued)
ROTATE instruction
• Rotate instructions position binary data by rotating the information in a register or memory
location, either from one end to another or through the carry flag.
• There are four available rotate instructions:
Rotate through carry left (RCL)
Rotate out of carry left (ROL)
Rotate through carry right (RCR)
Rotate out of carry right (ROR)
11 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
SHIFT and ROTATE instructions (continued)
ROTATE instruction (Continued)
• A rotate count can be immediate or located in register CL.
• Example: ROL SI, 14 ; SI rotates left (out of carry) 14 places.
RCL BL, 6 ; BL rotates left through carry 6 places.
RCR AH, CL ; AH rotates right through carry the number of places
specified by CL.
ROR WORD PTR[BP], 2 ; The word contents of the stack segment memory
location addressed by BP rotate right (out of carry) 2 places.
Problem:
(a) AX=0F07H and BX=6644H, find the value of AX and BX after the execution following
instructions-
MOV CL, 4;
RCL AX, CL;
ROR BX, CL
(b) AX=0F07H and BX=6644H, find the value of AX and BX after the execution following
instructions-
SHL AX, 4
SAR BX, 3
12 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC