# 8085 program with a subroutine to add ten packed BCD numbers.

Here we will see how to add ten packed BCD numbers using 8085.

## Problem Statement

A set of ten packed BCD numbers is stored in the memory location starting from 8040H to 8049H.

1. Write a program with a subroutine to add these numbers in BCD. If a carry is generated save it to register B, and adjust it to BCD. The final sum will be less than 9999BCD. Store the sum at locations 8060H and 8061H.

2. Write a second subroutine to unpack the BCD sum stored in registers A and B, and store them in the OutputBuffer memory starting at 8062H. The most significant digit (BCD4) should be stored at 8065H and the least significant digit (BCD1) at 8062H.

## Discussion

The numbers are present at 8040 onwards. As the B will store the carry, we have to clear the B first. In the first phase we have to take the number from memory, then add them as BCD numbers, and store them accordingly. In the next phase the Unpacking task will be completed. In that part we are taking the 16-bit BCD number then break it into 4 digits. Then store them into given location.

Data

8040
12
8041
23
8042
42
8043
55
8044
10
8045
99
8046
35
8047
45
8048
76
8049
81

## Program

HEX Codes
Labels
Mnemonics
8000
21, 40, 80

LXI H 8040H
Initialize pointer with the first location of IN-BUFFER
8003
0E, 0A

MVI C 0AH

Initialize counter with the number of data to be add
8005
AF

XRA A
Clear the contents of the accumulator and the Reg. B
8006
47

MOV B, A

8007
CD, 23, 80
NEXTBCD:
CALL
A subroutine that add BCD numbers
800A
23

INX H

Go to next location
800B
0D

DCR C
Decrement count until 0 is reached
800C
C2, 07, 80

JNZ
NEXTBCD

800F
32, 60, 80

STA 8060 H
Store the result byte available in A
8012
57

MOV D, A

Temporarily D = A
8013
78

MOV A, B
Putting B in A
8014
32, 61, 80

STA 8061 H

To store it in OUT-BUFFER
8017
7A

MOV A, D
Restoring back A
8018
21, 62, 80

LXI H 8062H

Initialize pointer with the last location of OUT-BUFFER
801B
CD, 2E, 80

CALL
UNPAK
A subroutine that separates two nibbles from a packed BCD number
801E
78

MOV A, B

Copy B to A
801F
CD, 2E, 80

CALL UNPAK
A subroutine that separates two nibbles from a packed BCD number
8022
76

HLT

Terminate the program
8023
86
Add the contents of the memory location specified by the HL register pair with A
8024
27

DAA

8025
D0

RNC
If no carry is produced then return to the calling program
8026
57

MOV D, A

Copy A to D
8027
78

MOV A, B
If a carry is produced then adjust the contents of the Reg. B
8028
C6. 01

802A
27

DAA

802B
47

MOV B, A

802C
7A

MOV A, D
Copy D to A
802D
C9

RET

802E
57
UNPAK:
MOV D, A
Copy A to D
802F
E6, 0F

ANI 0FH

Mask off the most significant four bits
8031
77

MOV M, A
Writing A to the memory
8032
23

INX H

Go to next location
8033
7A

MOV A, D
Copy D to A
8034
E6, F0

ANI F0H

Mask off the least significant four bits
8036
0F

RRC
Rotate accumulator 4 times to get the first BCD digit
8037
0F

RRC

8038
0F

RRC

8039
0F

RRC

803A
77

MOV M, A
Writing A to the memory
803B
23

INX H

Go to next location
803C
C9

RET

Data

8060
78
8061
04
8062
08
8063
07
8064
04
8065
00