Flag register in Intel 8086

The flag register of the Intel 8086 microprocessor is a 16-bit register containing status flags and control flags. These flags provide information about the results of arithmetic and logical operations and also control the operation of the processor. Below is a detailed description of the flag register in the 8086:

Structure of the flag register

The 16-bit flag register is divided into two parts: the status flags (9 bits), which indicate the result of arithmetic or logical operations, and the control flags (3 bits), which control specific processor operations.

The structure of the flag register is as follows:

Bit position Flag name Description
0 CF Carry flag
1 Reserved Reserved (always 1)
2 PF Parity flag
3 Reserved Reserved (always 0)
4 AF Auxiliary carry flag
5 Reserved Reserved (always 0)
6 ZF Zero flag
7 SF Sign flag
8 TF Trap flag
9 IF Interrupt enable flag
10 DF Direction flag
11 OF Overflow flag
12-15 Reserved Reserved (always 0)

Status flags

The status flags are updated after arithmetic or logical operations to reflect the result.

Carry flag (CF)


Bit position: 0

Purpose: indicates a carry or borrow in arithmetic operations.

Set when: on addition a carry occurs; on subtraction a borrow occurs. Example: when adding AX and BX, the CF flag is set if the result exceeds 16 bits.

Parity flag (PF)


Bit position: 2

Purpose: indicates the parity of the result (an even or odd number of ones).

Set when: the result contains an even number of ones. Example: MOV AL, 03h sets PF because 03h (00000011₂) contains two ones (even parity).

Auxiliary carry flag (AF)


Bit position: 4

Purpose: indicates a carry or borrow in the low-order nibble (4 bits) of the operation.

Set when: on addition a carry propagates from bit 3 to bit 4; on subtraction a borrow propagates from bit 4 to bit 3. Example: used in BCD (binary-coded decimal) arithmetic.

Zero flag (ZF)


Bit position: 6

Purpose: indicates whether the result of the operation is zero.

Set when: the result is zero. Example: SUB AX, AX sets ZF because the result is zero.

Sign flag (SF)


Bit position: 7

Purpose: indicates the sign of the result (positive or negative).

Set when: the result is negative (the most significant bit is 1). Example: MOV AX, -1 sets SF because the result is negative.

Overflow flag (OF)


Bit position: 11

Purpose: indicates arithmetic overflow (the result is too large to fit in the destination register).

Set when: the result goes outside the range of the number. Example: ADD AL, 127 when AL is already equal to 127 (the result exceeds 127).

Control flags

The control flags control specific processor operations.

Trap flag (TF)


Bit position: 8

Purpose: enables single-step debugging.

Set when: TF = 1: the processor generates an interrupt after each instruction; TF = 0: normal execution. Example: used by debuggers for single-step code execution.

Interrupt enable flag (IF)


Bit position: 9

Purpose: enables or disables hardware interrupts.

Set when: IF = 1: interrupts are enabled; IF = 0: interrupts are disabled. Example: STI sets IF, enabling interrupts; CLI clears IF, disabling interrupts.

Direction flag (DF)


Bit position: 10

Purpose: controls the direction of string operations.

Set when: DF = 1: string operations proceed from high memory to low memory (decrementing); DF = 0: string operations proceed from low memory to high memory (incrementing). Example: STD sets DF; CLD clears DF.

Reserved bits

Bits 1, 3, 5 and 12–15 are reserved and must not be changed. Bit 1 is always 1, and bits 3, 5 and 12–15 are always 0.

Example of flag usage

Consider the following assembler code:

mov     ax, 0FFFFh  ; load AX with -1
add     ax, 1       ; add 1 to AX

Result: ax = 0000h

Flags:

CF: set (carry out of the most significant bit).

ZF: set (the result is zero).

SF: cleared (the result is positive).

OF: cleared (no signed overflow).

PF: set (an even number of ones in the result).

Summary of the flags

Flag Name Purpose
CF Carry flag Indicates a carry/borrow in arithmetic.
PF Parity flag Indicates whether the result has even or odd parity.
AF Auxiliary carry flag Indicates a carry/borrow in the low-order nibble.
ZF Zero flag Indicates that the result is zero.
SF Sign flag Indicates the sign of the result.
TF Trap flag Enables single-step debugging.
IF Interrupt enable Enables/disables hardware interrupts.
DF Direction flag Controls the direction of string operations.
OF Overflow flag Indicates signed arithmetic overflow.

Importance of the flag register

Status flags: provide feedback on the results of operations, allowing conditional jumps to be performed (for example, JZ, JC).

Control flags: control the behaviour of the processor, for example the handling of interrupts and strings.

The flag register is the most important component of the 8086 microprocessor, providing efficient program control and decision making.