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 = 1
  • 00000111₂ → āϤāĻŋāύāϟāĻŋ 1 → PF = 0
  • 00000000₂ → āĻļā§‚āĻ¨ā§āϝāϟāĻŋ 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:

  1. 5 − 5 → ZF = ?
  2. FFH + 01H → CF = ?
  3. 7FH + 01H → OF = ?
  4. 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 — āϛ⧋āϟ āωāĻ¤ā§āϤāϰ āĻĻāĻžāĻ“:

  1. Which flag enables single-step debugging?
  2. Does IF = 0 disable NMI?
  3. 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?