State Flags

horizontal rule

State Flags

horizontal rule

Let's start with the Zero Flag ZF, as it is the easiest to understand. If the result of an arithmetic or logical instruction which has been performed equals zero, then ZF is set, otherwise ZF is cleared. The means that after the instruction

SUB AX, AX

AX is equal to zero and the Zero Flag is set, ZF becomes equal to 1 which is denoted as ZR.
After the following instruction

ADD AX, 1

AX equals 1 and ZF is equal to NZ (cleared),
and after the next instruction

ADD AX, 0FFFFh

AX contains zero and ZF is equal to ZR (set) again.

bit #6d

horizontal rule

The next flag to consider is the Sign Flag SF which is especially important for instructions which process signed numbers. The Sign Flag can also be influenced by other instructions. The rule for this flag is:

If the highest bit of a result of an instruction which has been performed equals 1, then SF is set. Otherwise SF is clear.

For example, after the instruction

SUB AX, AX

AX = 0 and the Sign Flag is equal to PL (cleared, positive),
after the following instruction

SUB AX, 1

AX contains 0FFFFh and SF is now equal to NG (set, negative),
and after the next instruction

ADD AX, 2

AX contains 1 and SF equals to PL (cleared).

bit #7d

horizontal rule

Next in line is the Carry Flag CF which indicates the carry from, or the borrow to, the highest bit of the result of an arithmetic instruction. The value of the Carry Flag (1 if set or 0 if clear) is also used in performing arithmetic operations with long binary numbers.

Let's look at these simple examples.
After the instruction

SUB AX, AX

The Carry Flag is equal to NC (is cleared, nor carry),
after the instruction

MOV AX, 0FFFFh

The Carry Flag remains unchanged,
after the following instruction

ADD AX, 1

the value of the Carry Flag is CY (set, carry - yes!).,
and after

ADD AX, 0

The Carry Flag takes the value NC (cleared).

The Carry Flag can also be used as an indicator by certain DOS and BIOS routines. In this case its value is changed by special instructions - STC (SeT Carry) and CLC (CLear Carry). Later when BIOS and DOS services are covered, we'll consider the use of the Carry Flag.

bit #0d

horizontal rule

The Auxiliary Carry Flag AF is similar to the Carry Flag. It is designed for operations with BCD numbers, so it marks whether there have been carries from or borrows to bit #3d (counting from the right - don't forget about bit #0d) of the result. For example the instruction

SUB AX, AX

makes AF equal to NA (cleared),
the instruction

MOV AX, 8

does not change the AF flag,
the following instruction

ADD AX, 8

makes it equal to AY (set), and

ADD AX, 0

makes the Auxiliary Carry Flag equal to NA (cleared).

bit #4d

horizontal rule

The Overflow Flag OF, together with other state flags, reflects peculiarities of arithmetic operations.

bulletFirst, it is set after the addition or subtraction of signed numbers if the result does not fit into the operand, otherwise it is clear. In other words, when adding integers of the same sign, a carry from the (n-2)-th bit to the (n-1)-th bit (the sign bit) will result in the Overflow Flag being set. Similarly, a borrow from the sign bit obtained while subtracting two integers of opposite signs will set the Overflow Flag.
bulletSecondly, if the high half of the result of a multiplication is not equal to zero, then both CF and OF are set, otherwise they are both cleared.

The sequence of instructions below should help you to understand the OF flag:

	SUB AX, AX	;Clear AX - OF must be NV (cleared)
	MOV AL, 128	;OF is unchanged
	ADD AX, 128	;OF is set (equal to OV)

bit #11d

horizontal rule

The next flag - the Parity Flag is rarely used nowadays, which is probably why it is often misunderstood.

The rule for the Parity Flag PF is a bit unusual, but very simple:

If the lower byte of the result of the instruction, which has just been performed has an even number of bits equal to 1,
the
PF is set.
Otherwise it is clear.

This means that the instruction

MOV AX, 0F001h

does not change the Parity Flag,
while the instruction

ADD AX, 2

sets the PF flag to PE (set),
and the next instruction

ADD AL, 5h

makes the PF flag equal to PO (cleared).

bit #2d

horizontal rule

Later, in another Chapter, I will use these codes with real examples while I demonstrate the use of CodeView Debugger.

horizontal rule

Flags Chapter 4....Control Flags

Revised - Tuesday, October 26, 1999 06:52 PM Central Daylight Time


Página cedida por Luis Piñuel