Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

ALP to convert two digit Packed BCD to Unpacked BCD

What is BCD?
BCD or Binary-Coded Decimal is a system of representing numerals that assigns a four-
digit binary code to each digit 0 through 9 in a decimal (base 10) number. In BCD system
the decimal numerals [0-9] are represented by four bit binary as follows.
Digit BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

In the decimal system, all numbers larger than 9 have two or more digits. In the binary-
coded decimal system, each digit is encoded separately. The full number is first
segregated into its individual digits. These digits are then represented by their equivalent
4-bit binary-coded decimal codes as shown in above table.
Invalid BCD:
The invalid BCD numbers: 1010, 1011, 1100, 1101, 1110, 1111.
Unpacked BCD
In Unpacked BCD, only one digit is stored per byte.
Decimal Unpacked BCD
2 0000 0010
7 0000 0111
9 0000 1001
Packed BCD:
In the case of packed BCD numbers, two BCD digits are stored per byte.
Decimal BCD
23 0010 0011
72 0111 0010
94 1001 0100
Logic:
Consider you want to convert 1001 1000 (i.e. packed BCD 98) to unpacked BCD. The
expected result is 0000 1001 (unpacked BCD 9) and 0000 1000 (unpacked BCD).
1. Mask lower nibble (four bits) of the packed BCD number. (1001 0000)
2. Rotate right by 4 bits. (0000 1001)
3. Store the upper unpacked BCD digit.
4. Mask upper nibble (four bits) of the packed BCD number. (0000 1000)
5. Store the lower unpacked BCD digit.
Suggested instructions:
 AND instruction for masking
 ROR for rotate

You might also like