Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8576
Скачиваний: 0
Data Types and Data Storage |
39 |
several bits. In a particular machine the basic unit of data storage is called the word size. Word size in computers often ranges from 8 to 128 bits, in powers of 2. Microcontrollers and other digital devices sometimes use word-sizes that are determined by their specific architectures. For example, some PIC microcontrollers use a 14-bit word size.
In most digital machines the smallest unit of storage individually addressable is eight bits (one byte). Individual bits are not directly addressable and must be manipulated as part of larger units of data storage.
3.2.3 Byte Ordering
The storage of a single-byte integer can be done according to the scheme in Figure 3-1. However, the maximum value that can be represented in eight bits is the decimal number 255. To represent larger binary integers requires additional storage area. Since memory is usually organized in byte-size units, any decimal number larger than 255 requires more than one byte of storage. In this case the encoding is padded with the necessary leading zeros. Figure 3-2 is a representation of the decimal number 21,141 stored in two consecutive data bytes.
machine storage
binary decimal
= 01010010 10010101 = 21,141
Figure 3-2 Representation of an Unsigned Integer
One issue related to using multiple memory bytes to encode binary integers is the successive layout of the various byte-size units. In other words, does the representation store the most significant byte at the lowest numbered memory location, or viceversa. For example, when a 32-bit binary integer is stored in a 32-bit storage area we can follow the conventional pattern of placing the low-order bit on the right-hand side and the high-order bit on the left, as we did in Figure 3-1. However, if the 32-bit number is to be stored into four byte size memory cells, then two possible storage schemes are possible, as shown in Figure 3-3.
LOW-TO-LOW STORAGE SCHEME
32 bits
high |
low |
HIGH-TO-LOW STORAGE SCHEME
32 bits
high |
low |
|
high |
low |
high |
low |
memory bytes
memory bytes
Figure 3-3 Byte Ordering Schemes
40 |
Chapter 3 |
In the low-to-low storage scheme the low-order 8-bits of the operand are stored in the low-order memory byte, the next group of 8-bits are moved to the following memory byte in low-to-high order, and so on. Conceivably, this scheme can be described by saying that the “little end” of the operand is stored first, that is, in lowest memory. According to this notion, the storage scheme is described as the lit- tle-endian format. If the “big-end” of the operand, that is, the highest valued bits, is stored in the low memory addresses then the byte ordering is said to be in big-endian format. Some Intel processors (like those of 80x86 family) follow the lit- tle-endian format. Some Motorola processors (like those of the 68030 family) follow the big-endian format, while others (such as the MIPS 2000) can be configured to store data in either format.
In many situations the programmer needs to be aware of the byte-ordering scheme; for example, to retrieve memory data into processor registers so as to perform multi-byte arithmetic, or to convert data stored in one format to the other one.
This last operation is a simple byte-swap. For example, if the hex value 01020304 is stored in four consecutive memory cells in low-to-high order (little-endian format) it appears in memory (low-to-high) as the values 04030201. Converting this data to the big-endian format consists of swapping the individual bytes so that they are stored in the order 01010304. Figure 3-4 is a diagram of a byte swap operation.
31 |
24 |
23 |
16 |
15 |
8 |
7 |
0 |
||||
31 |
24 |
23 |
16 |
15 |
8 |
7 |
0 |
Figure 3-4 Data Format Conversion by Byte Swapping
3.2.4 Sign-Magnitude Representation
Representing signed numbers requires differentiating between positive and negative magnitudes. One possible scheme is to devote one bit to represent the sign. Typically the high-order bit is set (1) to denote negatives and reset (0) to denote positives. Using this convention the decimal numbers 93 and -93 are represented as follows:
01011101 binary = 93 decimal
11011101 binary = -93 decimal
|
|—————————- sign bit
This way of designating negative numbers, called a sign-magnitude representation, corresponds to the conventional way in which we write negative and positive numbers longhand, that is, we precede the number by its sign. Sign-magnitude representation has the following characteristics:
Data Types and Data Storage |
41 |
1.The absolute value of positive and negative numbers is the same.
2.Positive numbers can be distinguished from negative numbers by examining the high-order bit.
3.There are two possible representations for zero, one negative (10000000B) and one positive (00000000B).
But a major limitation of sign-magnitude representation is that the processing required to perform addition is different from that for subtraction. Complicated rules are required for the addition of signed numbers. For example, considering two operands labeled x and y, the following rules must be observed for performing signed addition:
1.If x and y have the same sign, they are added directly and the result is given the common sign.
2.If x is larger than y, then y is subtracted from x and the result is given the sign of x.
3.If y is larger than x then x is subtracted from y and the result is given the sign of y.
4.If either x or y is 0 or -0 the result is the non-zero element.
5.If both x and y are -0, then the sum is 0.
However, there are other numeric representations that avoid this situation. A consequence of sign-magnitude representation is that, in some cases, it is necessary to take into account the magnitude of the operands in order to determine the sign of the result. Also, the presence of an encoding for negative zero reduces the numerical range of the representation and is, for most practical uses, an unnecessary complication. An important limitation of using the high-order bit for representing the sign is the resulting halving of the numerical range.
3.2.5 Radix Complement Representation
The radix complement of a number is defined as the difference between the number and the next integer power of the base that is larger than the number. In decimal numbers the radix complement is called the ten’s complement. In the binary system the radix complement is called the two’s complement. For example, the radix complement of the decimal number 89 (ten’s complement) is calculated as follows:
100= higher power of 10
-89
----
11 = ten’s complement of 89
The use of radix complements to simplify machine subtraction operations can best be seen in an example. The operation x = a - b with the following values:
a = 602 b = 353
602
- 353
_____
x = |
249 |
42 |
Chapter 3 |
Note that in the process of performing longhand subtraction we had to perform two borrow operations. Now consider that the radix complement (ten’s complement) of 353 is:
1000 - 353 = 647
Using complements we can reformulate subtraction as the addition of the ten’s complement of the subtrahend, as follows:
602 + 647
______
1249
|____________ discarded digit
The result is adjusted by discarding the digit that overflows the number of digits in the operands.
In performing longhand decimal arithmetic there is little advantage in replacing subtraction with ten’s complement addition. The work of calculating the ten’s complement cancels out any other possible benefit. However, in binary arithmetic the use of radix complements entails significant computational advantages because binary machines can calculate complements efficiently.
The two’s complement of a binary number is obtained in the same manner as the ten’s complement of a decimal number, that is, by subtracting the number from an integer power of the base that is larger than the number. For example, the two’s complement of the binary number 101 is:
1000B |
= |
2^3 = 8 |
decimal (higher power of 2) |
- 101B |
= |
5 |
decimal |
_________ |
_________ |
||
011B |
= |
3 |
decimal |
While the two’s complement of 10110B is calculated as follows:
100000B |
= |
2^5 = 32 |
decimal (higher power of 2) |
- 10110B |
= |
22 |
decimal |
_______ |
__________ |
||
01010B |
10 decimal |
||
You can perform the binary subtraction of 11111B (31 decimal) minus 10110B (22 decimal) by finding the two’s complement of the subtrahend, adding the two operands, and discarding any overflow digit, as follows:
11111B = 31 decimal
+ 01010B = 10 decimal (two’s complement of 22)
_______
101001B discard______|
01001B = 9 decimal (31 minus 22 = 9)
In addition to the radix complement representation, there is a diminished radix representation that is often useful. This encoding, sometimes called the radix-mi- nus-one form, is created by subtracting 1 from an integer power of the base that is larger than the number, then subtracting the operand from this value. In the decimal
Data Types and Data Storage |
43 |
system the diminished radix representation is sometimes called the nine’s complement. This is due to the fact that an integer power of ten, minus one, results in one or more 9-digits. In the binary system the diminished radix representation is called the one’s complement. The nine’s complement of the decimal number 76 is calculated as follows:
100 = next highest integer power of 10
99 = 100 minus 1
-76
----
23 = nine’s complement of 89
The one’s complement of a binary number is obtained by subtracting the number from an integer power of the base that is larger than the number, minus one. For example, the one’s complement of the binary number 101 (5 decimal) can be calculated as follows:
1000B |
= |
2^3 = |
8 decimal |
||
111B |
= |
1000B |
minus 1 = |
7 |
decimal |
- 101B |
5 |
decimal |
|||
------ |
--------- |
||||
010B |
= |
2 |
decimal |
||
An interesting feature of one’s complement is that it can be obtained changing every 1 binary digit to a 0 and every 0 binary digit to a 1. In this example 010B is the one’s complement of 101B. In this context the 0 binary digit is often said to be the complement of the 1 binary digit, and vice versa. Most modern computers contain an instruction that inverts all the digits of a value by changing all 1 digits into 0, and all 0 digits into 1. The operation is also known as logical negation.
Furthermore, the two’s complement can be obtained by adding 1 to the one’s complement of a number. Therefore, instead of calculating
100000B - 10110B
-------
01010B
we can find the two’s complement of 10110B as follows:
10110B = number
01001B = change 0 to 1 and 1 to 0 (one’s complement)
+ |
1B |
then add 1 |
--------- |
||
01010B = two’s complement
This algorithm provides a convenient way of calculating the two’s complement in a machine equipped with a complement instruction. Finally, the two’s complement can be obtained by subtracting the operand from zero and discarding the overflow.
The radix complement of a number is the difference between the number and an integer power of the base that is larger than the number. Following this rule, we calculate the radix complement of the binary number 10110 as follows:
100000B |
= |
2^5 = 32 |
decimal |
- 10110B |
= |
22 |
decimal |
------- |
---------- |
||
01010B |
10 decimal |
||
44 |
Chapter 3 |
However, the machine calculation of the two’s complement of the same value often produces a different result, for example:
100000000B |
= |
28 = 256 |
decimal |
- 00010110B |
= |
22 |
decimal |
__________ |
___________ |
||
11101010B |
234 |
decimal |
|
The difference is due to the fact that in the longhand method we have used the next higher integer power of the base compared to the value of the subtrahend (in this case 100000B) while the machine calculations use the next higher integer power of the base compared to the operand’s word size, which is normally either 8 or 16 bits. In this example the operand’s word size is eight bits and the next highest integer power of 2 is 100000000B. In either case, the results from two’s complement subtraction are valid as long as the minuend is an integer power of the base that is larger than the subtrahend.
For example, to perform the binary subtraction of 00011111B (31 decimal) minus
00010110B (22 decimal) we can find the two’s complement of the subtrahend and add, discarding any overflow digit, as follows:
00011111B = 31 decimal
+11101010B = 234 decimal (two’s complement of 22)
_________
100001001B
discard____|
00001001B = 9 decimal (31 minus 22 = 9)
In addition to the simplification of subtraction, two’s complement arithmetic has the advantage that there is no representation for negative 0. It can be argued that there are cases in which a negative zero notation could be useful, but in fact this is usually unnecessary. While both the two’s complement and the one’s complement schemes can be used to implement binary arithmetic, system designers usually prefer the two’s complement.
3.3 Encoding of Fractional Numbers
In any positional number system the weight of each integer digit is determined by the formula:
P = d * BC
where d is the digit, B is the base or radix, and C is the zero-based column number, starting from right to left. Therefore, the value of a multi-digit positive integer to n digits can be expressed as a sum of the digit values:
dn*Bn + dn-1*Bn-1 + dn-2*Bn-2 + ... + d0*B0
where d is the value of the digit and B is the base or radix of the number system. This representation can be extended to represent fractional values. Recalling that we can
Data Types and Data Storage |
45 |
extend the sequence to the right of the radix point, as follows:
x− n |
= |
1 |
|||||||||||||||||||||||||||||||||||||
xn |
|||||||||||||||||||||||||||||||||||||||
INTEGER PART |
|||||||||||||||||||||||||||||||||||||||
27 |
= 128 |
||||||||||||||||||||||||||||||||||||||
26 |
= 64 |
||||||||||||||||||||||||||||||||||||||
25 |
= 32 |
||||||||||||||||||||||||||||||||||||||
24 |
= 16 |
||||||||||||||||||||||||||||||||||||||
23 |
= 8 |
||||||||||||||||||||||||||||||||||||||
22 |
= 4 |
||||||||||||||||||||||||||||||||||||||
21 |
= 2 |
||||||||||||||||||||||||||||||||||||||
20 |
= 1 |
radix point |
|||||||||||||||||||||||||||||||||||||
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
||||||||||||||||||||||||
FRACTIONAL PART |
|||||||||||||||||||||||||||||||||||||||
.500 |
1/2 |
2-1 |
|||||||||||||||||||||||||||||||||||||
.250 |
1/4 |
2-2 |
|||||||||||||||||||||||||||||||||||||
.125 |
1/8 |
2-3 |
|||||||||||||||||||||||||||||||||||||
.0625 |
1/16 |
2-4 |
|||||||||||||||||||||||||||||||||||||
.03125 |
1/32 |
2-5 |
|||||||||||||||||||||||||||||||||||||
.015625 |
1/64 |
2-6 |
|||||||||||||||||||||||||||||||||||||
.0078125 |
1/128 |
2-7 |
|||||||||||||||||||||||||||||||||||||
.00390625 1/256 2-8
Figure 3-5 Positional Weights in a Binary Fraction
In the decimal system the value of each digit to the right of the decimal point is calculated as 1/10, 1/100, 1/1000, and so on. The value of each successive digit of a binary fraction is the reciprocal of a power of 2; therefore, the sequence is: 1/2, 1/4, 1/8, 1/16, .... Figure 3-5 shows the positional weight of the integer and fractional digits in a binary number.
In Chapter 2 we used the positional weights of the binary digits to convert a binary number to its decimal equivalent. A similar method can be used to convert the fractional part of a binary number. Using the decimal equivalents shown in Figure 3-5 we convert the binary fraction .10101 to a decimal fraction as follows
.1 |
0 |
1 |
0 |
1 |
|
| |
| |
| |
|||
.500 |
_____________________| |
| |
| |
||
.125 |
_________________________| |
| |
|||
.03125 |
____________________________| |
||||
------ |
|||||
.65625 |
|||||
3.3.1 Fixed-Point Representations
The encoding and storage of fractional numbers (also called real numbers) in binary form presents several difficulties. The first one is related to the representation of the radix point. Since there are only two symbols in the binary set, and both are used to represent the numerical value of the number, there is no other symbol available for the decimal point.