---
title: "C program: sum of two 8-bit BCD numbers"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-bcd-sum-c"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-bcd-sum-c
site: BCT Engineering
---

# C program: sum of two 8-bit BCD numbers

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2072 Asoj, 5 marks
- Video: [8051 BCD addition program | Program for the addition of BCD numbers in 8051 Microcontroller](https://www.youtube.com/watch?v=xOeJFkWppvk) — Electronics for You
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/8051-bcd-sum-c
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Write a program using C-programming language to find the sum between two 8-bit BCD data stored in RAM locations 50H and 51H and store the BCD sum at RAM locations 52H and 53H.

## Short answer

Unpack each BCD byte into tens and units digits. Add units digits; if sum >9, subtract 10 and increment tens. Add tens digits; if sum >9, subtract 10 and set carry byte (53H) to 1. Pack the two-digit sum into 52H.

## Answer

Each byte holds two packed BCD digits (e.g. 68H means 68). The units and tens digits are added separately and adjusted: any digit above 9 is reduced by 10 with a carry to the next digit. The sum goes to 52H, and the hundreds digit (carry) to 53H.

```c
#include <reg51.h>
#include <absacc.h>

#define NUM1   DBYTE[0x50]      /* first packed BCD number   */
#define NUM2   DBYTE[0x51]      /* second packed BCD number  */
#define SUM    DBYTE[0x52]      /* BCD sum (two low digits)  */
#define CARRY  DBYTE[0x53]      /* 01 if the sum exceeds 99  */

void main(void)
{
    unsigned char lo, hi;

    lo = (NUM1 & 0x0F) + (NUM2 & 0x0F);     /* add the units digits */
    hi = (NUM1 >> 4)   + (NUM2 >> 4);       /* add the tens digits  */

    if (lo > 9) {                           /* decimal adjust units */
        lo -= 10;
        hi++;                               /* carry into tens      */
    }
    CARRY = 0;
    if (hi > 9) {                           /* decimal adjust tens  */
        hi -= 10;
        CARRY = 1;                          /* hundreds digit = 1   */
    }
    SUM = (hi << 4) | lo;                   /* pack the two digits  */

    while (1);                              /* stop */
}
```

Examples (tested in an 8051 simulator): 68H + 59H gives 52H = 27H, 53H = 01H (127); 45H + 38H gives 83H, 00H; 99H + 99H gives 98H, 01H (198).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does the packed BCD byte 68H represent?
   A. Binary 104
   B. The decimal number 68
   C. The decimal number 6.8
   D. Binary 68
   Answer: B. Packed BCD stores one decimal digit per nibble, so 68H means decimal 68. (Key point: Packed BCD)

2. What adjustment is made when the units-digit sum exceeds 9?
   A. Add 6 to the tens digit
   B. Subtract 10 from the units and add 1 to the tens digit
   C. Clear the units digit
   D. Add 10 to the units digit
   Answer: B. A digit above 9 is reduced by 10 and a carry goes to the next digit. (Key point: Decimal adjust)

3. What are the stored results for 68H + 59H?
   A. 52H = 27H, 53H = 01H
   B. 52H = 127H, 53H = 00H
   C. 52H = 21H, 53H = 06H
   D. 52H = 0C7H, 53H = 00H
   Answer: A. 68 + 59 = 127, so the two low digits 27H go to 52H and the hundreds carry 01H to 53H. (Key point: Result of 68H + 59H)

4. How does the program get the tens digit of NUM1?
   A. NUM1 & 0x0F
   B. NUM1 >> 4
   C. NUM1 * 10
   D. NUM1 % 4
   Answer: B. Shifting right by 4 moves the upper nibble (tens digit) down; & 0x0F gives the units digit. (Key point: Digit separation)

## Related in Embedded System

- [Count number of 1s in ROM data](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-count-ones-asm) — Embedded System, chapter 9, asked 2×
- [Count number of 0s in ROM data](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-count-zeros-asm) — Embedded System, chapter 9, asked 2×
- [Get data from P0, send to P1 (asm and C)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-p0-to-p1-assembly-c) — Embedded System, chapter 9

## Related in other subjects

- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
- [8085 ALP: compare two tables of 16-bit data](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-compare-tables-1ffffh) — Microprocessor, chapter 2
- [8086 ALP: sum of 100 even numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-sum-even-numbers) — Microprocessor, chapter 3
- [Stored program concept and processing cycle](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Astored-program-processing-cycle) — Microprocessor, chapter 1, asked 6×
