Slide 10.6: USES operator
Slide 11.2: Conditional jumps
Home

Conditional Processing


To implement any logic structure, use a combination of comparisons and jumps. Two steps are involved in executing a conditional statement:
  1. An operation such as CMP, AND, or SUB modifies the CPU flags.
  2. A conditional jump instruction tests the flags and causes a branch to a new address.
CMP Instruction
The CMP (compare) instruction performs an implied subtraction of a source operand from a destination operand. Neither operand is modified.

CMP — Compare

Usage: CMP dest,src

Flag O D I S Z A P C
Result *     * * * * *

Subtracts source from destination and updates the flags but does not save result. Flags can subsequently be checked for conditions.
Clocks
Operands 286 386 486 Size Bytes
reg, reg 2 2 1 2
mem, reg 7 5 2 2-4
reg, mem 6 6 2 2-4
reg, immed 3 2 1 3-4
mem, immed 6 5 2 3-6
accum, immed 3 2 1 2-3

When two operands are compared, the Zero and Carry flags indicate the relation between operands.
CMP Results ZF CF
destination < source 0 1
destination > source 0 0
destination = source 1 0

CMP Results Flags
destination < source SFOF
destination > source SF = OF
destination = source ZF = 1
If two operands being compared are assumed to be signed, the Sign, Zero, and Overflow flags indicate the relations between operands.