Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 67 ยท Instruction Set Architecture

Tracing Register and Flag Contents After Instructions

Learn to work out register values and the C, Z, S, V and P flags after ADD, SUB, CMP, INC, AND and shifts, with worked traces and a Python flag calculator.

Exam questions and debugging sessions often ask the same thing: "After this instruction, what is in the register, and which flags are set?" The answer decides which branch will be taken, so being able to trace it by hand is a core skill. It is mechanical once you know the rule for each flag and what each instruction does to it.

This lesson fixes the meaning of five common flags, works examples for ADD, SUB, CMP, INC, AND and shifts, and gives you a Python calculator to check your own traces. We use 8-bit values so that every pattern fits on one line. It builds on Binary Arithmetic and Overflow and Machine Instruction Families.

The five flags

The flags register (or status register) holds single-bit results of the last arithmetic or logic instruction.

Flag Name Set to 1 when...
C Carry An addition produced a carry out of the top bit, or a subtraction needed a borrow (unsigned overflow)
Z Zero The result is all zeros
S Sign (negative) The top bit of the result is 1
V Overflow The result of a signed operation does not fit (two's complement overflow)
P Parity The low 8 bits of the result contain an even number of 1s

Memorize two tests for signed overflow in addition: V is 1 if both operands have the same sign and the result has a different sign. Equivalently, V is the XOR of the carry into the top bit and the carry out of it.

For subtraction a - b, the hardware computes a + (~b) + 1. Carry means "no borrow" on many processors (ARM, 6502), whereas on x86 the carry flag after SUB is set when a borrow occurred. Check your processor's manual; in this lesson we use the x86 convention: C = 1 means a < b for unsigned operands.

Worked example 1: ADD

MOV AL, 0xF0 then ADD AL, 0x20.

Text
  1111 0000   (0xF0 = 240 unsigned, -16 signed)
+ 0010 0000   (0x20 =  32)
-----------
1 0001 0000   -> 8-bit result 0001 0000 = 0x10, carry out = 1
  • Result AL = 0x10.
  • C = 1 (the 9th bit exists: 240 + 32 = 272 does not fit in 8 bits as unsigned).
  • Z = 0 (the result is not zero).
  • S = 0 (top bit of 0x10 is 0).
  • V = 0: the operands have different signs (-16 and +32), so signed overflow is impossible. The signed result -16 + 32 = 16 is right.
  • P = 0: 0x10 has a single 1, which is odd.

Notice that C and V disagree. That is normal: C describes unsigned correctness, V describes signed correctness, and the same bits are treated both ways.

Worked example 2: signed overflow

MOV AL, 0x7F then ADD AL, 0x01.

Text
  0111 1111   (+127)
+ 0000 0001   (+1)
-----------
  1000 0000   (0x80 = -128 signed, 128 unsigned)
  • AL = 0x80, C = 0 (no carry out; 128 fits unsigned), Z = 0, S = 1.
  • V = 1: two positive numbers gave a negative result.
  • P = 0: one 1 bit, odd.

Worked example 3: SUB and CMP

MOV AL, 5 then SUB AL, 9.

Text
  0000 0101   (5)
- 0000 1001   (9)
-----------
  1111 1100   (0xFC = 252 unsigned, -4 signed)
  • AL = 0xFC; C = 1 because 5 < 9 (a borrow was needed).
  • S = 1, Z = 0.
  • V = 0: 5 - 9 = -4 fits in a signed byte.
  • P: 0xFC = 1111 1100 has six 1s, which is even, so P = 1.

CMP AL, 9 performs the same subtraction and sets the same flags but does not change AL. That is why conditional jumps follow it:

Jump Condition on flags Meaning after CMP a, b
JE / JZ Z = 1 a == b
JB / JC C = 1 a < b (unsigned)
JA C = 0 and Z = 0 a > b (unsigned)
JL S != V a < b (signed)
JG Z = 0 and S == V a > b (signed)

What does not change the flags

  • MOV, PUSH, POP, LEA and JMP leave the flags alone.
  • INC and DEC update Z, S, V and P but leave C unchanged, so they can be used for loop counters inside a multi-word addition.
  • AND, OR, XOR and TEST set Z, S and P from the result and clear C and V.
  • NOT changes no flags.
  • Shifts put the last bit shifted out into C. SHL AL, 1 moves the old top bit into C; SHR moves the old bottom bit into C.

A flag calculator

Python Compute result and flags for 8-bit operations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Verify the first four lines against the hand calculations above.

Tracing a short program

Trace this sequence, tracking AL and the flags after each line. The simulator prints the same table you would draw on paper.

Python Trace a program step by step

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

MOV changes no flags (the first line shows the all-zero starting flags). ADD gives the signed overflow of example 2. SUB AL, 9 borrows, so C=1 and S=1 (the value is -4), and INC then leaves C at 1 and only changes the other flags, which is exactly the point of "INC does not change C". AND clears C and V, and SHL shifts the old top bit into C.

Common mistakes

  • Confusing C and V. C is for unsigned numbers, V for signed ones. A single add can set either, both, or neither.
  • Forgetting that CMP does not store the result. Only the flags change.
  • Using signed jumps on unsigned data (or the reverse). Compare 0xF0 with 0x10: unsigned 240 > 16, but signed -16 < 16, so JA and JG disagree.
  • Using INC to detect carry. It does not touch C.
  • Parity covers only the low byte, even for 16- or 32-bit results.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. After 8-bit ADD 0xFF + 0x01, what are the result and the C and Z flags?

    255 + 1 = 256 wraps to 0x00 with a carry out, so C=1, and the zero result sets Z=1.
  2. 2. After 8-bit ADD 0x50 + 0x50, which flags are set?

    0x50 + 0x50 = 0xA0 = 160. Two positive numbers gave a negative result: V=1 and S=1. There is no carry out (160 < 256), so C=0.
  3. 3. Which instruction leaves the carry flag unchanged?

    INC and DEC update Z, S, V and P but not C. ADD and SUB set it, and AND clears it.
  4. 4. After CMP 5, 9 (x86), which jump is taken?

    5 - 9 borrows, so C=1: JB is taken. 5 is not greater than 9, so JA and JG are not taken.
  5. 5. What does the parity flag P describe?

    P=1 means an even count of 1 bits in the low byte of the result. It is unrelated to the numeric parity of the value.

Summary

  • C is the unsigned carry/borrow, Z means zero, S copies the top bit, V is signed overflow, and P is even parity of the low byte.
  • V = 1 in an addition when both operands share a sign and the result has the other sign.
  • CMP and TEST set flags without storing a result; conditional jumps read those flags.
  • INC and DEC keep C; AND, OR, XOR clear C and V; shifts move the bit shifted out into C; MOV changes nothing.
  • Use unsigned jumps (JA, JB) for unsigned data and signed jumps (JG, JL) for signed data.

Next: Assembly Language Basics.