Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8632

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

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.



Chapter 4

Digital Logic, Arithmetic, and Conversions

This chapter is about the fundamental arithmetic and logical operations of digital machines. It serves as a background for developing processing routines which involve decisions, data filtering and processing, and number crunching. Here we discuss logical and arithmetic operations in general, that is, without reference to any individual processor. There are so many different hardware versions of microcontrollers that it is not feasible to develop an actual routine for each device. On the other hand, once the logic is understood, the actual coding is a simple process of finding a way of implementing it in a specific instruction set. The chapter also includes material related to data type conversions since these operations are closely related to the other material in this chapter.

4.0 Microcontroller Logic and Arithmetic

All microcontrollers contain instructions to perform arithmetic and logic transformations on binary or decimal operands. These instructions can be classified into three groups:

1.Logical instructions. Sometimes these are called Boolean operators. The group includes instructions with mnemonics such as AND, NOT, OR, and XOR. They perform the logical functions that correspond to their names.

2.Arithmetic instructions. Typically this group of instructions performs integer addition and subtraction. Occasionally, the instruction set includes multiplication and division. The operands can be signed or unsigned binary and binary coded decimal numbers.

3.Auxiliary and bit manipulation instructions. This group includes instructions to shift and rotate bits, to compare operands, to test, set, and reset individual binary digits, and to perform various auxiliary operations.

4.0.1 CPU Flags

All microcontrollers are equipped with a special register that reflects the current processing status. This register, sometimes called the status register or the flags register, contains individual bits, usually called flags, that are meaningful during the execution of logic and arithmetic operations. The most common flags are:

55

56

Chapter 4

1.The zero flag. This flag is set if a previous operation produces a value of zero.

2.The carry/overflow flag. This flag is set if there has been a carry or a borrow-out of the high-order bit of the operand.

3.The half-carry or digit-carry flag. This flag is set if there has been a carry or a bor- row-out of the low-order nibble of the operand.

Not all instructions affect all the flags. For example, loading a zero constant into a register may be said to produce a zero value; however, such an instruction may or may not affect the zero flag, according to the implementation on each particular device. More powerful and sophisticated microcontrollers sometimes implement other flags, such as flags to indicate a negative operand, an arithmetic overflow, or an interrupt.

4.0.2 Word Size

The word-size of a computer or a digital device refers to the number of bits used in storing data and in moving data in and out of the various machine units. In other words, a machine’s word-size is the native data unit for a particular architecture. In this manner we speak of the Pentium having a 32-bit word size or the PIC16x84 having an 8-bit word-size for data operations and 14-bit program words.

In the context of digital arithmetic and logic the data word-size determines the processing capabilities of each device. For example, a machine with an 8-bit word-size can perform unsigned addition of operands whose sum does not exceed the decimal value 255, since 255 is the largest unsigned integer that can be stored in eight bits. However, a machine with 16-bit words can perform unsigned additions up to a sum of 65,535 since it is the largest number that can be stored in 16 bits.

Therefore, the coding of numerical routines is determined by the word size of the machine or device. A device with 8-bit word-size requires multi-byte arithmetic to perform addition that exceeds a sum of 255, while a machine with a 16-bit word can do direct addition up to the sum 65,535. Considering that most popular microcontrollers have 8-bit word-sizes, we assume this limit in the arithmetic and logic algorithms and routines developed in this chapter.

4.1 Logical Instructions

The logical instructions include the Boolean operators, AND, OR, NOT, and XOR, as well as instructions to shift and rotate individual bits.

The logical instructions operate on a bit-by-bit basis; therefore, in the AND, OR, NOT, and XOR there is no interaction between bits. The action performed by the logical instructions is as follows:

1.AND, OR, and XOR logically combine each bit in the source operand with the corresponding bit in the destination operand. The result does not affect the neighboring bits.

2.The NOT operator inverts all bits in the destination operand.


Digital Logic, Arithmetic, and Conversions

57

These actions explain the term bitwise operation sometimes used to describe the instructions.

4.1.1 Logical AND

The AND instruction performs a bitwise logical AND of two operands. This determines that a bit in the result is set if and only if the corresponding bits are set in both operands. A frequent use of the AND operation is to clear one or more bits without affecting the remaining ones. This action is possible because ANDing with a 0 bit always clears the result bit and ANDing with a 1 bit preserves the original value of the first operand.

For example, if we have the binary coded decimal number 34 packed into a single byte, we can isolate the four low-order bits as follows:

hexadecimal

binary

34

0011

0100

AND

0F

0000

1111

mask

-------------

---------

04

0000

0100

The second operand, in this case 0FH, is called a mask. The AND operation preserves the 1-bits in the mask and clears the bits that are 0. Consequently, the mask 00000001B clears the seven high-order bits and preserves the original value of the low-order bit.

4.1.2 Logical OR

The OR operation performs the bitwise logical inclusive OR of two operands. After a logical OR, a bit in the result is set if one or both of the corresponding bits in the operands were set. A frequent use for the OR is to selectively set one or more bits. The action takes place because ORing with a 1-bit always sets the result bit, while ORing with a 0-bit preserves the original value in the first operand.

For example, to set the high-order bit (bit number 7) we can OR with a 1 bit, as follows:

hexadecimal

binary

34

0011

0100

OR

80

1000

0000

mask

----

---------

B4

1011

0100

The OR operation sets the bits that are 1 in the mask and preserves the bits that are masked 0.

4.1.3 Logical XOR

The XOR operator performs the bitwise logical exclusive OR of the two operands. Therefore, a bit in the result is set if the corresponding bits in the operands have opposite values. For this reason, XORing a value with itself always generates a zero result since all bits necessarily have the same value. On the other hand, XORing with a 1-bit inverts the value of the other operand, since 0 XOR 1 is 1 and 1 XOR 1 is 0. This toggling action of XORing with a 1 bit generates identical bitwise results as the NOT operation,