# Logical instructions of 8051

As there were 24 arithmetic instruction sets,and there are 25 logical instructions in 8051. AND , OR, XOR , NOT …. These instructions come under logical instructions.

**ANL A, #14**_{H}** :**

This is AND instruction performed between the data of register A, and the 14_{H}.

A ← A *14_{H} , ^{ }the result is stored in A register, * is the symbol of and operation.

A | B | A*B |

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

The AND table shows that

- AND operation between any bit and zero is always
**zero.** - AND operation between any bit and one is the
**bit itself**the result.

Applications of AND operation.

- Reset the lower nibble without affecting higher nibble of a register A ( 0100 1011 ). Then we do an AND operation with A register. The bits we wanted to RESET should be AND with 0. The bits we wanted to keep unchanged , do AND operation with 1.

**ANL A, R**_{0}** :**

This is AND instruction similar to above, but this is AND operation between A register and R_{0} register. The result is stored in A register.

A ← A *R_{0}

**ANL A, 14**_{H}** :**

A ← A + [ 14_{H} ]

Here the AND is performed between A register content and content of 14_{H} location. The result is stored in A register.

A ← A *[ 14_{H} ]

**ANL A, @R**_{0}** :**

Here @R_{0} stands for the data of the location , stored in R_{0}.

**ANL 14**_{H}**, A :**

This is the same to **ANL A, 14**_{H}** , **but in this instruction the final result is stored in 14_{H }location.

[ 14_{H }] ← A * [ 14_{H} ]

**ANL 14**_{H}**, #14**_{H}** :**

[ 14_{H }] ← 14_{H} + [ 14_{H} ] ; both are different here.

**ORL A, #14 _{H} **

**ORL A, R**

_{0}**ORL A, 14**

_{H}**ORL A, @R**

_{0}**ORL 14**

_{H}, A**ORL 14**

_{H}**, #14**

_{H}These all are OR instructions, and all are similar to AND instruction types.

A | B | A+B |

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

The OR table shows that

- OR operation between any bit and zero is the
**bit itself**the result. - OR operation between any bit and one is always
**one**.

Applications of OR operation.

- SET the lower nibble without affecting higher nibble of a register A ( 0100 1011 ). Then we do an OR operation with A register. The bits we wanted to SET should be OR with 1. The bits we wanted to keep unchanged , do OR operation with zero.

**XRL A, #14 _{H} **

**XRL A, R**

_{0}**XRL A, 14**

_{H}**XRL A, @R**

_{0}**XRL 14**

_{H}, A**XRL 14**

_{H}**, #14**

_{H}This is X-OR operation, and the instruction are similar

A | B | A xor B |

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

The XOR table shows that

- XOR operation between any bit and zero is the
**bit itself**the result. - XOR operation between any bit and one is the
**complement of the bit**.

**ROTATE INSTRUCTIONS:**

These rotate instructions are important part of logical instructions of 8051, and these rotate instructions are classifies as

**RL A**

This instruction is to shift all the bits from LSB to all the way left of it. Thus named rotate left.

From the above figure, rotating left goes all up to MSB, and MSB value is assigned to LSB as well as Carry Flag, where the CF has lost its value.

**RR A**

This instruction is to shift all the bits from MSB to all the way right of it. Thus named rotate right.

From the above figure, rotating right goes all up to LSB, and LSB value is assigned to MSB as well as Carry Flag, where the CF has lost its value.

**RLC A**

This instruction is to shift all the bits from LSB to all the way left of it including the carry flag in it. Thus named rotate left with carry.

From the above figure, rotating left goes all up to CF, and then the value of CF is assigned to LSB. Here we don’t lose the data of the CF flag register.

**RRC A**

This instruction is to shift all the bits from MSB to all the way left of it including the carry flag in it. Thus named rotate right with carry.

From the above figure, rotating right goes all up to CF, and then the value of CF is assigned to MSB. Here we don’t lose the data of the CF flag register.

**Application of rotate instructions:**

Find whether a byte is even or odd?

To find this , we have to determine the value of LSB ( all other bits are multiple of 2 ) , if LSB is 1 then its odd number or else it’s even number.

Rotate left and let LSB come in CF, access the CF.

Then rotate back right to get actual data. To get the original value of CF use RRC and RLC.

Find any bit in a register !

Then rotate the bits, until the required bit gets in CF. access the CF.

CF is a part of the flag register which is SFR, and all the bits of CF are given or accessed by the programmer.

Swap higher nibble and lower nibble.

Rotate 4 times. Exclude CF in the loop.

**CLR A:**

Clears the each and every bit of A register.

A ← 00_{H}

**CPL A:**

It gives back the complement of the bit.

A ← A ( A bar )

**SWAP A:**

It swaps the higher nibble of the byte with lower nibble.

A_{Lower nibble } ↔ A_{Higher nibble}

These were 25 logical instruction sets of logical groups.

**NOP **

This is no operation, which means 8051 takes the instructions, decodes it and then analyzes it’s a null operation. This whole process consumes 1µ second. We can keep this time waste in a loop and increase it upto few seconds. This instruction can be used as a **delay **in 8051. And this is the software delay type.

Related Topics:

Logical instructions in 8086