# CHAPTER # 5 - ARITHMETIC INSTRUCTIONS IN 8051

CHAPTER # 5 - ARITHMETIC INSTRUCTIONS IN 8051

After completing this chapter, you will be able to perform...

• Unsigned Data and its possible range
• Addition and subtraction for unsigned data
• Binary Coded Decimal system of data representation
• Packed and Unpacked binary coded data
• Addition and subtraction on binary coded data
• Unsigned data multiplication and division
• Signed data and its possible range
• Code signed data arithmetic instructions
• Carry and overflow examples and problems.

What are Unsigned Numbers?:

The data in which all the bits are used to represent data. No bits are set for the positive or negative sign. The operand can be between 00 - FFH or 0 to 255 decimal, for 8-bit data.

SECTION I - ADDITION OF UNSIGNED NUMBERS:

The accumulator register 'A' must be involved in order to add numbers. The format of the ADD instruction is:

ADD A,source                              ;ADD the value inside source to the value inside accumulator and save the result back in accumulator register i.e. A=A+source

The ADD instruction add the source operand with the destination operand. These are the two operands which the ADD instruction is used. The destination operand is always in register A while the source operand can be a register accumulator A, immediate data or in memory. The memory to memory arithmetic operations are not allowed in 8051 assembly language programming. Flag register contains 'Auxiliary Carry' AC flag, 'Carry Flag' CY and 'Parity Flag' PF. The ADD instruction could change any of the above mentioned flags according to its operations of operands involved.

Example # 1:

Lets see how the flag register is affected:

MOV A, #80H;Load the value 80H into the accumulator register A
ADD A, #80H            ;Add the value 80H with the value in accumulator 0F5H and save the result in register A
80H   10000 0000
+ 80H+ 1000   0000
100H    0000 0000

The destination register accumulator contains 00. The  following flags are affected:

Carry Flag CY = 1Out from D7, there is a carry
Parity Flag PF = 1An even number of once is zero
Auxiliary Carry AC = 0No carry from D3 to D4

How to Add Individual Bytes in 8051:

It is already clear that, the maximum value that an 8-bit register can hold is FFH in hex. The carry flag should be checked after the addition of each operand, in order to calculate the sum of any number of operands.

Example # 2:

Lets assume that the RAM locations 30H to 44H have some values given below. The code program will find the sum of the values. When the program ends, the register A should contain the low byte and R2 contain the high byte.

 30H = 8A 31H = 9C 32H = 3E 33H = 9D 34H = FF

CLR A;Clear the accumulator register A
MOV R5, A;Clear the register R2
AGAIN: ADD A, @R0     ;Add the byte which is pointed to by R0 register
JNC NEXT;If no carry, then do not accumulate carry, ignore and jump
INC R5;If there is a carry increment R7 by 1 to keep track or carries
NEXT: INC R0;Increment the R0 pointer
DJNZ R3, AGAIN;Repeat the loop again and again, until R3 is zero
END;End of the code program

When R0=30H and R3=5, Carry flag=0:

• In the instruction "AGAIN: ADD A, @R0", the 8AH value is added to the accumulator register A. The accumulator contains value 0H which is added to the value 8AH i.e A=0+8AH=8AH. The carry flag is zero. i.e CY=0 and the instruction R5 is skipped, since there is no carry so R5=00, and the counter decrement by 1 value and jump to the label 'AGAIN' by the instruction "DJNZ R3,AGAIN" i.e R3=04.

When R0=31H and R3=4, Carry flag=1:

• In the second iteration of the loop, 9CH is added to the accumulator register A by the instruction "AGAIN: ADD A, @R0". The accumulator already contains value 8AH which is added to the value 9CH i.e A=9C+8AH=26H. The carry flag is set to 1. i.e CY=1 and R5 cannot be skipped in this case and R5 is incremented by 1 i.e R5=01 by the instruction "INC R5", and the counter decrement by 1 value again, and jump to the label 'AGAIN' by the instruction "DJNZ R3,AGAIN" i.e R3=03.

When R0=32H and R3=3, Carry flag=0:

• In the third iteration of the loop, 3EH is added to the accumulator register A by the instruction "AGAIN: ADD A, @R0". The accumulator already contains value 26H which is added to the value 3EH i.e A=26+3EH=64H. The carry flag is set to 0. i.e CY=0 and R5 is skipped in this case and R5 is not incremented by 1, and the counter decrement by 1 value again, and jump to the label 'AGAIN' by the instruction "DJNZ R3,AGAIN" i.e R3=02.

When R0=33H and R3=2, Carry flag=1:

• In the fourth iteration of the loop, 9DH is added to the accumulator register A by the instruction "AGAIN: ADD A, @R0". The accumulator already contains value 64H which is added to the value 9DH i.e A=64H+9DH=01H. The carry flag is set to 1 again. i.e CY=1 and R5 cannot be skipped in this case and R5 is incremented by 1 again i.e R5=02 by the instruction "INC R5", and the counter decrement by 1 value again, and jump to the label 'AGAIN' by the instruction "DJNZ R3,AGAIN" i.e R3=01.

When R0=34H and R3=1, Carry flag=1:

• In the fifth iteration of the loop, FFH is added to the accumulator register A by the instruction "AGAIN: ADD A, @R0". The accumulator already contains value 01H which is added to the value FFH i.e A=01H+FFH=00H. The carry flag once again is set to 1. i.e CY=1 and R5 cannot be skipped in this case and R5 is incremented by 1 i.e R5=03 by the instruction "INC R5", So, the total accumulated carry becomes 03, as the carry occurs total of three times in this program. The counter is decrement by 1 value again, by the instruction "DJNZ R3,AGAIN" i.e R3=00. This time it cannot jump to the label 'AGAIN' as R3=0, DJNZ as it is zero so the condition becomes false and it comes out from the loop. The program is end. When the loop is finished, the sum is held by the accumulator registers A and R5. The accumulator register has low byte saved while the R5 has high byte saved.

The ADDC instruction means addition with carry. The ADDC instruction is used when two 16-bit data operands are need to be added and the propagation of a carry from the lower byte to the higher byte.

Example # 3:

Lets add the two 16 bit numbers:

Carry 1
2B D6
+  2A 7C
56  52

In the example above, when the first byte D6+7C is added, we get the result D6+7C=152, then due to carry flag originally we get D6+7C=52. "1" of "152" will go to the higher byte as a carry. The carry flag CY=1. The carry is propagated to the higher byte then we have 1+2B+2A=56. All the values are in hex. i.e 1H+2BH+2AH=56H.

Example # 4:

Lets add two 16 bit numbers. First number is 2DF6H and 4C9DH. Put the sum in R5 and R4. R5 for higher byte and R4 for lower byte.

CLR C;Clear the carry flag to zero i.e CY = 0
MOV A, #F6H;Load the low byte to the accumulator register. i.e A=F6H
ADD A, #9DH                     ;Add the low byte in the accumulator F6H with another low byte 9DH and place the result back in accumulator
MOV R4, A;Save the result of low bytes, sum of addition in R4 register
MOV A, #2DH;Load the high byte to the accumulator register. i.e A=2DH

ADDC A, #4CH                                                   ;Add with carry, the high byte in the accumulator 2DH with another high byte 4CH and place the result back in accumulator. i.e. A=1+2D+2C= 7A. All values are in Hex.
MOV R5, A;Save the result of high bytes, sum of addition in R5 register

After execution of the above mentioned code, the register R4 has 93H and R5 register has value 7A. i.e R4=93H, R5=7AH.