# Addition of Two 8 Bit Numbers with Carry in 8051 Microcontroller

## Program 1

### Problem

Suppose the two data bytes are 99H and 64H stored in Register R2 and R3 of the Bank 1.

Write an assembly language program for 8051 microcontroller to perform addition of above numbers. Assume result is greater than 8-bit, hence store LSB of result in R4 and MSB of result in R5.

### Algorithm

First of all, we have to select Register Bank 1, and then we can perfom1 addition on the data bytes stored in R2 and R3 register.

Step 1: Select Bank 1.

Step 2: initialize MSB counter with 00H.

Step 3: Load first number from R2 to Accumulator.

Step 4: Add second number in R3 with Accumulator.

Step 5: If Result < 8 bit, then go to Step 7.

Step 6: Increment MSB counter by 1.

Step 7: Store Result i.e. LSB to R4 and MSB to R5.

Step 8: 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 R5, #00H ; Initialize MSB counter with 00H.

MOV A, R2 ; Copy content of accumulator to accumulator.

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

JNC NEXT ; If result is <9 bit then go to NEXT.

INC R5 ; Increment MSB counter by 1.

NEXT: MOV R4, A ; Copy the result from accumulator into R4.

LOOP: AJMP LOOP ; Stop.

## Program 2

### Problem

Suppose two data bytes are stored in memory locations 4000H and 4001H. Write a program for 8051 microcontroller to perform addition of these two bytes from external memory locations.

Assume result is greater than 8-bit, hence store LSB of result in 4002 and MSB of result is stored in 4003H.

### Algorithm

Step 1: Initialize memory pointer using DPTR register with 4000h.

Step 2: Initialize MSB counter with 00h.

Step 3: Load first number from memory location 4000h.

Step 4: Increment memory pointer by 1.

Step 5: Load second number from memory location 4001h.

Step 7: If result<8 bit, then go to step 9.

Step 8: Increment MSB Counter by 1.

Step 9: Increment Memory Pointer by 1.

Step 10: Store LSB of Result in memory location4002h.

Step 11: Store MSB of Result in memory location 4003h.

Step 12: Stop.

### Program

MOV R1,00H ; Initialize MSB counter with 00h.

MOV DPTR, #4000H ; Load 4000h into DPTR.

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

MOV R0, A ; Copy content of accumulator into R0.

INC DPTR ; Increment DPTR by 1, now DPTR= 4001h.

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

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

JNC NEXT ; If result<8 bit, then go to NEXT.

INC R1 ; Increment MSB counter by 1.

NEXT: INC DPTR ; Increment memory pointer by 1.

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

INC DPTR ; Increment DPTR by 1, now DPTR= 4003h.

MOV A, R1 ; Copy content of R1 (MSB) to accumulator.

MOVX @DPTR, A ; Copy the result in accumulator (MSB) to address 4003h

LOOP: AJMP LOOP ; Stop.

## Recent posts

### 1 thought on “Addition of Two 8 Bit Numbers with Carry in 8051 Microcontroller”

1. This design is wicked! You certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!