 Erik Østergaard - BCD (Binary Coded Decimal) Number System    ## BCD (Binary Coded Decimal) Number System

### The BCD Number System

You should now be familiar with the Binary, Decimal and Hexadecimal Number System. If we view single digit values for hex, the numbers 0 - F, they represent the values 0 - 15 in decimal, and occupy a nibble. Often, we wish to use a binary equivalent of the decimal system. This system is called Binary Coded Decimal or BCD which also occupies a nibble. In BCD, the binary patterns 1010 through 1111 do not represent valid BCD numbers, and cannot be used.

Conversion from Decimal to BCD is straightforward. You merely assign each digit of the decimal number to a byte and convert 0 through 9 to 0000 0000 through 0000 1001, but you cannot perform the repeated division by 2 as you did to convert decimal to binary.

Let us see how this works. Determine the BCD value for the decimal number 5,319. Since there are four digits in our decimal number, there are four bytes in our BCD number. They are:

 Thousands Hundreds Tens Units 5 3 1 9 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1

Since computer storage requires the minimum of 1 byte, you can see that the upper nibble of each BCD number is wasted storage. BCD is still a weighted position number system so you may perform mathematics, but we must use special techniques in order to obtain a correct answer.

### PACKED BCD

Since storage on disk and in RAM is so valuable, we would like to eliminate this wasted storage. This may be accomplished by packing the BCD numbers. In a packed BCD number, each nibble has a weighted position starting from the decimal point. Therefore, instead of requiring 4 bytes to store the BCD number 5319, we would only require 2 bytes, half the storage. The upper nibble of the upper byte of our number would store the THOUSANDS value while the lower nibble of the upper byte would store the HUNDREDS value. Likewise, the lower byte would store the TENS value in the upper nibble and the UNITS digit in the lower nibble. Therefore, our previous example would be:

 Thousands - Hundreds Tens - Units 53 19 0 1 0 1 0 0 1 1 0 0 0 1 1 0 0 1

### SUBROUTINE TO PACK A 2 BYTE BCD NUMBER

Lets assume that a buffer called NUMBUF contains a two digit BCD number stored in two bytes and we wish to Pack the BCD number into a single byte. We will create a Subroutine called PACKIT to pack the BCD number. Let's develop the algorithm to Pack a BCD number.

### SUBROUTINE PACKIT

On Entry:

1. Register B contain the upper byte of the BCD number.
2. Register C contains the lower byte of the BCD number.

On Exit:

1. Register A contains the Packed BCD Number.

To pack the number we must:

1. A <= (B)
2. Rotate A Left 4 times
3. A <= (A) + (C)

That's all. We could enhance our routine by checking to see if the first number is zero. If it is, we do not need to rotate 4 times. Also, this routine assumes that the number is in BCD format. If we are unsure, we should test to see if the numbers are valid BCD numbers.

It would not work if the number is in ASCII. If we entered the values from the keyboard into the buffer, we would have to strip off the ASCII which occupies the upper nibble, as well as check for a valid BCD number. This is fairly simple. ASCII values for numbers beginning at 30H and end at 39H.

### SUBROUTINE PACKIT FOR ASCII VALUES

On Entry:

1. Register B contains the upper byte of the BCD number in ASCII.
2. Register C contains the lower byte of the BCD number in ASCII.

On Exit:

1. Register A contains the Packed BCD Number.

Therefore, since our subroutine Entry and Exit conditions are the same, our algorithm would now look like the following:

To pack the ASCII values in RP BC into a Packed BCD number we must:

1. A <= (B) (copy number into A)
2. A <= (A) - 30H (see if occurs before 0)
3. Jump on minus to error routine (not valid number)
4. Compare to 0AH (see if above 9)
5. Jump on positive to error routine (not valid number)
6. Rotate Left 4 times puts in upper nibble
7. B <= (A) (preserve high nibble in proper format)
8. A <= (C) (get low nibble)
9. A <= (A) - 30H (see if occurs before 0)
10. Jump on minus to error routine (not valid number)
11. Compare to 0AH (see if above 9)
12. Jump on positive to error routine (not valid number)
13. ADD A <= (A) + (B) (now it is packed)

Remember, subroutines belong after our EXIT Routine in the source code. Now that we have seen how to pack the number, let's see what we have to do in MAIN to pack a 4 digit number. We will just call PACKIT. You would use the proper algorithm for BCD or ASCII numbers depending which format your program uses.

### MODULE TO PACK A 4 DIGIT BCD NUMBER

On entry to this MODULE,

1. The number is located in Storage in a 4 byte buffer called INBUF
2. Packing will be accomplished by a Subroutine called PACKIT
3. The result will be stored in a two byte buffer called PAKBUF
4. HL is an address pointer to INBUF
5. DE is an address pointer to PAKBUF
6. B contains the Upper BCD number
7. C contains the Lower BCD number

### Module Algorithm

1. HL <= INBUF initialize Address Pointer
2. DE <= PAKBUF initialize Address Pointer
3. B <= (M) based on HL
4. HL <= (HL) + (1) Increment HL point to next digit
5. C <= (M) based on HL
6. HL <= (HL) + 1 Increment HL point to next digit
7. Exchange HL and DE so we can use MOV instead of STAX
8. CALL PACKIT
9. M <= (A) Store packed number
10. HL <= (HL) + 1 Increment HL point to next digit
11. Exchange HL and DE Put back into original locations
12. B <= (M) based on HL begin second time
13. HL <= (HL) + 1 Increment HL point to next digit
14. C <= (M) based on HL
15. HL <= (HL) + 1 Increment HL point to next digit
16. Exchange HL and DE so we can use MOV instead of STAX
17. CALL PACKIT
18. M <= (A) Store packed number
19. HL <= (HL) + 1 Increment HL point to next digit
20. Exchange HL and DE Put back into original locations

These examples are specially for use in programming in the Assembly Language.

##### My Sources / Mine kilder

Sources: Various books, the Internet, and various encyclopedias.

Kilder: Forskellige bøger, internettet og forskellige leksikoner.

### Computer Data Representation and Number Systems / Computer data repræsentation og talsystemer      ©1997 - 1999 Erik Østergaard, Copenhagen, Denmark.