Flag Register 8086
Status flags and control flags (DF, IF, TF).
The flag register of the 8086 is a 16-bit register that reflects the outcome of arithmetic and logical operations and controls certain processor behaviors. Understanding each flag, its condition for setting, and how instructions interact with it is critical for writing correct assembly programs and for answering GATE questions on conditional branching.
Status Flags
Status flags are set or cleared automatically by the processor after arithmetic and logical operations. They reflect the nature of the result and are used by conditional jump instructions to make branching decisions.
The Carry Flag (CF) is set when an arithmetic operation produces a carry out of or a borrow into the most significant bit. For an 8-bit addition, CF is set if the result exceeds FFH. For subtraction, CF acts as a borrow flag.
The Zero Flag (ZF) is set when the result of an operation is zero. The Sign Flag (SF) copies the most significant bit of the result, indicating whether the result is negative in signed arithmetic. The Overflow Flag (OF) indicates signed arithmetic overflow, meaning the result is too large or too small to be represented in the destination.
The Parity Flag (PF) is set if the lower 8 bits of the result contain an even number of 1-bits. The Auxiliary Carry Flag (AF) records a carry or borrow between bit 3 and bit 4 of the result. AF is used by the DAA (Decimal Adjust after Addition) instruction for BCD arithmetic correction.
Control Flags
Control flags are not set by arithmetic results but are explicitly set or cleared by the programmer to modify processor behavior. The Direction Flag (DF) controls string operation direction. When DF=0, string instructions such as MOVSB increment SI and DI after each operation (low to high address). When DF=1, they decrement (high to low address). STD sets DF; CLD clears it.
The Interrupt Flag (IF) enables or disables response to maskable interrupts on the INTR pin. STI sets IF (enables interrupts) and CLI clears it (disables interrupts). Non-maskable interrupts (NMI) are unaffected by IF. The Trap Flag (TF) enables single-step mode for debugging. When TF=1, the processor generates a Type 1 interrupt after each instruction, allowing debuggers to step through code one instruction at a time.
Mathematical Conditions for Flag Setting
For unsigned addition of two n-bit numbers A and B, the Carry Flag is set when A + B is greater than or equal to 2^n. For signed overflow, OF is set when two positive numbers add to a negative result, or two negative numbers add to a positive result. Mathematically, OF = Carry into MSB XOR Carry out of MSB.
Numerical Example
Consider an 8-bit addition to demonstrate flag behavior. The result determines which flags are set. After ADD AL, BL, the processor updates all status flags based on the 8-bit result.
Given:
AL = 0B4H (180 decimal, binary: 1011 0100)
BL = 0A8H (168 decimal, binary: 1010 1000)
Why this formula applies:
Adding two unsigned 8-bit values. If sum > FFH, CF is set. If MSB of result = 1, SF is set.
Formula:
Result = AL + BL (8-bit addition)
Substitution:
0B4H + 0A8H = 15CH
Calculation:
15CH in 8-bit = 5CH (carry out of bit 7)
Binary result (8-bit) = 0101 1100
Flags set after ADD AL, BL:
CF = 1 (carry out of MSB, 15CH > FFH)
ZF = 0 (result 5CH is not zero)
SF = 0 (MSB of 5CH = 0, positive result)
OF = 1 (positive + positive = positive but CF and MSB-carry differ: overflow in signed interpretation)
PF = 1 (0101 1100 has 4 ones, even parity)
Final Answer: CF=1, OF=1, ZF=0, SF=0, PF=1Exam Tip: OF is for signed overflow; CF is for unsigned overflow. For GATE, JA/JB use CF (unsigned), while JG/JL use SF and OF (signed). TF causes a Type 1 interrupt after each instruction for debugging. IF does not affect NMI.
Loading lab...
Quick Revision
- 8086 flag register is 16-bit; 6 status flags (CF, PF, AF, ZF, SF, OF) and 3 control flags (DF, IF, TF).
- CF = carry/borrow for unsigned; OF = overflow for signed arithmetic.
- DF=0: string ops increment SI/DI. DF=1: string ops decrement SI/DI. CLD clears DF, STD sets DF.
- IF=1 enables INTR; IF=0 disables INTR. NMI is never masked by IF.
- TF=1 causes single-step (Type 1) interrupt after each instruction for debugging.
- AF is used by DAA/DAS for BCD correction after addition/subtraction.
- OF = Carry into MSB XOR Carry out of MSB. Key GATE formula for overflow detection.
Status Control Flags
Assess processor status and execution control flags.
Q1.Which control flag dictates the increment or decrement direction of string operations?
Related Articles
8086 Architecture
BIU and EU, 16-bit data bus.
9 min read
8086 Instruction Set
MOV, PUSH, POP, IN, OUT, arithmetic.
4 min read
8086 Addressing Modes
Based, Indexed, Based-Indexed, Relative.
9 min read
Memory Segmentation
CS, DS, SS, ES, physical address calculation.
4 min read
8085 Architecture Overview
8-bit ALU, registers, flags reduction.
4 min read