## Tuesday, March 22, 2016

### 8051 Addition of Unsigned Numbers

In the 8051, in order to add numbers together, the accumulator register (A) must be involved. The form of the ADD instruction is

ADD A, operand ; A = A + operand

The instruction ADD is used to add two operands. The destination operand is always in register A while the source operand can be a register, immediate data, or in memory. Remember that memory-to-memory arithmetic operations are never allowed in 8051 Assembly language. The operand can be between 00 and FFH (0 to 255 decimal) for 8-bit data. C, AC, P flags are affected.

Example:
``` ORG 0H MAIN : DATA1 EQU 0F5H DATA2 EQU 0BH MOV A, #DATA1 ; A = F5H ADD A, #DATA2 ; A = F5H + BH = 00H MOV P0, A SJMP MAIN END; end of asm source file ```

After the addition register A contains 00H, C = 1, AC = 1, P = 0

Program to find the sum of values:
To calculate the sum of any number of operands, the carry flag should be checked after the addition of each operand. Carry flag should be accumulated as the operands are added to A.

Assume that RAM locations 40 – 44 have the following values in hex. 40=(7D) 41=(EB) 42=(C5) 43=(5B) 44=(30)

``` ORG 0H MAIN : MOV R0, #40H; load pointer MOV R2, #5; load counter CLR A; clear accumulator MOV R7, A; clear R7 AGAIN : ADD A, @R0; A= A+ value at R0 JNC NEXT; Jump if No carry INC R7 ; accumulate carry, if set NEXT : INC R0 ; increment pointer DJNZ R2, AGAIN; repeat until R2 = 0 END; end of asm source file ```

At the end when the loop is finished, the sum is held by registers A and R7, where A has the low byte and R7 has the high byte.

Related topics:
8051 Addition   |   8051 Addition with Carry   |   8051 Addition of 16-bit Numbers   |   8051 Binary Coded Decimal   |   8051 ADDC Instruction   |   8051 ADD Instruction   |   8051 Signed Numbers

List of topics: 8051