The flag register of 8086 is a 16-bit register that contains 16 flip-flops. So, it can store a maximum of 16-bit of data. Out of 16-bits, 9-bits are used as flags as shown in the below figure. These nine flags are divided into two parts as status flags and control flags.
Status Flags :
In the flag register, 6 out of 9 flags are used as status flags. When the microprocessor performs an arithmetic or logical operation in ALU, then depending upon the status of the result, the microprocessor will store corresponding status bits 0 or 1 in status flags. The status flags are,
- Carry flag (CF)
- Parity flag (PF)
- Auxiliary carry flag (AF)
- Zero flag (ZF)
- Sign flag (SF), and
- Overflow flag (OF).
The operation of each status flag is as follows,
Carry Flag (CF) :
When the microprocessor performs the addition of two 8 or 16-bit numbers the result obtained in ALU will be a maximum of 9 or 17 bit. The last carry generated is directly copied into carry flag. Similarly, when the microprocessor performs subtraction of (x – y) of two 8 or 16-bit numbers then,
- If x ≥ y, then the additional borrow required to perform subtraction is zero, so CF = 0.
- If x ≤ y, then additional borrow is required. So CF = 1 and the result of the subtraction (x – y) will be a negative number represented using 2’s complement method.
Parity Flag (PF) :
When the microprocessor performs an arithmetic or logical operation in ALU the parity status of only 8 LSBs (least significant bit) of 8 or 16-bit result is stored into parity flag.
- If the number of ones in 8 LSBs of result is even i.e., 0, 2, 4, 8, etc, then PF = 1.
- If the number of ones in 8 LSBs of result is odd i.e., 1, 3, 5, 7, etc then PF = 0.
Auxiliary Carry Flag (AF) :
When the microprocessor performs the addition of two 8 or 16-bit numbers, the carry generated after the addition of 4 LSBs (least significant bit) is copied into the AF flag. Similarly, when the microprocessor performs subtraction of two 8 or 16-bit numbers, the borrow required to perform subtraction of 4 LSBs is copied into the AF flag. This flag cannot be accessed by the programmer and it is used in representing a decimal number in binary code.
Zero Flag (ZF) :
When the microprocessor performs an arithmetic or logical operation in ALU and all the 8 LSBs or 16 LSBs of the result are zero, then ZF = 1. If 8 or 16-bit result in ALU is non-zero, then ZF = 0. This flag is also set whenever the result of a certain register becomes zero following an operation.
Sign Flag (SF) :
When the microprocessor performs an arithmetic or logical operation of an 8-bit number, the MSB of the result (D7 bit) is directly copied into the sign flag. Similarly, when the microprocessor performs an arithmetic or logical operation of a 16-bit number, MSBs of result (D15 bit) is directly copied into the sign flag. If the sign flag is set, the result will be negative otherwise the result will be positive.
Overflow Flag (OF) :
When the microprocessor performs an arithmetic operation of signed binary numbers and if the result is within the range then OF = 0. If the signed result is out of range, then OF = 1. The range of 8 bit signed numbers is +7FH to -80H, and the range of 16 bit signed numbers is +7FFFH to -8000H.
Example for CF, PF, AF, ZF, SF, OF: The below shows the status of all status flags after performing the addition and subtraction operation.
Control Flags :
Depending upon the value of the controlling flag bit, the microprocessor will control a particular operation i.e., these flags are used to control certain operations. There are three controlling flags namely,
- Interrupt flag (IF),
- Direction flag (DF), and
- Trap flag (TP).
Interrupt Flag (IF) :
Interrupt flag is used to enable or disable the hardware interrupt pin INTR. If the interrupt flag is made 1 by giving the instruction STI (Set Interrupt flag), then INTR is enabled. If the interrupt flag is made zero by giving the instruction CLI (Clear interrupt flag), then INTR interrupt is disabled.
Direction Flag (DF) :
String instructions are used for a block or chain of data, MOV SB/MOV SW instruction is used to transfer a block of bytes or words from source memory location to destination memory location.
To start the data transfer from 1st byte or word, initially, we have to store the effective address of source and destination of 1st byte or word location into SI and DI registers. After each byte or word transfer, the effective address in SI and DI should increase by 1 or 2. To increase SI and DI after each string operation we have to make DF = 0 using the instruction CLD (Clear Direction flag).
To start data transfer from the last byte or word, initially, we have to store the effective address of the last byte or word of source and destination location into register SI and DI. After each byte or word transfer, the effective address in SI and DI should decrease by one or two. To decrement SI and DI after string instruction we have to make DF = 1 using instruction STD (Set direction flag).
Trap Flag :
If the trap flag is made zero, then the microprocessor will execute the complete program in one operation and it is called a free-running operation. If the trap flag is made one, then the microprocessor will execute the program in single-stepping mode. Single stepping mode is used to detect the errors in the program i.e., software debugging.