Overview

A number of instructions operate with respect to the condition code registers.

The conventional way to implement conditional operationsis through a conditional transfer of control, where the program follows one execution path when a condition holds and another when it does not.

An alternate strategy is through a conditional transfer of data. This approach computes both outcomes of a conditional operation and then selects one based on whether or not the condition holds.

Instructions

CMP and TEST

InstructionOperandsBased OnDescription
cmp[bwlq]Compare
test[bwlq]Test

SET

The description of the SET commands apply to the case of a comparison instruction. That is, the condition codes are set according to computation t = a - b, where t, a, and b may be interpreted as signed or unsigned depending on the SET instruction invoked.

InstructionSynonymEvaluationDescription
setesetzZFEqual / zero
setnesetnz~ZFNot equal / not zero
setsSFNegative
setns~SFNonnegative
setlsetngeSF ^ OFLess (signed <)
setlesetng(SF ^ OF) | ZFLess or equal (signed <=)
setgsetnle~(SF ^ OF) & ~ZFGreater (signed >)
setgesetnl~(SF ^ OF)Greater or equal (signed >=)
setbsetnaeCFBelow (unsigned <)
setbesetnaCF | ZFBelow or equal (unsigned <=)
setasetnbe~CF & ~ZFAbove (unsigned >)
setaesetnb~CFAbove or equal (unsigned >=)

Note how the other condition code evaluations are easy to derive from setl and setb.

JMP

Jump instructions are categorized as either direct or indirect. Direct jump instructions specify a label whereas indirect jump instructions specify a * followed by an memory operand.

InstructionSynonymJump ConditionDescription
jmp Label-1Direct jump
jmp *Operand-1Indirect jump
je LabeljzZFEqual / zero
jne Labeljnz~ZFNot equal / not zero
js Label-SFNegative
jns Label-~SFNonnegative
jl LabeljngeSF ^ OFLess (signed <)
jle Labeljng(SF ^ OF) | ZFLess or equal (signed <=)
jg Labeljnle~(SF ^ OF) & ~ZFGreater (signed >)
jge Labeljnl~(SF ^ OF)Greater or equal (signed >=)
jb LabeljnaeCFBelow (unsigned <)
jbe LabeljnaCF | ZFBelow or equal (unsigned <=)
ja Labeljnbe~CF & ~ZFAbove (unsigned >)
jae Labeljnb~CFAbove or equal (unsigned >=)

CMOV

Like MOV instructions, but with the data transfer only happening if the move condition is satisfied.

InstructionSynonymMove ConditionDescription
cmovecmovzZFEqual / zero
cmovenecmovnz~ZFNot equal / not zero
cmovs-SFNegative
cmovns-~SFNonnegative
cmovlcmovngeSF ^ OFLess (signed <)
cmovlecmovng(SF ^ OF) | ZFLess or equal (signed <=)
cmovgcmovenle~(SF ^ OF) & ~ZFGreater (signed >)
cmovgecmovnl~(SF ^ OF)Greater or equal (signed >=)
cmovbcmovnaeCFBelow (unsigned <)
cmovbecmovnaCF | ZFBelow or equal (unsigned <=)
cmovacmovnbe~CF & ~ZFAbove (unsigned >)
cmovaecmovnb~CFAbove or equal (Unsigned >=)

Bibliography

  • Bryant, Randal E., and David O’Hallaron. Computer Systems: A Programmer’s Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.