2. Logic Instructions
To manipulate individual bits
Binary Value 0 treated as false
Binary Value 1 treated as true
In Assembly Language:
AND
OR
XOR
NOT
TEST
2
3. Truth Tables
3
a b a AND b a OR b a XOR b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
a NOT a
0 1
1 0
5. Syntax
AND destination, source
OR destination, source
XOR destination, source
Destination:
Stores result
Can be Register or Memory Location
Source:
May be a Constant, Register or Memory Location
5
6. Effects on Flags
SF, ZF, PF reflects the result
AF is undefined
CF, OF = 0
6
7. MASK
To modify only selective bits in destination, we
construct a source bit pattern known as MASK.
To choose mask, use following properties:
b AND 1 = b
b AND 0 = 0
b OR 1 = 1
b OR 0 = b
b XOR 0 = b
b XOR 1 = ~b (complement of b)
Where b represents a bit (0 or 1)
7
8. MASK
To choose mask, use following properties:
b AND 1 = b
b AND 0 = 0
b OR 1 = 1
b OR 0 = b
b XOR 0 = b
b XOR 1 = ~b (complement of b)
Where b represents a bit (0 or 1)
8
9. MASK
To choose mask, use following properties:
b AND 1 = b
b AND 0 = 0
b OR 1 = 1
b OR 0 = b
b XOR 0 = b
b XOR 1 = ~b (complement of b)
Where b represents a bit (0 or 1)
9
10. MASK
To choose mask, use following properties:
b AND 1 = b
b AND 0 = 0
b OR 1 = 1
b OR 0 = b
b XOR 0 = b
b XOR 1 = ~b (complement of b)
Where b represents a bit (0 or 1)
10
11. MASK
To choose mask, use following properties:
b AND 1 = b
b AND 0 = 0
b OR 1 = 1
b OR 0 = b
b XOR 0 = b
b XOR 1 = ~b (complement of b)
Where b represents a bit (0 or 1)
11
12. MASK
To choose mask, use following properties:
b AND 1 = b
b AND 0 = 0
b OR 1 = 1
b OR 0 = b
b XOR 0 = b
b XOR 1 = ~b (complement of b)
Where b represents a bit (0 or 1)
12
13. MASK
To choose mask, use following properties:
b AND 1 = b
b AND 0 = 0
b OR 1 = 1
b OR 0 = b
b XOR 0 = b
b XOR 1 = ~b (complement of b)
Where b represents a bit (0 or 1)
13
14. Contd..
1. The AND instruction:
- May be used to clear specific destination bits
while preventing the others.
- A 0 mask bit clears the corresponding
destination bit.
- A 1 mask bit preserves the corresponding
destination bit.
14
15. Example 1
Clear the sign bit of AL while leaving the other
bits unchanged.
Solution:
AND AL, 7Fh
Where 7Fh (0111 1111) is the mask.
Suppose: AL = 0D4h Binary: 1101 0100
→
AND 1101 0100
0111 1111
= 0101 0100 54h
→
15
16. Contd..
2. The OR instruction:
- May be used to set specific destination bits
while preventing the others.
- A 1 mask bit sets the corresponding destination
bit.
- A 0 mask bit preserves the corresponding
destination bit.
16
17. Example 2
Set the MSB and LSB of AL while preserving the
other bits.
Solution:
OR AL, 81h
Where 81h (1000 0001) is the mask.
Let
AL = 42h Binary: 0100 0010
→
OR AL, 81h OR 0100 0010
→
1000 0001
= 1100 0011 C3h
→
17
18. Contd..
3. The XOR instruction:
- May be used to complement specific
destination bits while preventing the others.
- A 1 mask bit complements the corresponding
destination bit.
- A 0 mask bit preserves the corresponding
destination bit.
18
19. Example 3
Change the sign bit of DX.
Solution:
XOR DX, 8000h
Where 80h (1000 0000) is the mask.
DX = 7230h 0111 0010 0011 0000
→
XOR 0111 0010 0011 0000
1000 0000 0000 0000
= 1111 0010 0011 0000 F230h
→
19
20. Nibbles
A nibble is a unit of data in computing that consists
of four bits. A bit is the smallest unit of data in a
computer and can have a value of either 0 or 1. A
nibble, being four bits, can represent 16 different
values (2^4 = 16), ranging from 0000 to 1111 in
binary notation.
Four bits that are at the higher-order positions
within a byte (or word). In a byte, which is typically
composed of eight bits, the high nibble consists of
the four bits at positions 4-7 (counting from right to
left, where the rightmost bit is bit 0).
20
21. Nibbles contd…
For example, consider the byte 1010 1101 in
binary notation. In this byte:
The high nibble is 1010, corresponding to bits 4-7.
The low nibble is 1101, corresponding to bits 0-3.
21
22. Converting an ASCII digit to a Number
ASCII code for digit “0-9” is “30h-39h”
AND AL, 0Fh ;Clears the high nibble.
How to convert decimal digit to ASCII code?
Add 48 to every decimal digit.
E.g. to convert 213 into ASCII code, just add 48 to 2 then
48 to 1 and lastly 48 to three
So we get 50 49 51
22
24. Lowercase to Uppercase letter
Lower case: 61h to 7Ah ( 97 to 122)
Uppercase: 41h to 5Ah (65 to 90)
24
25. Clearing a Register
MOV AX, 0 ;machine code 3 bytes
OR
SUB AX, AX ;machine code 2 bytes
OR
XOR AX, AX ;machine code 2 bytes
25
26. Testing a Register for zero
CMP CX, 0
Is same like:
OR CX, CX ;sets ZF = 1 if CX is 0
26
27. NOT Instruction
Performs the one’s complement operation on the
destination.
Syntax:
NOT destination
No effect on flags
Example: Complement the bit in AX:
NOT AX
27
28. TEST Instruction
Performs an AND operation without changing
destination i.e. only status flags updated.
Syntax:
TEST destination, source
MOV AL, F0h ; AL = 1111 0000
TEST AL, 0Fh ; = 0000 1111
28
29. TEST Instruction
Performs an AND operation without changing
destination i.e. only status flags updated.
Syntax:
TEST destination, source
Effects on flags:
SF, ZF and PF reflects the results
AF is undefined
CF, OF = 0
29