Flags Register

The flags register (RFLAGS) is a 64 bit bitfield register. It contains a few bits of information that is designed to be accessible by any program, but not necessarily writable.

It is accessible through three overlapping "registers": FLAGS for the lower 16 bits, EFLAGS for the lower 32, and RFLAGS for all 64. Despite the fact that only bits 0 through 21 are defined, RFLAGS exists for consistency with the rest of the x86-64 register set.

List of Fields

In ascending order (least significant to most significant bit), the register contains the following bits of information:

CF - Carry Flag (bit 0)
Indicates if an arithmetic operation generated a carry or borrow out of the most significant bit of the result. It indicates an overflow or underflow for unsigned arithmetic.
VF - "V" Flag (bit 1)
Reserved. Always set.
See below.
PF - Parity Flag (bit 2)
Indicates if the least significant byte of a result contains an even number of 1s (meaning there is parity), and cleared otherwise. As its name implies, it is used for operations involving parity (such as data transmission). For those situations, there exists two instructions: JP/JPE - Jump If Parity (Even) and JNP/JPO - Jump If No Parity (Odd).
It is obsolete, but continues to exist for compatibility reasons.
(bit 3)
Reserved. Always cleared.
AF - Auxiliary Cary Flag (bit 4)
Sometimes referred to as the "Half Carry Flag" it is similar to the carry flag (bit 0), but always operates on overflow or underflow involving bit 3 of the result. The purpose of this flag is for BCD instructions such as the AAA - ASCII Adjust After Addition instruction.
It is obsolete, but continues to exist for compatibility reasons.
KF - "K" Flag (bit 5)
Reserved. Always cleared.
See below.
ZF - Zero Flag (bit 6)
As the name implies, this flag is set if the result is zero, and cleared otherwise.
SF - Sign Flag (bit 7)
As the name implies, this flag is set to the most significant bit (the sign bit) of the result.
TF - Trap Flag (bit 8)
Enables single-step debugging of a thread.
IF - Interrupt Enable Flag (bit 9)
Controls whether the processor should respond to maskable interrupts or not. It does not inhibit non-maskable interrupts.
DF - Direction Flag (bit 10)
Controls the "direction" of string instructions. When set, the instructions will decrement (process from high to low addressed), and when cleared, they will increment (low to high).
OF - Overflow Flag (bit 11)
Indicates if an arithmetic operation generated a carry or borrow out of the second most significant bit of the result. It indicates an overflow or underflow for signed arithmetic.
IOPL - I/O Privilege Level (bits 12 and 13)
Indicates the processor ring of a thread. A zero indicates the most privilege, while a three indicates the least.
On the 8086 and 80186, bit 12 is always set, and bit 13 is always cleared.
NT - Nested Task (bit 14)
Controls if the current thread is linked to a previously executed thread.
On the 8086 and 80186, this is always set.
(bit 15)
Reserved.
On the 8086 and 80186, this is always set. Later models have this always cleared.
RF - Resume Flag (bit 16)
Controls whether the processor responds to debug exceptions.
VM - Virtual-8086 Mode (bit 17)
Controls whether the processor is in Virtual-8086 mode (inside of Protected Mode).
AC - Alignment Check / Access Control (bit 18)
A user-controllable flag that can mask the AM bit in the CR0 register. These flags (if both set) force alignment checking on data in memory.
VIF - Virtual Interrupt Flag (bit 19)
TODO
VIP - Virtual Interrupt Pending (bit 20)
TODO
ID - ID Flag (bit 21)
Provides a method of checking for support of the CPUID instruction. On the 80486 and older processors, attempts to set this bit will fail as the processor will just clear it. However, Pentium (and newer) processors (which support CPUID) will allow writing to this bit.
Therefore, support for the CPUID instruction can be checked by setting this bit, storing it in the flags register, then rereading that register again. If this bit is still set, the CPU supports CPUID. If the bit is cleared when read back, the processor is an 80486 or older, and other methods must be used to determine which processor is running (and therefore, which features are available).
(bits 22 and up)
Reserved. Always cleared.

V and K Flags

On the 8085 (the predecessor to the 8086), there exists an 8 bit flag register. It is the basis for the x86 flags register of today, and, short of two changes, was the same as the lowest 8 bits of x86's FLAGS register. Those two changes are: the undocumented "V" and the "K" flags.[VKFlags] These reside in bits 1 and 5, respectively.

The V flag resides in bit 1 of the flag register and simply indicated a signed overflow or underflow.

The V flag is simply the exclusive-or of the carry into the top bit and the carry out of the top bit. This is a standard formula for computing overflow for signed addition and subtraction.[VKFlags]

The K flag resides in bit 5 of the flag register and stores the result of exclusive ORing (XOR) the V flag with the most significant bit of the result.

One mystery was the purpose of the K flag: "It does not resemble any normal flag bit." Its use for increment and decrement is clear, but for arithmetic operations why would you want the exclusive-or of the overflow and sign? It turns out the the K flag is useful for signed comparisons. If you're comparing two signed values, the first is smaller if the exclusive-or of the sign and overflow is 1. This is exactly what the K flag computes.[VKFlags]

These bits were removed with the introduction of the 8086 and defined to be a 1 for the V flag (bit 1) and a 0 for the K flag (bit 5). To this day, these fixed values have stayed, and attempts to change them, or any other reserved bits, is undefined.

References