• Aucun résultat trouvé

Boolean Arithmetic

Dans le document II II II II II (Page 143-147)

Boolean arithmetic refers to the logic operators. High-level language users will be most familiar with these commands in reference to conditional statements. We have all used ex-pressions like

IF A>15 AND C=7 THEN ...

or

IF J<3 OR K=2 THEN ...

and, less frequently, IF NOT L=4 THEN ...

AND, OR and NOT are three of the various Boolean

mathematical functions. When used in conditional statements, they serve as logic operators. Programmers who use the BASIC graphics GET and PUT commands should also be familiar with these operations. With the graphics commands (as in machine language), however, their bit-oriented nature is more apparent.

The 8088 has four Boolean arithmetic commands, AND, OR, XOR, and NOT. The Boolean operators have the general format shown below. The operator is one of the four Boolean arithmetic commands. The function is the operation performed by the operator. Any source or destination combination legal with commands such as ADD or SUB is legal with the Boolean operators. Note that the operator NOT has only one operand which acts as both the source and the destination. The Bool-ean commands can perform their operations on either bytes or words.

OPERATOR destination,source

destination = source FUNCTION destination

AND. We all understand the logical significance of the English word and. In the statement "Send Jack and Jill to the well," it is clear that both Jack and Jill are supposed to go to

135

8

Advanced Arithmetic

the well. In high-level languages, the AND operator serves a similar purpose. It is generally used to link two logical state-ments together. When both of the logical statestate-ments are true, the entire statement is true. In machine language, AND is a little different.

The AND operation inspects each bit of its two operands and sets the destination as follows:

o

AND 0 = 0

o

AND 1 = 0 1 AND 0 = 0 1 AND 1 = 1

In other words, a bit will be set in the destination only if it is set in both the source and the destination. For example, if we start with

11110000B and 11010111B

after ANDing these two numbers together, we obtain the result

11110000B AND 11010111B 11010000B

Every time the corresponding bits are both 1, the result is a 1.

If a 1 and a 0 line up together, then the resulting bit is O.

The AND operator can also be used to mask off unwanted portions of a number. For example, we can isolate the lower nybble of a BCD packed byte (held in BL) using the

instruction:

AND BL,OFH ;(OFH=OOOOl111B)

8

8

II

This operation tells the microprocessor to AND the contents of

BL with OFH, and store the result in BL. For example, if BL _ holds 01010011B,

01010011B contents of BL

AND 00001111B

8

00000011B

The upper nybble of BL has been masked off. This is useful when you only want to deal with part of a number. For in-stance, the sample program in the last chapter used AND to extract the low nybble from a number.

136

II

II

II

8

a

II

- " II

8

Advanced Arithmetic

You can also use this operation to isolate a single bit; you simply AND the number you are inspecting with the appro-priate mask byte.

For example, if you want to isolate bit 5 (the bit representing the decimal value 32), you would use:

AND destination,32 ;(32D=00100000B)

This might prove useful in graphics applications.

Inspecting bits in this way proves so useful that Intel en-gineers provided the 8088 with another AND instruction called TEST. TEST is identical to AND in all respects, except that the result of the AND is not stored. For example, if you use

TEST destination,16

the flags will be set just as in the operation AND destination,16

but the value of the destination will be unchanged. After such a TEST, you can JZ (Jump if Zero) or JNZ (Jump if Not Zero) to check for either a clear or set bit.

One often finds code such as AND AX,AX

or

TEST AX,AX

This command is used to set the flags (PF, SF, or ZF) accord-ing to the value of AX. Note that the value of AX is

unchanged.

OR. The OR operator is, in a sense, the converse of the AND operation. If we change our English example to read

"Send Jack or Jill to the well," it takes on a new meaning.

Now we are saying that either Jack or Jill (or both of them, making this OR inclusive) should go to the well.

The OR operation inspects the bits of the source and destination. The bits of the result are set according to the following rules:

o

OR 0 = 0

o

OR 1 = 1

lOR 0 = 1 lOR 1 = 1

If either (or both) of the bits is I, the resulting bit is also 1.

137

8

Advanced Arithmetic

Only when both of the bits are 0 is the result O. For example, if we start with the numbers

01010100B and 11101010B

and OR them together, we obtain the result 01010100B

OR 11101010B 11111110B

This operation has combined the two numbers (do not confuse this with adding them together). Whereas AND is used to separate two numbers, OR is used to put them together. For example, we could use OR to overlap two graphics images or to pack unpacked BCD digits (see the section on bit shifting in this chapter).

Programmers sometimes use code such as OR AX/AX

when they want to set the flags according to the value of AX.

AX is not changed, but the SF, ZF, and PF flags are set appropriately.

XOR. The Exclusive OR operation sets the bits of the re-sult according to the following rules:

o

XOR 0 = 0

o

XOR 1 = 1

1 XOR 0 = 1 1 XOR 1 = 0

A bit in the result is set only if the two bits of the operands differ.

XOR is used to invert specific bits. If we start with the two numbers

11110000B and 10010111B

in AL and BL respectively, and perform XOR AL/BL

11110000B XOR 10010111B 01100111B

AL will hold 01100111B. XOR is very useful for graphics applications. (See Chapter 12 for a discussion of XOR in ref-erence to computer graphics.)

Programmers sometimes use code such as:

138

II

II

I

II II

II

II

a

a

I

- a

II

XOR AX,AX

8

Advanced Arithmetic

when they want to zero a register. To zero a register with the MOV instruction requires more bytes than with XOR. If you need to make a program compact, you can use XOR

register,register (or SUB register,register) when you need to zero a register. (IBM programmers do this in the ROM BIOS; it's a fairly common technique.)

NOT. The NOT instruction has the general format shown below. The source can be any general register, or an addressed memory location. NOT can be used on both bytes and words. After a NOT is performed, the result replaces the source value.

NOT source

NOT reverses the bits of the operand value. All of the l's are made O's, and all of the O's are made l's. In other words, it follows the rules

NOT 0 = 1 NOT 1 = 0

Generally, NOT is used to negate a number. The 8088 pro-vides a negate instruction (NEG), but it can be used only on bytes or words. You cannot use NEG, for example, on a 32-bit number. To negate a 32-bit number, you must first NOT the two words and then add 1 to the result. The sample code be-low negates a 32-bit number stored in AX:DX (AX holds the least significant word).

NOT AX ;take the ones complement of the number NOTDX

ADD AX,l ;add 1 to the result for twos complement ADC DX,O

Dans le document II II II II II (Page 143-147)