Chapter 6 · Simulation

Flags Lab

← Chapter 6

Flags Lab

Goal: see, at the bit level, why the six status flags (CF, PF, AF, ZF, SF, OF) become 0 or 1 after the 8086's arithmetic instructions.

How to use it

  1. Choose the instruction and the size (8 bit: AL, 16 bit: AX); type the operands in hexadecimal (0C8h), binary (11001000b) or decimal (200, -56) and press Apply.
  2. CF (before the instruction): ADC and SBB use this value as the carry/borrow input; INC and DEC do not change CF, so it stays as it is.
  3. Step ▶ (or →) computes the columns one at a time from right to left; the top row is the carry (or borrow) bit going into each bit. Then each flag is revealed with its reason.
  4. Try it yourself: after the bits are computed, predict the six flags; correct and wrong answers are counted.

Rules

  • CF: carry out of the most significant bit (addition) or borrow (subtraction). In the unsigned interpretation it means the result did not fit the range.
  • OF: the carry into the most significant bit differs from the carry out. In the signed interpretation it means the result did not fit the range (8 bit: −128…127, 16 bit: −32768…32767).
  • AF: carry or borrow from bit 3 into bit 4. SF: the most significant bit of the result. ZF: the result is zero. PF: the low byte of the result has an even number of 1s.
  • SUB, SBB, CMP, DEC and NEG are subtractions: destination − source (0 − operand for NEG). CMP does not store the result, it only sets the flags.
  • INC and DEC do not touch CF. For NEG, CF is 1 if the operand is not zero.

All results shown are computed with the 8086 flag rules and were checked against an 8086 interpreter.

Related slides: Chapter 6 · Arithmetic, Logic and Shift Instructions (The Flags at a Glance, CF or OF?, Flags After ADD Step by Step, INC, DEC, NOT, NEG).