07 Flags
āĻāĻŦāĻžāϰ 8086 Flag RegisterâāĻāĻāĻŋ EU-āĻāϰ āĻ āĻāĻļāĨ¤
Flag āĻā§?
āĻā§āύ⧠operation-āĻāϰ āĻĒāϰ⧠āĻĢāϞāĻžāĻĢāϞ āĻļā§āύā§āϝ āĻšā§ā§āĻā§ āĻāĻŋ āύāĻž, carry āĻšā§ā§āĻā§ āĻāĻŋ āύāĻžâāĻ āϧāϰāύā§āϰ āϤāĻĨā§āϝ āĻāĻāĻāĻŋ bit āĻĻāĻŋā§ā§ āĻāĻžāύāĻžāύ⧠āĻšā§āĨ¤ āĻāĻ bit-āĻā§ flag āĻŦāϞā§āĨ¤
8086-āĻāϰ FLAGS register 16-bit, āϤāĻŦ⧠⧝āĻāĻŋ flag āĻŦā§āϝāĻŦāĻšā§āϤ āĻšā§:
- ā§ŦāĻāĻŋ Status/Conditional flags: CF, PF, AF, ZF, SF, OF
- ā§ŠāĻāĻŋ Control flags: TF, IF, DF
āĻāĻā§ Status flags āĻŦā§āĻāĻŋ; Control flags āĻĒāϰā§āϰ āϧāĻžāĻĒā§āĨ¤
ā§§. ZF â Zero Flag
āĻĢāϞāĻžāĻĢāϞ āĻļā§āύā§āϝ āĻšāϞ⧠ZF = 1, āĻ āύā§āϝāĻĨāĻžā§ 0āĨ¤
āĻāĻĻāĻžāĻšāϰāĻŖ: 5 â 5 = 0 â ZF = 1
Exam: âZF is set when the result of an operation is zero.â
⧍. SF â Sign Flag
SF āĻĢāϞāĻžāĻĢāϞā§āϰ most significant bit (MSB) āĻāĻĒāĻŋ āĻāϰā§āĨ¤
8-bit āĻĢāϞāĻžāĻĢāϞ⧠bit 7 āĻšāϞ⧠MSBāĨ¤ Signed number āĻšāĻŋāϏā§āĻŦā§ āĻŦā§āϝāĻžāĻā§āϝāĻž āĻāϰāϞ⧠MSB = 1 āĻŽāĻžāύ⧠negativeāĨ¤
āĻāĻĻāĻžāĻšāϰāĻŖ: 10000000â â SF = 1
Exam: âSF copies the most significant bit of the result and indicates its sign when interpreted as a signed number.â
ā§Š. CF â Carry Flag
āϝā§āĻā§ āϏāϰā§āĻŦā§āĻā§āĻ bit-āĻāϰ āĻŦāĻžāĻāϰ⧠carry āĻā§āϞ⧠CF = 1āĨ¤ āĻŦāĻŋā§ā§āĻā§ borrow āĻĒā§āϰā§ā§āĻāύ āĻšāϞā§āĻ CF = 1āĨ¤
8-bit āĻāĻĻāĻžāĻšāϰāĻŖ:
FFH + 01H = 100H
āĻāĻŋāύā§āϤ⧠8-bit āĻāĻžā§āĻāĻžā§ āĻļā§āϧ⧠00H āĻĨāĻžāĻā§; āĻŦāĻžāĻāϰā§āϰ carry āĻŦā§āĻāĻžā§ CF = 1āĨ¤
Exam: âCF is set when an addition produces a carry out of the most significant bit or a subtraction requires a borrow.â
ā§Ē. OF â Overflow Flag
Signed arithmetic-āĻāϰ āĻĒā§āϰāĻā§āϤ āĻĢāϞ āύāĻŋāϰā§āĻĻāĻŋāώā§āĻ bit-āĻāϰ signed range-āĻāϰ āĻŦāĻžāĻāϰ⧠āĻā§āϞ⧠OF = 1āĨ¤
8-bit signed range: â128 to +127āĨ¤
āĻāĻĻāĻžāĻšāϰāĻŖ: 127 + 1-āĻāϰ āĻĒā§āϰāĻā§āϤ āĻĢāϞ +128, āϝāĻž āĻāĻ range-āĻ āϧāϰ⧠āύāĻžāĨ¤ Stored result āĻšā§ 80H, signed āĻšāĻŋāϏā§āĻŦā§ â128āĨ¤ āϤāĻžāĻ OF = 1āĨ¤
CF āĻŦāύāĻžāĻŽ OFâāĻā§āϰā§āϤā§āĻŦāĻĒā§āϰā§āĻŖ āĻĒāĻžāϰā§āĻĨāĻā§āϝ:
| 8-bit addition | Stored result | CF | OF |
|---|---|---|---|
FFH + 01H |
00H |
1 | 0 |
7FH + 01H |
80H |
0 | 1 |
āĻĒā§āϰāĻĨāĻŽāĻāĻŋāĻā§ signed āĻšāĻŋāϏā§āĻŦā§ āĻĻā§āĻāϞ⧠â1 + 1 = 0, āϤāĻžāĻ signed overflow āύā§āĻāĨ¤
Exam: âOF is set when the signed result is outside the representable range.â
ā§Ģ. PF â Parity Flag
āĻĢāϞāĻžāĻĢāϞā§āϰ lowest 8 bits-āĻ 1-āĻāϰ āϏāĻāĻā§āϝāĻž āĻā§ā§ āĻšāϞ⧠PF = 1, āĻŦāĻŋāĻā§ā§ āĻšāϞ⧠0āĨ¤
00000011ââ āĻĻā§āĻāĻāĻŋ 1 â PF = 100000111ââ āϤāĻŋāύāĻāĻŋ 1 â PF = 000000000ââ āĻļā§āύā§āϝāĻāĻŋ 1; āĻļā§āύā§āϝāĻ āĻā§ā§ â PF = 1
Exam: âPF is set when the least significant byte of the result contains an even number of 1 bits.â
ā§Ŧ. AF â Auxiliary Carry Flag
āϝā§āĻā§ bit 3 āĻĨā§āĻā§ bit 4-āĻ carry āĻā§āϞ⧠AF = 1āĨ¤ āĻŦāĻŋā§ā§āĻā§ āĻāĻ boundary-āϤ⧠borrow āĻšāϞā§āĻ set āĻšā§āĨ¤ āĻāĻāĻŋ BCD arithmetic-āĻ āĻāĻžāĻā§ āϞāĻžāĻā§āĨ¤
āĻāĻĻāĻžāĻšāϰāĻŖ: 0FH + 01H = 10H â AF = 1
Exam: âAF is set when a carry or borrow occurs between bits 3 and 4.â
Quick check â āĻļā§āϧ⧠flag value āĻĻāĻžāĻāĨ¤ Assume 8-bit operations:
5 â 5â ZF = ?FFH + 01Hâ CF = ?7FH + 01Hâ OF = ?- Result =
00000111ââ PF = ?
āĻāĻŦāĻžāϰ āĻŦāĻžāĻāĻŋ ā§ŠāĻāĻŋ Control Flags: TF, IF āĻ DFāĨ¤ āĻāĻā§āϞ⧠processor-āĻāϰ āĻāĻŋāĻā§ āĻāĻāϰāĻŖ āύāĻŋā§āύā§āϤā§āϰāĻŖ āĻāϰā§āĨ¤
ā§§. TF â Trap Flag
Program debug āĻāϰāĻžāϰ āϏāĻŽā§ āĻāĻāĻāĻŋ instruction āĻāϰ⧠execution āĻĒāϰā§āϝāĻŦā§āĻā§āώāĻŖ āĻāϰāϤ⧠āĻŦā§āϝāĻŦāĻšā§āϤ āĻšā§āĨ¤
- TF = 1: Single-step mode āĻāĻžāϞā§; āĻĒā§āϰāϤāĻŋāĻāĻŋ instruction-āĻāϰ āĻĒāϰ⧠single-step interrupt āĻšā§āĨ¤
- TF = 0: Single-step mode āĻŦāύā§āϧāĨ¤
Exam:
âThe Trap Flag enables single-step execution for debugging. When TF is set, a single-step interrupt is generated after each instruction.â
⧍. IF â Interrupt Enable Flag
Interrupt āĻšāϞ⧠CPU-āĻā§ āĻā§āύ⧠āĻāĻāύāĻžāϰ āĻāύā§āϝ attention āĻĻā§āĻā§āĻžāϰ requestāĨ¤ IF āĻĻāĻŋā§ā§ INTR pin-āĻāϰ maskable hardware interrupt āĻā§āϰāĻšāĻŖ āύāĻŋā§āύā§āϤā§āϰāĻŖ āĻāϰāĻž āĻšā§āĨ¤
| IF | Meaning |
|---|---|
| 1 | Maskable hardware interrupts enabled |
| 0 | Maskable hardware interrupts disabled |
āĻā§āϰā§āϤā§āĻŦāĻĒā§āϰā§āĻŖ: IF = 0 āĻŽāĻžāύ⧠āϏāĻŦ interrupt āĻŦāύā§āϧ āύā§āĨ¤ NMI â Non-Maskable Interrupt IF āĻĻāĻŋā§ā§ āĻŦāύā§āϧ āĻāϰāĻž āϝāĻžā§ āύāĻžāĨ¤
Exam:
âThe Interrupt Enable Flag controls maskable hardware interrupts. IF = 1 enables them, while IF = 0 disables them. It does not affect NMI.â
ā§Š. DF â Direction Flag
String instructions āĻ āύā§āĻāĻā§āϞ⧠byte āĻŦāĻž word-āĻāϰ āĻāĻĒāϰ āϧāĻžāϰāĻžāĻŦāĻžāĻšāĻŋāĻāĻāĻžāĻŦā§ āĻāĻžāĻ āĻāϰā§āĨ¤ DF āύāĻŋāϰā§āϧāĻžāϰāĻŖ āĻāϰ⧠āĻŦā§āϝāĻŦāĻšā§āϤ SI āĻ/āĻŦāĻž DI āϏāĻžāĻŽāύ⧠āϝāĻžāĻŦā§, āύāĻžāĻāĻŋ āĻĒā§āĻāύ⧠āϝāĻžāĻŦā§āĨ¤
| DF | Direction | Index update |
|---|---|---|
| 0 | Toward higher addresses | Increment |
| 1 | Toward lower addresses | Decrement |
Byte operation: index āĻŦāĻĻāϞāĻžā§ ā§§ āĻāϰā§āĨ¤
Word operation: index āĻŦāĻĻāϞāĻžā§ ā§¨ āĻāϰā§āĨ¤
āĻāĻĻāĻžāĻšāϰāĻŖ: SI = 1000H āĻšāϞ⧠āĻāĻāĻāĻŋ byte string operation-āĻāϰ āĻĒāϰā§:
- DF = 0 â SI = 1001H
- DF = 1 â SI = 0FFFH
DF āϏāĻŦ instruction-āĻ SI/DI āĻŦāĻĻāϞāĻžā§ āύāĻžâāĻāĻāĻŋ āĻĒā§āϰāϝā§āĻā§āϝ string operations-āĻāĨ¤
Exam:
âThe Direction Flag controls the direction of string operations. When DF = 0, the applicable index registers are incremented; when DF = 1, they are decremented. The change is 1 for byte operations and 2 for word operations.â
āĻŽāύ⧠āϰāĻžāĻā§: TF â single-step, IF â interrupts, DF â directionāĨ¤
Quick check â āĻā§āĻ āĻāϤā§āϤāϰ āĻĻāĻžāĻ:
- Which flag enables single-step debugging?
- Does IF = 0 disable NMI?
- If DF = 0 and DI = 2000H, what is DI after a word string operation that updates DI?
Hex to Decimal and Binary
In the binary system, each digit position represents a power of 2, starting from the right side (position 0) and moving left.
To convert the binary number 1111 1111 to the decimal value 255, you simply add up the value of each position where there is a 1:
Binary Digit Position Calculation Value
1 (Far left) 7 2⡠(2 à 2 à 2 à 2 à 2 à 2 à 2) 128
1 6 2âļ 64
1 5 2âĩ 32
1 4 2â´ 16
1 3 2Âŗ 8
1 2 2² 4
1 1 2š 2
1 (Far right) 0 2â° (Any number to the power of 0 is 1) 1
The Final Sum:
\(128+64+32+16+8+4+2+1=\mathbf{255}\)
An easy shortcut to remember for an 8-bit binary number is that the next position over (the 9th digit) would equal 256. Because all eight bits before it are completely filled with 1s, their total sum will always be exactly one less than the next power of two (256 - 1 = 255).
Would you like to see how to convert a different decimal number into binary, or explore how negative numbers are handled in binary?