Bryand and O'Hallaron Chapter 3 Section 3.6.2 Condition Code Instructions 1. Compare and Test Instructions Instruction Effect Description cmpb s2,s1 s1-s2 Compare bytes testb s2,s1 s1&s2 Test byte cmpw s2,s1 s1-s2 Compare words testw s2,s1 s1&s2 Test word cmpl s2,s1 s1-s2 Compare double words testl s2,s1 s1&s2 Test double words 2. The test instructions set the zero and negative flags based on the bitwise and of the two operations. The instruction testl %eax,%eax will set the zero and negative bits based on the value in %eax. 3. Figure 3.10 The Set Instructions (byte destination only) Instruction Synonym Effect Description sete D setz D <- ZF Equal/Zero setne D setnz D <- ~ZF Not equal/Not zero sets D D <- SF Negative setns D D <- ~SF Nonnegative setg D setnle D <- ~(SF^OF)&~ZF Greater (signed >) setge D setnl D <- ~(SF^OF) Greater or equal (signed >=) setl D setnge D <- (SF^OF) Less (signed <) setle D setng D <- (SF^OF)&~ZF Less or equal (signed <=) seta D setnbe D <- ~(SF^OF)&~ZF Greater (signed >) setae D setnb D <- ~(SF^OF) Greater or equal (signed >=) setb D setnae D <- (SF^OF) Less (signed <) setbe D setna D <- (SF^OF)&~ZF Less or equal (signed <=) 4. Typical instruction sequence for a C predicate such as (a < b) #assumes a is in %edx, b is in %eax cmpl %eax,%edx # compute %edx - %eax (thats a - b) setl %al # low order byte = SF^0F (1 if a < b signed) movzbl %al,%eax # upper 24 bits of %eax set to zero When a "cmpl b,a" instruction is followed by "setl", we need to read the combination as "cmpl b>a". The "less" refers to the second operand of the cmpl. Very confusing (and the result of the assembler using AT&T syntax rather than Intel syntax). 5. Practice Problem 3.7 on page 151