Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8507
Скачиваний: 0
Number Systems |
27 |
The octal encoding serves as a shorthand representation for groups of 3-digit binary numbers.
Hexadecimal numbers (base 16) are used for representing values encoded in four binary digits. Since there are only ten decimal digits, the hexadecimal system borrows the first six letters of the alphabet (A, B, C, D, E, and F). The result is a set of sixteen symbols, as follows:
0 1 2 3 4 5 6 7 8 9 A B C D E F
Most modern computers are designed with memory cells, registers, and data paths in multiples of four binary digits. Table 2.2 lists some common units of memory storage.
Table 2.2
Units of Memory Storage
UNIT |
BITS |
HEX DIGITS |
HEX RANGE |
Nibble |
4 |
1 |
0 to F |
Byte |
8 |
2 |
0 to FF |
Word |
16 |
4 |
0 to FFFF |
Doubleword |
32 |
8 |
0 to FFFFFFFF |
In most digital-electronic devices memory addressing is organized in multiples of four binary digits. Here again, the hexadecimal number system provides a convenient way to represent addresses. Table 2.3 lists some common memory addressing units and their hexadecimal and decimal range.
Table 2.3
Units of Memory Addressing
UNIT |
DATA PATH |
ADDRESS RANGE |
|
IN BITS |
DECIMAL |
HEX |
|
1 paragraph |
4 |
0 to 15 |
0-F |
1 page |
8 |
0 to 255 |
0-FF |
1 kilobyte |
16 |
0 to 65,535 |
0-FFFF |
1 megabyte |
20 |
0 to 1,048,575 |
0-FFFFF |
4 gigabytes |
32 |
0 to 4,294,967,295 |
0-FFFFFFFF |
2.4 Number System Conversions
We use decimal numbers in our everyday life because they meaningfully represent common units used in the real world. To state that a certain historical event took place in the year 7C6 hexadecimal would convey little information to the average person. However, in computer systems based on two-state electronic cells binary representations are more convenient. Also note that hexadecimal and octal numbers are handy shorthand for representing groups of binary digits.
Numerical conversions between positional systems of different radices are based on the number of symbols in the respective sets and on the positional value (weight) of each column. But methods used for manual conversions are not always suitable for machine conversions, as we will see in the forthcoming sections.
28 |
Chapter 2 |
2.4.1 Binary-to-ASCII-Decimal
To manually convert a binary number to its decimal equivalent we take into account the positional weight of each binary digit, as shown in Figure 2-2.
POSITIONAL WEIGHT TABLE (decimal values)
27 |
= 128 |
|
26 |
= |
64 |
25 |
= |
32 |
24 |
= |
16 |
23 |
= |
8 |
22 |
= |
4 |
21 |
= |
2 |
20 |
= |
1 |
1 0 0 1 0 1 0 1
DIGIT VALUE TABLE (digit x weight)
1 |
x 1 |
= |
1 |
|
1 |
x 4 |
= |
4 |
|
1 |
x |
16 |
= |
16 |
1 |
x |
128 |
= 128 |
|
total |
149 |
|||
Figure 2-2 Binary to ASCII Decimal Conversion Example
The positional weight table in Figure 2-2 lists the decimal value of each binary column. These weights are powers of the system's base (2 in the binary system). In the digit value table, also in Figure 2-2, the decimal values of the binary columns holding a 1 digit are added. The sum of the weights of all the one-digits in the operand is the decimal equivalent of the binary number. In this case 10010101 binary = 149 decimal.
The method in Figure 2-2, although useful in manual conversions, is not an algorithm for computer conversions. Figure 2-3 is a flowchart of a low-level bi- nary-to-decimal conversion routine.
START
SETUP ASCII DIGIT STORAGE
INITIALIZE POINTER TO STORAGE
BINARY / 10
REMAINDER + 30H = ASCII DIGIT
ASCII DIGIT TO STORAGE
STORAGE POINTER TO NEXT DIGIT
QUOTIENT = BINARY
NO
QUOTIENT = 0
?
YES
END
Figure 2-3 Flowchart for a Binary to ASCII Decimal Conversion
Number Systems |
29 |
The algorithm for the processing in Figure 2-3 can be written as follows:
1.Set up and initialize a string storage area (sometimes called a buffer) to hold the ASCII decimal digits of the result. Set up the buffer pointer to the right-most digit position of the result.
2.Obtain the remainder of the value divided by 10.
3.Add 30H to remainder digit to convert to ASCII representation.
4.Store remainder digit in buffer and index the buffer pointer to the preceding digit.
5.Quotient of division by 10 becomes the new binary value.
6.End conversion routine if quotient is equal to 0. Otherwise, continue at step 2.
Note that the numerical digits are located from 30H to 39H in the ASCII table. This makes is easy to convert a binary digit to ASCII simply by adding 30H. Likewise, an ASCII digit is converted to binary by subtracting 30H.
2.4.2 Binary-to-Hexadecimal Conversion
The method described in Section 2.4.1 for a binary to ASCII decimal conversion can be adapted to other radices by representing the positional weight of each binary digit in the number system to which the conversion is to be made. In the case of a binary to ASCII hexadecimal conversion the positional weight of each binary digit is a hexadecimal value. Figure 2-4 shows the conversion of the binary value 10010101 into hexadecimal by using the corresponding positional weights.
POSITIONAL WEIGHT TABLE
(hexadecimal values) 27 = 80H
26 = 40H
25 = 20H
24 = 10H
23 = 8H
22 = 4H
21 = 2H
20 = 1H
1 0 0 1 0 1 0 1
DIGIT VALUE TABLE (digit x weight)
1 |
x 1H |
= |
1H |
|
1 |
x 4H |
= |
4H |
|
1 |
x |
10H |
= 10H |
|
1 |
x |
80H |
= 80H |
|
total |
95H |
|||
Figure 2-4 Binary to ASCII Hexadecimal Conversion Example
The machine conversion binary to ASCII hexadecimal is similar to the binary to ASCII decimal algorithm described previously. In the case of the conversion into ASCII hexadecimal digits the buffer need only hold four ASCII characters, since a 16-bit binary cannot exceed the value FFFFH. In the case of binary to ASCII hex the divisor for obtaining the digits is 16 instead of 10.
2.4.3 Decimal-to-Binary Conversion
Longhand conversion of decimal into binary can be performed by using the positional weights to find the binary 1-digits and then subtracting this positional weight from the decimal value. The process is shown in Figure 2-5.
30 |
Chapter 2 |
POSITIONAL WEIGHTS
(decimal values) 27 = 128 26 = 64 25 = 32 24 = 16 23 = 8 22 = 4 21 = 2 20 = 1
1 0 0 1 0 1 0 1
149 - |
128 |
= |
21 |
1 0 0 0 0 0 0 0 |
21 - 16 = 5 |
0 0 0 1 0 0 0 0 |
|||
5 - 4 = 1 |
0 0 0 0 0 1 0 0 |
|||
1 - 1 = 0 |
0 0 0 0 0 0 0 1 |
|||
binary result |
1 0 0 1 0 1 0 1 |
|||
Figure 2-5 Example of Decimal to Binary Conversion
In the example of Figure 2-5 we start with the decimal value 149. Since the highest power of 2 smaller than 149 is 128, which corresponds to bit 7, we set bit 7 in the result and perform the subtraction:
149 - 128 = 21
At this point the highest positional weight smaller than 21 is 16, which corresponds to bit 4. Therefore we set bit 4, and perform the subtraction:
21 - 16 = 5
The remaining steps in the conversion can be seen in the illustration. The conversion is finished when the result of the subtraction is 0.
Suppose there is a numerical value in the form of a string of ASCII decimal, octal, or hexadecimal digits. In order for a processor to perform simple arithmetic operations on such data, the data must first be converted to binary. The binary value is then loaded into machine registers or memory cells. However, methods suited for manual conversion do not always make a good computer algorithm. Figure 2.6 shows two decimal-to-binary conversion algorithms that are suited for machine coding.
Using the first method of Figure 2-6, the individual decimal digits are multiplied by their corresponding positional values. The final result is obtained by adding all the partial products. Although this method is frequently used, it has the disadvantage that a different multiplier is used during each iteration (1, 10, 100, 1000). The second method in Figure 2-6 starts with the high-order ASCII-decimal digit. The calculations consist of multiplying an accumulated value by 10. Initially, this accumulated value is set to 0. After multiplication by 10, the value of the digit is added to the accumulated value. The following algorithm is based on the second method in Figure 2-6.
.
Number Systems |
31 |
|||||||||||||||||||||||
METHOD NUMBER 1 |
3 |
4 |
5 |
9 |
ASCII DECIMAL DIGITS |
|||||||||||||||||||
9 |
x 1 |
= |
9 |
|||||||||||||||||||||
5 |
x 10 |
= |
50 |
|||||||||||||||||||||
4 |
x 100 |
= |
400 |
|||||||||||||||||||||
3 |
x 1000 = 3000 |
|||||||||||||||||||||||
binary = 3459 |
||||||||||||||||||||||||
METHOD NUMBER 2 |
||||||||||||||||||||||||
3 |
4 |
5 |
9 |
ASCII DECIMAL DIGITS |
||||||||||||||||||||
0 x 10 + 3 |
= |
3 |
||||||||||||||||||||||
3 x 10 + 4 |
= |
34 |
||||||||||||||||||||||
34 x 10 + 5 |
= |
345 |
||||||||||||||||||||||
345 x 10 + 9 = 3459
Figure 2-6 Machine Conversion of ASCII Decimal to Binary
1.Set up and initialize to binary zero a storage location for holding the value accumulated during conversion. Set up a pointer to the highest order ASCII digit in the source string.
2.Test the ASCII digit for a value in the range 0 to 9. End of routine if the ASCII digit is not in this range.
3.Subtract 30H from ASCII decimal digit.
4.Multiply accumulated value by 10.
5.Add digit to accumulated value.
6.Increment the pointer to the next digit and continue at step 2. Figure 2-7 is a flowchart of the conversion algorithm.
START
SETUP BINARY ACCUMULATOR
INITIALIZE POINTER TO FIRST SOURCE DIGIT
NO |
|
VALID DIGIT |
END |
? |
|
YES |
ASCII DIGIT - 30H
ACCUMULATOR X 10
ACCUMULATOR + DIGIT
POINTER TO NEXT DIGIT
Figure 2-7 Flowchart for ASCII to Machine Register Conversion