# Addition of Two 8-Bit Numbers in 8051 Microcontroller

## Program 1

### Problem

Add the two data bytes 50H and 60H. Then load one of the data byte in Accumulator and then add second byte with accumulator.

### Program

MOV A, #50H ; Load 50H into accumulator

LOOP: AJMP LOOP ; Stop

## Program 2

### Problem

Suppose the two data bytes are 40H and 50H stored in Register R2 and R3 of the Bank 1. Add them and store result in Register R4 of Bank 1.

### Algorithm

• Step 1: Select Bank 1.
• Step 2: Load first number from R2 to Accumulator.
• Step 3: Add second number from R3 with
• Step 4: Store result to R4.
• Step 5: Stop.

### Program

CLR PSW.4 ; Clear 4th bit of PSW

SETB PSW.3 ; Set 3rd bit of PSW

; above steps are used to select register bank 1

MOV A, R2 ; Copy content of accumulator to accumulator

ADD A, R3 ; Add accumulator and R3, result stored into accumulator

MOV R4, A ; Copy the result from accumulator into r4

LOOP: AJMP LOOP ; Stop

## Program 3

### Problem

Suppose two data bytes are stored in memory location 3000H and 3001H. Write a program to add these two bytes from external memory locations and store result in memory location 3002H of the external memory.

### Algorithm

Step 1: Initialize memory pointer using DPTR register with 3000H.

Step 2: Load first number from the memory location 3000H.

Step 3: Increment memory pointer by 1.

Step 4: Load second number from memory location 3001H.

Step 6: Store Result in memory location 3002H by incrementing memory pointer.

Step 7: Stop.

### Program

MOV DPTR, #3000H ; Load 3000h into DPTR

MOVX A, @DPTR ; Copy the data from 3000h to accumulator ;indirectly

MOV R0, A ; Copy content of accumulator into R0

INC DPTR ; Increment DPTR by 1, now DPTR= 3001h

MOVX A, @DPTR ; Copy data from 3001h into accumulator

ADD A, R0 ; Add content accumulator and R0, result in ;accumulator

INC DPTR ; Increment DPTR by 1, now DPTR= 3002h

MOVX @DPTR, A ; Copy the result in accumulator to address 3002h

LOOP: AJMP LOOP ; Stop