8051 Microcontroller Logical Operations
8051 Microcontroller Logical Operations
8051 Microcontroller Logical Operations
Logical Operations
Prepared by : A. B. Modi
Target Audience : 5th Semester Students
Learning Objectives
After learning this chapter, Students should be able to:
➢ Describe and Use byte-level AND, OR, XOR and NOT boolean instructions.
➢ Describe and Use bit-level AND, OR, XOR and Not boolean instructions.
➢ Describe and Use bit-level set, clear and data-moving instructions.
➢ Describe and Use Rotate instructions.
➢ Use the A register nibble-swapping instruction.
Introduction
➢ Data and SFRs may be manipulated using byte opcodes.
➢ Many of the SFRs, and a unique internal RAM area that is bit addressable,
may be operated on the individual bit level.
➢ Bit operators are used when quick response is required as it makes the
program compact.
➢ Boolean Opcodes : ANL, ORL, XRL and CPL
➢ Rotate opcodes : RL, RLC, RR, RRC, SWAP
Byte-Level Logical Operations
➢ Can be used with all types of addressing modes for the source of data
bytes.
➢ Result can be stored on A or a direct address in internal RAM.
➢ These are called “byte-Level Logical operations” because the entire byte is
affected.
ANL A, #n ;AND each bit of A with the same bit of immediate number n,
Result is placed in A.
ANL A, add ;AND each bit of A with the same bit of the direct RAM address,
result is placed in A
ANL A, Rr ;AND each bit of A with the same bit of register Rr, result is placed
In A.
ANL A, @Rr ;AND each bit of A with the same bit of the contents of RAM
Address in Rp, result is placed in A.
ANL add,A ;AND each bit of A with the same bit of the direct RAM address,
result is placed in direct RAM add.
ANL add, #n ;AND each bit of A with the same bit of the immediate number,
result is placed in direct RAM add.
ORL A, #n ;OR each bit of A with the same bit of immediate number n,
Result is placed in A.
ORL A, add ;OR each bit of A with the same bit of the direct RAM address,
result is placed in A
ORL A, Rr ;OR each bit of A with the same bit of register Rr, result is
placed in A.
ORL A, @Rr ;OR each bit of A with the same bit of the contents of RAM
Address in Rp, result is placed in A.
ORL add,A ;OR each bit of A with the same bit of the direct RAM address,
result is placed in direct RAM add.
ORL add, #n ;OR each bit of A with the same bit of the immediate number,
result is placed in direct RAM add.
XRL A, #n ;XOR each bit of A with the same bit of immediate number n,
Result is placed in A.
XRL A, add ;XOR each bit of A with the same bit of the direct RAM address,
result is placed in A
XRL A, Rr ;XOR each bit of A with the same bit of register Rr, result is
placed in A.
XRL A, @Rr ;XOR each bit of A with the same bit of the contents of RAM
Address in Rp, result is placed in A.
XRL add,A ;XOR each bit of A with the same bit of the direct RAM address,
result is placed in direct RAM add.
XRL add, #n ;XOR each bit of A with the same bit of the immediate number,
result is placed in direct RAM add.
CLR A ;clear each bit of A register to 0
Many of these byte-level operations use a direct address, which can include
the port SFR addresses, as a destination. The normal source of data from a
port are the port pins; the normal destination for port data is the port latch.
When the destination of a logical operation is the direct address of a port, the
latch register, not the pins, is used both as source for the original data and
then the destination for the altered byte of data.
Any port operation that must first read the source data, logically operate on it,
and then write it back to the source must use latch.
Logical operations that use the port as a source, but not as a destination, use
the pins of the oprt as the source of the data.
MOV A, #0FFh
MOV R0, #77h
ANL A,R0
MOV 15h, A
CPL A
ORL 15h, #88h
XRL A, 15h
XRL A, R0
ANL A, 15h
ORL A, R0
CLR A
XRL 15h, A
XRL A, R0
Bit - Level Logical Operations
➢ Certain internal RAM and SFRs can be addressed by their byte addresses
or by the address of each bit within a byte.
➢ Bit addressing is very convenient when you wish to alter a single bit of a
byte.
➢ Internal RAM byte addresses 20h to 2Fh are both byte and bit
addressable.
Internal RAM Bit Addresses
Byte Bit Byte Bit Whole bytes do not
Address Addresses Address Addresses have to be used up
to store one or two
20h 00h - 07h 28h 40h - 47h bits of data.
RLC A ;Rotate the A register and the carry flag one bit position to the left
RRC A ;Rotate the A register and the carry flag one bit position to the right