EFLAGS Register 

Status and Flags Register

In programming, many times the programmer or the developer need to monitor the kind of result produced from the execution of arithmetic instructions and then based on the status report the program will perform specific tasks.

To fulfill that demand, Intel processors provide us the 32-bit register named EFLAGS consisting of several flags. Based on these flags, the CPU is directed to which sequence of instructions it should be executing.

The flags are grouped into:
Status Flags
Control Flags
System Flags

Status Flags

We can monitor the kind of result produced from the execution of arithmetic instructions by evaluating the status flags of the EFLAGS register.

There are six status flags defined in the following tables:

NameStands forMeaning
OFOverflowOF set to 1 if the result of signed number operation is out-of-range
SFSignSF set to 1 if the leftmost (i.e. most significant) bit of the result is 1.
ZFZeroZF set to 1 if the result is equal to 0.
AFAuxiliary CarryAF set to 1 if there is a carry out of/borrow to the least significant hexadecimal digit or bit#3 of the result.
PFParityPF set to 1 if the rightmost (i.e. least significant) 8-bits of the result contain an even number of ones.
CFCarry FlagCF set to 1 if there is a carry out of the most significant bit of the result. This indicates that the result of unsigned number operation is out-of-range.

Control Flag

There is only one control flag, named DF (Direction Flag). We use control Flag only for string instructions.

System Flag

In addition, because of its very complex architecture and its sophisticated capability, the Pentium processor provides us many additional flags to monitor and control the overall operations. These flags are useful for Operating System designer, Security management, and for supporting multimedia applications. They controls I/O, maskable interrupts, debugging, task switching, and virtual-8086 mode.

However, most of these flags cannot be manipulated in real-mode programming.

 System Flags

Overflow Flag and Carry Flag

Both are indicators of out-of-range condition. Overflow flag is used to evaluate an out-of-range condition of signed number operations whereas Carry flag is used in unsigned number operations. Since a binary number can represent an unsigned number or signed number, the processor computes both flags and the user checks the appropriate flag to check if the result is out of range or not.

Overflow Flag is set to 1 if both operands have the same sign and the result has a different sign. Otherwise, OF is reset to 0.

Subtraction operation A - B can be performed as operation A + (-B).

Logically, we can formulate the OF as:

OF = (NOT a AND NOT b AND c) OR (a AND b AND NOT c)

a is the sign bit of the first operand; b is the sign bit of the second operand; and c is the sign bit of the result.

The Pentium processor performs a subtraction operation A - B as an addition operation A + (-B). (-B) is obtained by finding the two's complement of B.

There are four possible conditions happen due to arithmetic operations.

OF=0, CF=0
OF=0, CF=1
OF=1, CF=0
OF=1, CF=1

Example: Addition 0Fh + 08h
 
AL = 0Fh 0
0001111
BL = 08h 0
0001000
AL+BL=17h00
0010111
CF=0, OF=0,Both operands and the result are positive. PF=1, ZF=0, SF=0, AF=1

Example: Addition 0Fh + F8h
 
AL = 0Fh 0
0001111
BL = F8h 1
1111000
AL+BL=07h10
0000111
CF=1, OF=0,Operands have different sign bits. PF=0, ZF=0, SF=0, AF=1 As a signed operation, 15 + (-8) = 7 (ok). As an unsigned operation, 15 + 248 = 263 > 255 (out-of-range).

Example: Addition 4Fh + 40h
 
AL = 4Fh 0
1001111
BL = 40h 0
1000000
AL+BL=8Fh01
0001111
CF=0, OF=1, The result and operands have different sign bits. PF=0, ZF=0, SF=1, AF=0 As a signed operation, 79 + 64 = 143 > 127 (out-of-range). As an unsigned operation, 143 < 255 (ok).

Example: Addition F8h + 81h
 
AL = F8h 1
1111000
BL = 81h 1
0000001
AL+BL=79h10
1111001
CF=1, OF=1, Operands are negatives, the result is positive. PF=0, ZF=0, SF=0, AF=0 As a signed operation, -8 + -127 = -135 < -128 (out-of-range). As an unsigned operation, 248 + 129 = 377 > 255 (out-of-range).

However, compare the above examples with the following examples:

Example: Subtraction 0Fh - 08h
 
AL = 0Fh 0
0001111
BL = -08=F8h 1
1111000
AL+BL=07h00
0000111
CF=0 Compare it with the addition 0Fh + F8h, OF=0 PF=0, ZF=0, SF=0, AF=0 (Why?) As a signed operation, 15 - 8 = 7 (ok). As an unsigned operation, 15 - 8 = 7 > 0 (ok).

Example: Subtraction F8h - 7Fh
 
AL = F8h 1
1111000
BL = -7Fh = 81h 1
0000001
AL+BL=79h10
1111001
CF=0, OF=1 PF=0, ZF=0, SF=0, AF=1 (Why?) As a signed operation, -8 -127 = -135 < -128 (out-of-range).
As an unsigned operation, because of no negative -8=248. Thus, 248 - 127 = 121 < 255. (Ok)

Flag Manipulation Instructions

Pentium accommodates with some instructions to manipulate the flags directly.

InstructionsUsage
CLCCF = 0
STCCF = 1
CMCTo complement CF. If the previous CF is 0, then the result is 1.