4.3 Flags and Machine State
After an arithmetic instruction, a program often needs to know more than the stored result. Was the unsigned result too large? Is the result zero? Did signed arithmetic overflow? The 8086 records such facts as individual bits in the 16-bit FLAGS register.
Some flags describe a result; others control processor behavior.
The defined 8086 flags
| Bit | Name | Group | Meaning introduced here |
|---|---|---|---|
| 0 | CF — Carry Flag | status | carry out on addition or borrow on subtraction |
| 2 | PF — Parity Flag | status | low result byte contains an even number of 1 bits |
| 4 | AF — Auxiliary Carry Flag | status | carry or borrow across the low-nibble boundary |
| 6 | ZF — Zero Flag | status | result is zero |
| 7 | SF — Sign Flag | status | copy of the result's most significant bit |
| 8 | TF — Trap Flag | control | enables single-step behavior |
| 9 | IF — Interrupt Enable Flag | control | enables recognition of maskable external interrupts |
| 10 | DF — Direction Flag | control | selects increasing or decreasing index movement for string instructions |
| 11 | OF — Overflow Flag | status | signed result is outside the representable range |
Other bit positions are reserved in the original 8086 FLAGS word. Later x86 processors extend this register, but this tutorial is describing the 8086 state.
Carry and overflow answer different questions
The same bit pattern can represent unsigned or signed data. Therefore CF and OF must not be treated as synonyms.
For an 8-bit demonstration, calculate 7FH + 01H:
- As unsigned values, , which fits in 0 through 255. There is no ninth-bit carry, so CF=0.
- As signed two's-complement values, cannot fit in −128 through 127. The stored pattern
80Hrepresents −128, so OF=1.
Now calculate FFH + 01H. The stored byte is 00H with a ninth-bit carry, so CF=1 and ZF=1. Signed −1 plus 1 equals 0, which fits, so OF=0.
For subtraction, the 8086 sets CF when an unsigned borrow is needed. This convention makes 00H - 01H produce FFH with CF=1.
Lab 1 — make each arithmetic flag reveal itself
Change 8-bit operands and choose ADD or SUB. The analyzer exposes the full intermediate result and all six arithmetic flags, with missions that isolate signed overflow, unsigned carry, zero, and auxiliary carry.
Check your understanding
Reading the remaining status flags
- ZF asks whether every stored result bit is zero.
- SF copies the top stored result bit. For an 8-bit operation it copies bit 7; for a 16-bit operation it copies bit 15.
- PF examines only the low byte and becomes 1 for even parity. The byte
00000000has zero 1 bits, and zero is even, so PF=1.
- AF records a carry or borrow between bit 3 and bit 4. It supported decimal-adjust operations; it is still useful here for understanding that carries can occur inside a word even when CF is 0.
Not every instruction updates every flag. Never assume that FLAGS is completely recomputed after every operation; the instruction definition states which flags are changed, preserved, or undefined.
Control flags change future behavior
The three control flags do not summarize an arithmetic result:
- DF=0 makes supported string instructions advance SI and/or DI; DF=1 makes them retreat. This permits traversal from low to high addresses or high to low addresses.
- IF=1 allows the processor to recognize a pending maskable external interrupt at an eligible instruction boundary; IF=0 postpones that recognition. IF does not mask the non-maskable interrupt (NMI), and it is not a universal switch for processor-detected exceptions.
- TF=1 supports debugging by causing a trap after each instruction. A debugger can inspect the new machine state before allowing another instruction.
The diagram isolates concepts. Exact priority and timing are defined by the processor; the important beginner rule is that flags participate in the machine state that determines what happens next.
Lab 2 — control direction, interrupt recognition, and tracing
Copy bytes forward or backward, queue a maskable interrupt, and toggle single-step tracing. Missions require you to distinguish an interrupt that remains pending from one that is serviced.
Check your understanding
When analyzing a flag, always state the operation width and interpretation. “Overflow happened” is incomplete until you say whether you mean an unsigned carry, a signed overflow, or a carry between internal digit positions.