Bottom of This Page |
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.
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 |
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.
On Entry:
On Exit:
To pack the number we must:
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.
On Entry:
On Exit:
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:
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.
On entry to this MODULE,
These examples are specially for use in programming in the Assembly Language.
Sources: Various books, the Internet, and various encyclopedias.
Kilder: Forskellige bøger, internettet og forskellige leksikoner.
©1997 - 1999 Erik Østergaard, Copenhagen, Denmark. |