You are on page 1of 12

Logic Instructions

 Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG)


 Shift and Rotate instructions (SHL, SAL, SHR, SAR)

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.

Example: AND AL, BL


AND AX, [DI]

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

• OR instruction is used to set bit of a unknown binary number.

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.

• Example: XOR CH, DL ; CH= CH xor DL


XOR DX, [SI] ; DX is exclusive-Ored with the word
content of the data segment memory location addressed by SI.
4 Lecture materials on "Logic Instructions" By- Mohammed abdul kader, Lecturer, EEE, IIUC
Basic Logic Instructions (Continued)
Problem:
Develop a short sequence of instructions that sets the rightmost five bits, invert leftmost three
bits and clear middle four bits of DI without changing the remaining three bits of DI. Save the
result in SI.
Solution:
invert clear set
b15 b14 b13 b12 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b2 B0

1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 XOR with E000H


to invert
1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 AND with FC3FH
to clear
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 OR with 001FH to
set

XOR DI, E000H ; invert leftmost three bits


AND DI, FC3FH ; clear middle three bits
OR DI, 001FH ; set rightmost five bits
MOV SI, DI ; save result in SI

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.

Example: TEST DL, DH ; DL is ANDed with DH.


TEST AH, 4 ; AH is ANDed with 4.

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.

Example: NOT CH ; CH is one’s complemented


NOT BYTE PTR[BX] ; The byte contents of data segment memory
location addressed by BX are one’s complemented.

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.

Example: NEG CH ; CH is two’s complemented.


NEG AX; AX is two’s complemented.

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.

• Example: SHL AX, 1 ; AX is logically shifted left 1 place.


SHR BX, 12 ; BX is logically shifted right 12 places (12 times)
SAR SI, 2 ; SI is arithmetically shifted right 2 places.
SAL DATA, CL ; The contents of data segment memory location
DATA are arithmetically shifted left the number
of spaces specified by CL.
• Division by SHIFT instructions: A shift right always divides by 2 for each bit
position shifted. Two times right shifting divides by 4. For n times right shift the number
is divided by 2−𝑛 .

• Multiplication by SHIFT instructions: A shift left always multiplies by 2 for each


bit position shifted. Two times left shifting multiplies by 4. For n times left shift the
number is multiplied by 2𝑛 .

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

You might also like