# Arithmetic instructions in 8051

There are 111 instructions present in the 8051 microcontroller, which includes arithmetic instructions of 8051 and classified as

• Arithmetic instruction set.
• Logical instruction set.
• Boolean instructions ( bit operations ).
• Data transfer instruction set.
• Branch control group.
• Instruction set branch control SJMP LJMP AJMP.

Arithmetic instruction set:

There are 24 arithmetic instructions in 8051 microcontroller, but there are 64 opcodes. Reason for more opcodes is these 24 operations affect flag registers ( carry flag, auxiliary flag and overflow flag ).

A  ← A + 14H
Here we are adding number 14H ( H stands for hexadecimal number ) to the value of A register and the final value is stored in A register. Addition result can be a 9-bit number, then the 9th bit is represented by carry flag. If CF = 1, then there was a carry out of MSB. Here A register was used, if any other register is used, then also result would be stored in A register, as it is a accumulator.

A ←A + data of 14H
Data in the location of 14H  is added with the value of A register and the final result is stored in A register.

ADD A ,Rr { r starts from 0 to 7 }:
A  ← A + Rr
Register A is added with the general purpose register ( defined register ), there are 8 general purpose registers in 8051.

ADD A, @RP { only R0 and R1 }:
A  ← A + value of the location, stored in the RP register.

The answer of this addition would be 5H + 3H , which is 8H.

These two are similar instructions. Till we have seen 8 arithmetic instructions of 8051 ( normal addition and addition with a carry ).

A  ← A + 14H + CF CF = previous data stored in carry flag.
This instruction comes into play when we perform additions with carry.

2 F
+ 1 3
In this addition first F + 3 is added, which is 12 , 1 is carry which is stored in the carry flag. Next 8051 adds 2 + 1 + 1( carry stored in the carry flag ).

This is similar to the above instruction, but instead of adding directly 14H ,here the data from that location is accessed. Final result is stored in A register.

SUBB A, #14H:

This stands for subtraction with a borrow. This is similar to addition with a carry.

A ← A – 14H – borrow.

Where is this borrow is stored ?

Either borrow or carry, both are stored in a carry flag. If the 8051 is performing addition, flag stores carry, and if 8051 performs subtraction it stores borrow. Make sure we clear the carry flag before performing subtraction.

CLR C;
SUBB A, #15H ;

If the carry flag is not cleared, data stored in the carry flag will bring us a wrong result.

[ SUBB A, 14H ] [ SUBB A, @R0 ] [SUBB A, R0 ]

These four instructions are similar to addition instructions.

INC A :

This instruction increments the value of A.
What is A is FF ?
Then after incrementing A becomes 00 , it’s a circular loop.

INC R0 :
Increments the register value.

INC 25H :
Increment the data stored in this location.

INC @R0 :
Increments the data of the location, stored in this register.

INC DPTR :
It is a 16-bit operation. It increases the data pointer SFR.

DEC A , DEC R0 ,DEC 25H ,DEC @R0

These four operations are for decrementing the values.

MUL A B :
This operation multiplies A and B registers. Multiplication of two 8-bit numbers is a 16-bit number and thus A register can’t hold the whole result. There are two bytes in the result, and thus they are divided as higher byte and lower byte. We don’t always get a 16-bit result, there are chances of even getting an 8-bit result ( then it would be part of a lower byte ). A register always stores the result , so lower byte is stored in A register and higher byte is stored in B register.

BHigher byte . ALower byte  ←  A * B

DIV A B:
This is a division instruction, allowed only on A and B registers. 8051 don’t give us division results in fraction. 8051 does not deal with floating numbers.
BRemainder . AQuotient  ←  A / B

A quotient is an important result of a division , so it is stored in A and an optional result in B register.

Division with zero ?
Then A and B registers are filled with garbage value and this thing is indicated by an overflow flag. If overflow flag = 1, A and B have garbage values.

This is a common instruction in many other microcontrollers and microprocessors. Whenever an user gives numbers for operations numbers are given in decimal form ( not hexadecimal ), but the 8051 performs operations considering those numbers in hexadecimal form. Once the 8051 performs the operation it is sent to DA A for conversion to decimal form.

LN stands for lower nibble, HN stands for higher nibble.

CF stands for carry flag , AC stands for auxiliary flag.

Example 1:

Add 20 + 30 { user has given these numbers } ( decimal form )

LN ( 0 + 0 ) = 0 , LN is not > 9  and Auxiliary carry is not equal = 1

HN ( 2 + 3) = 5 , HN is not > 9  and Carry flag is not equal = 1

Both the conditions didn’t satisfy , so 50 is the final answer.

Example 2:

Add 25 + 25 { user has given these numbers } ( decimal form )

LN ( 5 + 5 ) = A , LN > 9  and Auxiliary carry is not equal = 1 , so add 06 to LN

HN ( 2 + 2) = 4 , HN is not > 9  and Carry flag is not equal = 1

One of the conditions is satisfied , so 4A is the not the final answer.  Add 06 to 4A , so we get 50, which is the correct decimal form.

Example 3:

Add 50 + 50 { user has given these numbers } ( decimal form )

LN ( 0 + 0 ) = 0 , LN is not > 9  and Auxiliary carry is not equal = 1

HN ( 5 + 5) = A , HN > 9  and Carry flag is not equal = 1, so add 60 to HN

One of the conditions is satisfied , so A0 is not the final answer.  Add 60 to A0 , so we get 100, which is the correct decimal form.

Example 4:

Add 99 + 99 { user has given these numbers } ( decimal form )