Professional Documents
Culture Documents
LESSON 7 - Logic Instructions
LESSON 7 - Logic Instructions
NEG Instruction – the NEG (Negate) instruction converts the specified operand to its 2’s complement
equivalent and the result returned to the operand location. This is, in effect, reversing the sign of an integer.
Format: NEG D
Action D 0 – [D]
Destination Example
register NEG AX
MM NEG byte ptr [BX]
Pointers:
1. Attempting to negate an operand having a value of zero causes no change to the operand and
resets the carry flag (CF = 0).
2. Attempting to negate an operand having a value of either 80H or 8000H causes no change to
the operand and sets the overflow flag (OF = 1).
3. Segment registers cannot be used as the destination operand. Therefore, the instruction NEG
CS is invalid.
4. If the destination operand is a memory location, the prefix byte ptr or word ptr should appear
after the NEG instruction to denote the data size of the destination operand.
Example:
Determine the value of AL and the value of the flags (assume all flags are initially 0) following
the instruction sequence:
MOV AL, 05H
NEG AL
AL contains:
0000 0000
- 0000 0101
1111 1011 = FBH = -5
The flags are affected as follows:
CF = AF = SF = 1 PF = ZF = OF = 0
CMP Instruction – the CMP (Compare) instruction subtracts the source operand from the destination operand.
It then discards the result but it updates the values of all the status flags.
Format: CMP D, S
Action: [D] – [S]
Logic instructions include the logic operations AND, OR, XOR and NOT. Also included in this group is the TEST
instruction. The TEST instruction is somewhat similar to the CMP instruction where flags are modified but the
operands are not altered.
I. AND Instruction – the Logical AND instruction logically ANDs the source and the destination operands
and stores the result in the destination operand.
Format: AND D, S
Action: [D] + [S] D
Destination Source Example
register register AND BX, CX
register MM AND DX, [BP + SI]
MM register AND BETA, CX
register immediate AND BX, 0015H
MM immediate AND byte ptr BETA, 12H
Pointers:
1. The AND instruction does not allow both source and destination operands to be memory
locations (no memory to memory AND). Therefore, the instruction AND SET, FILE is invalid.
2. Both source and destination operands should be of the same data size. The instruction AND
AH, BX is therefore invalid.
3. The destination operand cannot be immediate data. Therefore, the instruction AND 1800H,
CX is invalid.
4. CP and OF are always 0 after execution while AF is undefined.
5. As with the MOV instruction, if the destination operand is a memory location and the source
operand is immediate data, the prefix byte ptr or word ptr should appear after the AND
instruction to denote the data size of the immediate operand.
6. The AND instruction can be used to clear out or zero out certain bits in a byte or word. In order
to do this, simply logically AND a bit mask pattern and the byte or word concerned. A bit mask
pattern contains a pattern of 1’s and 0’s. A 0 in a bit position will cause that bit position to be
zeroed out, while a 1 in a bit position will not change the value.
Example:
MOV AL, 05H
MOV BL, FEH
AND AL, BL
AL = 05H
BL = FEH
AL = 0 0 0 0 0 1 0 1
BL = 1 1 1 1 1 1 1 0 (mask)
AL = 0 0 0 0 0 1 0 0
bit 0 is zeroed out, the rest of the bits remain the same.
Flags: CF, OF, PF, ZF, SF, are all equal to 0. AF is undefined.
II. OR Instruction – the Logical OR instruction logically ORs the source and the destination operands and
stores the result in the destination operand.
Format: OR D, S
Action: [D] + [S] D
Destination Source Example
register register OR BX, CX
register MM OR DX, [BP + SI]
MM register OR BETA, CX
register immediate OR BX, 0015H
MM immediate OR byte ptr BETA, 12H
Pointers:
1. The OR instruction does not allow both source and destination operands to be memory
locations (no memory to memory OR). OR SET, FILE is therefore invalid.
2. Both source and destination operands should be of the same data size. The instruction OR AH,
BX is therefore invalid.
3. The destination operand cannot be immediate data. Thus, the instruction OR 1800H, CX is
invalid.
4. CP and OF are always 0 after execution while AF is undefined.
5. As with the MOV instruction, if the destination operand is a memory location and the source
operand is immediate data, the prefix byte ptr or word ptr should appear after the OR
instruction to denote the data size of the immediate operand.
6. The OR instruction can be used to set a certain bit to 1 in a byte or word. In order to do this,
simply logically OR a bit mask pattern and the byte or word concerned. A bit mask pattern
contains a pattern of 1’s and 0’s. A 1 in a bit position will cause that bit position to be set to 1,
while a 0 in a bit position will not change the value.
Example:
MOV CL, 05H
MOV DL, 80H
OR CL, DL
CL = 0 0 0 0 0 1 0 1
DL = 1 0 0 0 0 0 0 0 (mask)
CL = 1 0 0 0 0 1 0 1
IV. NOT Instruction – the Logical NOT instruction performs a 1’s complement on the operand.
Format: NOT D
Action: [D] D
Destination Example
register NOT AX
MM NOT byte ptr BETA
Pointers:
1. The destination operand cannot be immediate data. Thus, the instruction NOT 1800H is
invalid.
2. Flags are not affected.
3. If the destination operand is a memory location, the prefix byte ptr or word ptr should appear
after the NOT instruction to denote the data size of the immediate operand.
Example:
Perform the following operations. Each instruction is dependent on one another. Assume all
flags are initially 0:
MOV BX, D32BH
MOV CX, 1056H
NOT BX
NOT CH
Answer:
MOV BX, D32BH
MOV CX, 1056H
BX = D32BH
CX = 1056H
NOT BX
[BX] = D32BH = 1101 0011 0010 1011
[BX]’ = 0010 1100 1101 0100
NOT CH
[CH] = 20H = 0001 0000
[CH]’ = 1110 1111
V. TEST Instruction – the TEST instruction logically ANDs the source and the destination operands. The result
is discarded but the values of the status flags are updated.
Format: TEST D, S
Action: [D] x [S]
Pointers:
1. The TEST instruction does not allow both source and destination operands to be memory
locations (no memory to memory AND). TEST SET, FILE is therefore invalid.
2. Both source and destination operands should be of the same data size. The instruction TEST
AH, BX is therefore invalid.
3. CP and OF are always 0 after execution while AF is undefined.
4. As with the MOV instruction, if the destination operand is a memory location and the source
operand is immediate data, the prefix byte ptr or word ptr should appear after the TEST
instruction to denote the data size of the immediate operand.
Example:
Perform the following operations. Each instruction is dependent on one another. Assume all flags
are initially 0:
MOV SI, 2376H
MOV DI, 1941H
TEST SI, DI
Answer:
MOV SI, 2376H
MOV DI, 1941H
SI = 2376H
DI = 1941H
TEST SI, DI
[SI] – 0010 0011 0111 0110
[DI] – 0001 1001 0100 0001
0000 0001 0100 0000
Flags:
CF, OF, PF, SF and ZF are all equal to 0, while AF is undefined.
Logic Instruction Program Tracing Example:
Trace the following program. Assume the following: all flags and registers (unless specified otherwise)
are initially zero, DS = 2000H, SS = 2001H, the label ALPHA = 001CH.
MOV BX, ALPHA
MOV SP, BX
NEG BX
AND BX, [AX]
OR BX, 0020H
CMP BX, SP
MOV CX, [SP]
TEST BX, CX
NOT AX
XOR AL, CL
Solution:
1. The instruction MOV BX, ALPHA simple loads the contents of the memory location whose
offset is ALPHA to BX.
Physical Address = DS * 10H + ALPHA
= 200000H + 001CH
= 2001CH
Since [2001CH] = 18H and [2001DH] = 00H, the contents of BX will be 0018H. The flags are
not affected (all flags are still zero).
2. The instruction MOV SP, BX copies the contents of BX to SP, SP will therefore be equal to
0018H. Flags are not affected.
3. The instruction NEG BX will simply perform 2’s complement on the contents of BX.
The content of BX is 0018H. The two’s complement of 0018H is FFE8H.
The new contents of BX will be FFE8H. The Flags will be:
SF= 1 ZF = 0 PF = 1 CF = 0 AF =0 OF = 0
4. The instruction AND BX, [AX] logically ANDS the contents of BX to the contents of the
memory location specified by AX and stores the results in BX.
Physical Address = DS * 10H + AX
= 20000H + 0000H
= 20000CH
Since [20000H] = AAH and [20001H] = 12H, the operation is as follows:
BX = FFE8H = 1111 1111 1110 1000
[AX] = 12AAH = 0001 0010 1010 1010
0001 0010 1010 1000
The new contents of BX will be 12A8H. The Flags will be:
SF= 0 ZF = 0 PF = 0 CF = 0 AF =X OF = 0
5. The instruction OR BX, 0020H will logically OR 0020H from the contents of register BX.
BX = 12A8H
- 0020H
1288H
The new contents of BX will be 1288H. The Flags will be:
SF= 0 ZF = 0 PF = 0 CF = 0 AF =X OF = 0
6. The instruction CMP BX, SP will merely subtract the contents of SP from the destination,
BX and discard the results and update the flags.
BX = 12A8H
- SP = 0018H
1290H
The new contents of BX will be 1290H. The Flags will be:
SF= 0 ZF = 0 PF = 1 CF = 0 AF =0 OF = 0
7. The instruction MOV CX, [SP] moves the contents of the memory location whose offset is
pointed to by BP to CX. Since SP is used, the SS register is addressed instead of DS.
Top of the Stack = SS * 10H + SP
= 20010H + 0018H
= 20028H
Since [20028H] = 91H and [20029H] = 9FH, the new contents of CX will be 9F91H. The flags
are not affected and will remain as (from previous instruction).
SF= 0 ZF = 0 PF = 1 CF = 0 AF =0 OF = 0
8. The instruction TEST BX, CX will logically ANDs the contents of CX from BX and discard the
results and update the flags.
BX = 12A8H = 0001 0010 1010 1000
(mask) CX = 9F91H = 1001 1111 1001 0001
0001 0010 1000 0000
The new contents of AX will be 0C6AH. The Flags will be:
SF= 0 ZF = 0 PF = 0 CF = 0 AF =X OF = 0
9. The instruction NOT AX performs 1’s complement on operand and stores the results to
AX.
AX = E11FH = 1110 0001 0001 1111
AX’ = 0001 1110 1110 0000
= 1EE0H
The new contents of AX will be 1EE0H. The Flags will be:
SF= 1 ZF = 0 PF = 0 CF = 0 AF =1 OF = 0
10. The instruction XOR AL, CL will logically XORs the contents of AL to CL and stores the
results in AL.
AL = E0H = 1110 0000
+ CL = 91H = 1001 0001
71H = 0111 0001
The new contents of AL will be 71H. The Flags will be:
SF= 0 ZF = 0 PF = 1 CF = 0 AF =X OF = 0