Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
Скачиваний:
475
Добавлен:
12.08.2013
Размер:
4.99 Mб
Скачать

Data Types and Data Storage

 

 

 

 

 

 

 

 

 

51

 

 

 

 

sign of number

 

 

 

 

 

 

 

 

 

implied leading digit

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

field

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10000101

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.1111110

1100000 00000000

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

exponent

 

 

 

 

 

 

 

 

 

significand

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

field

 

 

 

 

 

 

 

 

 

 

 

field

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0

1

0

0

0

0

1

0

1

1

1

1

1

1

1

0

1

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

42H

 

FEH

 

 

 

C0H

 

 

 

00H

MEMORY LAYOUT OF 127.375

 

MEMORY LAYOUT MAP FOR

IN LITTLE-ENDIAN FORMAT

 

IEEE SINGLE FORMAT

LOW ADDRESS

LOW ADDRESS

0 0 0 0 0 0 0 0

1 1 0 0 0 0 0 0

1 1 1 1 1 1 1 0

0 1 0 0 0 0 1 0

HIGH ADDRESS

 

legend:

m

m

m

m

m

m

m

m

00H

s = sign bit

16

17

18

19

20

21

22

23

e = exponent bit

 

 

 

 

 

 

 

 

C0H

m = mantissa bit

m

m

m

m

m

m

m

m

 

8

9

10

11

12

13

14

15

 

 

FEH

 

 

 

 

 

 

 

 

 

 

e

m

m

m

m

m

m

m

42H

 

8

1

2

3

4

5

6

7

 

 

 

 

 

 

 

 

 

 

 

s

e

e

e

e

e

e

e

 

 

 

1

2

3

4

5

6

7

 

 

 

 

 

 

 

 

 

 

HIGH ADDRESS

Figure 3-8 Encoding of the Number 127.375 in IEEE Single Format

bit value: 11111110-11000000-00000000 = 16,695,296

|------

| |---------------

|

|

|

 

|

|________

fractional part

|____________________

integer part

number: 127.375

3.4 Binary-Coded Decimals (BCD)

Floating-point encodings are the most efficient format for storing numerical data in a digital device and binary arithmetic is the fastest way to perform numerical calculations. But other representations are also useful. BCD (binary-coded decimal) is a way of representing decimal digits in binary form. There are two common ways of encoding decimal digits in binary format. One is known as the packed BCD format and the other one as unpacked. In the unpacked format each BCD digit is stored in one byte. In packed form two BCD digits are encoded per byte. The unpacked BCD format does not use the four high-order bits of each byte, which is wasted storage space. On the other hand, the unpacked format facilitates conversions and arithmetic operations on some machines. Figure 3.9 shows the memory storage of a packed and unpacked BCD number.

52 Chapter 3

UNPACKED BCD

 

PACKED BCD

 

 

 

 

 

2

 

 

23

 

0 0 0 0

0 0 1 0

 

 

 

 

 

0 0 1 0

0 0 1 1

 

 

 

 

3

 

 

79

 

0 0 0 0

0 0 1 1

 

0 1 1 1

1 0 0 1

 

 

 

 

 

 

 

 

 

 

7

 

 

 

 

0

0

0

0

0

1

1

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9

 

 

 

 

0

0

0

0

1

0

0

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 3-9 Packed and Unpacked BCD

3.4.1 Floating-Point BCD

Unlike the floating-point binary numbers, binary-coded decimal representations and

BCD arithmetic have not been explicitly described in a formal standard. Each machine or software package stores and manipulates BCD numbers in a unique and often incompatible way. Some machines include packed decimal formats, which are sign-magnitude BCD representations. These integer formats are useful for conversions and input-output operations. For performing arithmetic calculations a float- ing-point BCD encoding is required. This approach provides all the advantages of floating-point as well as the accuracy of decimal encodings.

The BCD floating-point format which we call BCD12 is shown Figure 3-8.

sign of number (1 BCD digit) sign of exponent (1 BCD digit)

exponent (4 BCD digits) significand (18 BCD digits)

S s e e e e m m m m m m m m m m m m m m m m m m

Figure 3-10 Map of the BCD12 Format

BCD12 requires 12 bytes of storage and is described as follows:

1.The sign of the number (S) is encoded in the left-most packed BCD digit. Therefore, the first four bits are either 0000B (positive number) or 0001B (negative number).

2.The sign of the exponent is represented in the four low-order bits of the first byte. The sign of the exponent is also encoded in one packed BCD digit. As is the case with the sign of the number field, the sign of the exponent is either 0000B (positive exponent) or 0001B (negative exponent)

3.The following two bytes encode the exponent in four packed BCD digits. The decimal range of the exponent is 0000 to 9999.

4.The remaining nine bytes are devoted to the significand field, consisting of 18 packed BCD digits. Positive and negative numbers are represented with a significand normal-

Data Types and Data Storage

53

ized to the range 1.00...00 to 9.00...99. The decimal point following the first significand digit is implied. The special value 0 has an all-zero significand.

5.The special value FF hexadecimal in the number’s sign byte indicates an invalid number.

The structure of the BCD12 format is described in Table 3.4.

Table 3.4

Field Structure of the BCD12 Format

CODE

FIELD NAME

BITS WIDE

BCD DIGITS

 

RANGE

 

 

 

 

 

 

S

sign of number

4

1

0

- 1 (BCD)

S

sign of exponent

4

1

0

- 1 (BCD)

E

exponent

16

4

0

- 9999

M

significand

72

18

0

- 99..99 (18 digits)

 

 

-----

 

 

 

 

Format size

96 (12 bytes)

 

 

 

Notes:

1.The significand is scaled (normalized) to a number in the range 1.00..00 to 9.99..99.

2.The encoding for the value zero (0.00..00) is a special case.

3.The special value FFH in the sign byte indicates an invalid number.

The BCD12 format, as is the case in all BCD encodings, does not make ideal use of the available storage space. In the first place, each packed BCD digit requires four bits, which in binary could serve to encode six additional combinations. At a byte level the wasted space is of 100 encodings (BCD 0 to 99) out of a possible 256 (0 to FFH). The sign field in the BCD12 format is wasteful since only one binary digit is actually required for storing the sign. Regarding efficient use of storage, BCD formats cannot compete with floating-point binary encodings. The advantages of BCD representations are a greater ease of conversion into decimal forms, and the possibility of using the processors’ BCD arithmetic instructions.