View Course Path

Logical instructions in 8051 – with example codes

In this article, we will be talking about the logical operations in the 8051 microcontroller. We would recommend that you go through our previous articles on data transfer and arithmetic instructions to better understand this topic.

List of logical instructions in 8051

Logical operations in 8051 perform bitwise operations between the accumulator and data stored in a memory location, register, or data given by the programmer. The result of a logical operation is stored in the accumulator itself. The table given below lists the logical operations which can be performed by the 8051. [A]=Accumulator; [Rn]= register in register bank of RAM space.

Operation Mnemonics Description
AND
ANL A, Rn [A]<-[A] AND [Rn]
ANL A, Address [A]<-[A] AND [Data at Address]
ANL A, @Rn [A]<-[A] AND [Data at Address in Rn]
ANL A, #data [A]<-[A] AND [Data]
ANL Address, A [Data at Address]<-[Data at Address] AND [A]
ANL Address, #data [Data at Address]<-[Data at Address] AND [Data]
OR
ORL A, Rn [A]<-[A] OR [Rn]
ORL A, Address [A]<-[A] OR [Data at Address]
ORL A, @Rn [A]<-[A] OR [Data at Address in Rn]
ORL A, #data [A]<-[A] OR [Data]
ORL Address, A [Data at Address]<-[Data at Address] OR [A]
ORL Address, #data [Data at Address]<-[Data at Address] OR [Data]
XOR
XRL A, Rn [A]<-[A] XOR [Rn]
XRL A, Address [A]<-[A] XOR [Data at Address]
XRL A, @Rn [A]<-[A] XOR [Data at Address in Rn]
XRL A, #data [A]<-[A] XOR [Data]
XRL Address, A [Data at Address]<-[Data at Address] XOR [A]
XRL Address, #data [Data at Address]<-[Data at Address] XOR [Data]
Clear CLR A [A]<-0
Complement CPL A [A]<-[A]’
Rotate left
RL A Shifts the value in accumulator to the left
RLC A Shifts the value in accumulator to the left through the accumulator
Rotate right
RR A Shifts the value in accumulator to the right
RRC A Shifts the value in accumulator to the right through the accumulator
Swap SWAP A Swaps the upper nibble of the accumulator with the lower nibble

AND operation 

The AND instructions compare the bits in the source and the destination operand and then store the result in the accumulator after performing the AND operation.

 

Opcode
Operand
Description
Size
Execution Time
Flags affected
Carry Overflow Auxilary carry
ANL
A, Rn Performs the AND operation on the data stored in the accumulator and the register Rn. The result is then stored in the accumulator 1 byte 12 clock cycles no no no
A, Address Performs the AND operation on the data stored at the given address and the accumulator. The result is then stored in the accumulator 2 bytes 12 clock cycles no no no
A, @Rn Uses the data stored in the register as an address and performs the AND operation on the data stored at that address and the accumulator. The result is then stored in the accumulator 1 byte 12 clock cycles no no no
A, #data Performs the AND operation on the data given by the programmer and the accumulator. The result is then stored in the accumulator 2 bytes 12 clock cycles no no no
Address, A Performs the AND operation on the data stored at the given address and the accumulator. The result is then stored at the given address 2 bytes 12 clock cycles no no no
Address, #data Performs the AND operation on the data stored at the given address and the data given by the programmer. The result is then stored at the given address 3 bytes 24 clock cycles no no no

Example

MOV A, #35H; Moves 35H into the accumulator 
ANL A, #0FH; Performs AND operation between the data stored in the accumulator and 0FH. 
;Then the result is stored in the accumulator (A=O5H)

OR Operation

The OR instructions compare the bits in the source and the destination operand and then store the result in the accumulator after performing the OR operation.

 

Opcode
Operand
Description
Size
Execution Time
Flags affected
Carry Overflow Auxilary carry
ORL
A, Rn Performs the OR operation on the data stored in the accumulator and the register Rn. The result is then stored in the accumulator 1 byte 12 clock cycles no no no
A, Address Performs the OR operation on the data stored at the given address and the accumulator. The result is then stored in the accumulator 2 bytes 12 clock cycles no no no
A, @Rn Uses the data stored in the register as an address and performs the OR operation on the data stored at that address and the accumulator. The result is then stored in the accumulator 1 byte 12 clock cycles no no no
A, #data Performs the OR operation on the data given by the programmer and the accumulator. The result is then stored in the accumulator 2 bytes 12 clock cycles no no no
Address, A Performs the OR operation on the data stored at the given address and the accumulator. The result is then stored at the given address 2 bytes 12 clock cycles no no no
Address, #data Performs the OR operation on the data stored at the given address and the data given by the programmer. The result is then stored at the given address 3 bytes 24 clock cycles no no no

Example

MOV A, #04H; Moves 04H into the accumulator 
ANL A, #30H; Performs AND operation between the data stored in the accumulator and 30H. 
;Then the result is stored in the accumulator (A=34H)

XOR Operation

These instructions compare the bits in the source and the destination operand and then store the result in the accumulator after performing the XOR operation.

 

Opcode
Operand
Description
Size
Execution Time
Flags affected
Carry Overflow Auxilary carry
XRL
A, Rn Performs the XOR operation on the data stored in the accumulator and the register Rn. The result is then stored in the accumulator 1 byte 12 clock cycles no no no
A, Address Performs the XOR operation on the data stored at the given address and the accumulator. The result is then stored in the accumulator 2 bytes 12 clock cycles no no no
A, @Rn Uses the data stored in the register as an address and performs the XOR operation on the data stored at that address and the accumulator. The result is then stored in the accumulator 1 byte 12 clock cycles no no no
A, #data Performs the XOR operation on the data given by the programmer and the accumulator. The result is then stored in the accumulator 2 bytes 12 clock cycles no no no
Address, A Performs the XOR operation on the data stored at the given address and the accumulator. The result is then stored at the given address 2 bytes 12 clock cycles no no no
Address, #data Performs the XOR operation on the data stored at the given address and the data given by the programmer. The result is then stored at the given address 3 bytes 24 clock cycles no no no
MOV A, #54H; Moves 54H into the accumulator
XRL A, #78H; Performs AND operation between the data stored in the accumulator and 78H. 
;Then the result is stored in the accumulator (A=2CH)

Clear operation
The CLR instruction is used to clear the data stored in the accumulator.

Example

MOV A, #50H; Moves 50H into the accumulator
CLR A; Clears the data stored in the accumulator
; the contents of the accumulator are now 00H

Compliment operation

The CPL instruction inverts all the bits stored in the accumulator.

Example

MOV A, # 0100 1100B; Loads the value (0100 1100B) into the accumulator
CPL A; Complements the value stored in the accumulator (A=1011 0011B)

Swap operation

Swaps the data in the upper nibble with the lower nibble of the accumulator.

Example

MOV A, #0110 0111B ;Loads the value (0110 0111B) into the accumulator
SWAP A             ;Swaps the data stored in the accumulator (A= 0111 0110B)

Rotate right operation

The RR instruction shifts the data stored in the accumulator to the right in a bit by bit fashion. The data in the LSB is shifted to the MSB.

Rotate_right_in_8051

Rotate right with carry operation

The RRC command performs the same operation but includes the carry bit in the operation as well. It is used for serial transmission of data to any external port.

RRC_IN_8051

Rotate left operation

The RL instruction shifts the data stored in the accumulator to the left in a bit by bit fashion. The data in the MSB is shifted to the LSB.

Rotate_left_in_8051

Rotate left with carry operation

The RLC command performs the same operation but includes the carry bit in the operation as well. It is used for serial transmission of data to any external port.

RLC_IN_8051

We hope that reading this article made you understand logical instruction in 8051. If you have any doubts regarding the same feel free to reach us through the comments section.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.