When the x86 Arithmetic Logic Unit (ALU) performs operations like
ADD, it flags the results of these operations ("became zero", "overflowed", "became negative") in a special 16-bit
FLAGS register. 32-bit processors upgraded this to 32 bits and called it
EFLAGS, while 64-bit processors upgraded this to 64 bits and called it
But no matter the name, the register is not directly accessible (except for a couple of instructions - see below). Instead, individual flags are referenced in certain instructions, such as conditional Jump or conditional Set, known as
cc means "condition code" and references the following table:
|Not Equal, Not Zero|
|Carry, Below, Not Above or Equal|
|No Carry, Not Below, Above or Equal|
|Above, Not Below or Equal|
|Not Above, Below or Equal|
|Greater or Equal, Not Less|
|Not Greater or Equal, Less|
|Greater, Not Less or Equal|
|Not Greater, Less or Equal|
In 16 bits, subtracting
0 is either
-1 depending on whether unsigned or signed arithmetic is used - but the destination holds
0xFFFF either way. It's only by interpreting the condition codes that the meaning is clear. It's even more telling if
1 is subtracted from
0x8000: in unsigned arithmetic, that merely changes
32,767; while in signed arithmetic it changes
32,767 - a much more noteworthy overflow!
The condition codes are grouped into three blocks in the table: sign-irrelevant, unsigned, and signed. The naming inside the latter two blocks uses "Above" and "Below" for unsigned, and "Greater" or "Less" for signed. So
JB would be "Jump if Below" (unsigned), while
JL would be "Jump if Less" (signed).
The above condition codes are useful for interpreting predefined concepts, but the actual flag bits are also available directly with the following two instructions:
AHregister with Flags
AHregister into Flags
Only certain flags are copied across with these instructions. The whole
RFLAGS register can be saved or restored on the stack:
FLAGSonto/from the stack
EFLAGSonto/from the stack
RFLAGSonto/from the stack
Note that interrupts save and restore the current
[R/E]FLAGS register automatically.
As well as the ALU flags described above, the
FLAGS register defines other system-state flags:
IFThe Interrupt Flag.
STIinstruction to globally enable interrupts, and cleared with the
CLIinstruction to globally disable interrupts.
DFThe Direction Flag.
MOVS(to compare and move between memory locations) automatically increment or decrement the index registers as part of the instruction. The
DFflag dictates which one happens: if cleared with the
CLDinstruction, they're incremented; if set with the
STDinstruction, they're decremented.
TFThe Trap Flag. This is a debug flag. Setting it will put the processor into "single-step" mode: after each instruction is executed it will call the "Single Step Interrupt Handler", which is expected to be handled by a debugger. There are no instructions to set or clear this flag: you need to manipulate the bit while it is in memory.
To support the new multitasking facilities in the 80286, Intel added extra flags to the
IOPLThe I/O Privilege Level.
002 being most privileged and
112 being least. If
IOPLwas less than the current Privilege Level, any attempt to access I/O ports, or enable or disable interrups, would cause a General Protection Fault instead.
NTNested Task flag.
CALLed another Task, which caused a context switch. The set flag told the processor to do a context switch back when the
The '386 needed extra flags to support extra features designed into the processor.
RFThe Resume Flag.
VMThe Virtual 8086 Flag.
VMflag indicated that this Task was a Virtual 8086 Task.
As the Intel architecture improved, it got faster through such technology as caches and super-scalar execution. That had to optimise access to the system by making assumptions. To control those assumptions, more flags were needed:
ACAlignment Check flag The x86 architecture could always access multi-byte memory values on any byte boundary, unlike some architectures which required them to be size-aligned (4-byte values needed to be on 4-byte boundaries). However, it was less efficient to do so, since multiple memory accesses were needed to access unaligned data. If the
ACflag was set, then an unaligned access would raise an exception rather than execute the code. That way, code could be improved during development with
ACset, but turned off for production code.
The Pentium added more support for virtualising, plus support for the
VIFThe Virtual Interrupt Flag.
IF- whether or not this Task wants to disable interrupts, without actually affecting Global Interrupts.
VIPThe Virtual Interrupt Pending Flag.
VIF, so when the Task does an
STIa virtual interrupt can be raised for it.
CPUIDinstruction. A Virtual monitor could disallow it, and "lie" to the requesting Task if it executes the instruction.