• Keine Ergebnisse gefunden

Flags

Im Dokument ASSEMBLY LANGUAGE (Seite 69-73)

Certain results of data manipulations are distinguished or denoted by flags. The flags that are affected by data manipulations are AF, CF, OF, PF, SF, and ZF.

The four basic mathematical operations (addition, subtraction, multiplication and division) are provided by the processor. 8-and l6-bit operations are available, as are signed 8-and unsigned arithmetic. The representation of signed values is by standard twos complement arithmetic. The addition and subtraction operations serve as both signed and unsigned operations; the two possibilities are distinguished by the flag settings.

Arithmetic may be performed directly on unpacked decimal digits or on packed decimal representations.

Some operations indicate these results only by setting flags.

For example, the processor implements "compare" as a special subtract which does not change either operand but does set flags to indicate a zero, positive, or negative result.

By using one of the conditional jump instructions, a program can test the setting of five of the flags (carry, sign, zero, overflow, and parity). The flow of program execution can be altered based on the outcome of a previous operation. One more flag, the auxiliary carry flag, is used by the ASCII and decimal-adjust instructions.

It is important to understand which instructions set which flags. Suppose you wish to load a value into AX, and then test whether the value is

o.

The MOV instruction does not set ZF, so the following does not work:

MOV AX, wData JZ Zero

Instead, since ADD does set ZF, the following does work:

MOV AX, wDa ta ADD AX, 0 JZ Zero

A flag can be set, but not tested, over the duration of several instructions. In such cases, the intervening instructions must be carefully checked to ascertain that they do not affect the flag in question. This is generally a dangerous programming practice.

(See Appendix A for the flags set by each instruction.)

Flag Usage

Most arithmetic operations set or clear six flag registers.

"Set" means set to I, and "clear" means clear to O.

Auxiliary Carry Flag (AF)

If an operation results in a carry out of or a borrow into the low-order four bits of the result, AF is set; otherwise it is cleared. A program cannot test this flag directly: i t is used solely by the decimal adjust instructions.

Carry Flag (CF)

If an operation results in a carry out of (from addition) or a borrow into (from subtraction), the high-order bit of the result, CF is set; otherwise it is cleared.

This flag usually indicates whether an addition causeS a "carry"

into the next higher order digit or a subtraction causes a

"borrow. II CF is not, however, affected by increment (INC) and decrement (DEC) instructions. CF is set by an addition that causes a carry out of the high-order bit of the destination, and cleared by an addition that does not cause a carry. CF is also affected by the logical AND, OR, and XOR instructions.

The contents of an operand are moved one or more positions to the left or right by the rotate and shift instructions. The carry flag is treated as if it were an extra bit of the operand. Only RCL and RCR preserve the original value in CF. The value does not, in these cases, remain in CF. The value is replaced with the next bit rotated out of the source. If an RCL is used, the value in CF is replaced by the high-order bit and goes into the low-order bit. If an RCR is used, the value in CF is replaced by the low-order bit and goes into the high-order bit. (This is useful in multiple-word arithmetic operations.) In other rotates and shifts, the value in CF is lost.

Overflow Flag (OF)

If a signed operation results in an overflow, OF is set;

otherwise it is cleared. (That is, an operation results in a carry into the high-order bit of the result but not a carry out of the high-order bit, or vice versa.)

Parity Flag (PF)

If the modulo 2 sum of the low-order eight bits of an operation is 0 (even parity), PF is set; otherwise i t is cleared (odd parity) .

Following certain instructions, the number of one bits in the destina tion is counted and the parity flag set if the number is even and cleared if the number is odd.

Sign Flag (SF)

If the high-order bit of the result is set, SF is set; otherwise it is cleared.

Following an operation, the high-order bit of its target can be interpreted as a sign. The SF flag is set equal t? this high-order bit by instructions that affect SF. Bi t 7 ~ s the high-order bit of a byte and bit 15 is the high-high-order bit of a word.

Zero Flag (ZF)

If the result of an operation is 0, ZF is set: otherwise i t is cleared.

Following certain zero flag is set, flag is cleared.

carry and a zero.

00110101 +11001011

06000600

operations, if the destination is zero, the and if the destination is not zero, the zero Both ZF and CF are set by a result that has a

Here is an example:

Carry Flag Zero Flag

1 1

8 MACRO ASSEMBLER

Im Dokument ASSEMBLY LANGUAGE (Seite 69-73)