# 8051 Program for Addition of Two 16 Bit Numbers

## Problem

Two 16-bit numbers are stored in register bank 2 as follows.

R0 – LSB of first number

R1 – MSB of first number

R2 – LSB of second number

R3 – MSB of second number

Write an assembly language program for 8051 microcontroller to add these two numbers and store the in same register bank as follows.

R4 – LSB of result

R5 – MSB of result

R6 – Carry if any.

## Algorithm

Step 1: Select register bank 2.

Step 2: Initialize Carry Counter to get result > 16 bit.

Step 3: Load LSB of first number in Accumulator.

Step 4: Add LSB of Second number with LSB of first number.

Step 5: Store LSB of result.

Step 6: Load MSB of first number in Accumulator.

Step 7: Add MSB of second number with MSB of first Number.

Step 8: If Carry is not 1, then go to step 10.

Step 9: Increment Carry Counter by 1.

Step 10: Store MSB of result.

Step 11: Store Carry of result.

Step 12: Stop.

## Program

SETB PSW.4 ; Set 4th bit of PSW.

CLR PSW.3 ; Clear 3rd bit of PSW.

; Above steps are performed to select register bank 2.

MOV R6, #00H ; Initialize R6 as carry counter.

MOV A, R0 ; Load LSB of first number in accumulator.

ADD A, R2 ; Add LSB of second number with accumulator.

MOV R4, A ; Store the LSB of result into R4.

MOV A, R1 ; Load MSB of first number in accumulator.

ADDC A, R3 ; Add MSB of second number with accumulator and carry.

JNC NEXT ; Jump on next if carry=0.

INC R6 ; If carry=1, increment counter by 1.

NEXT: MOX R5, A ; Store MSB of result to R5.

LOOP: AJMP LOOP ; Stop.