Rather than reading the condition codes directly, the two most common methods of accessing them are to set an integer register or to perform a conditional branch based on some combination of condition codes.
The different setinstructions described in Figure 3.9 set a single byte to 0 or to 1 depending on some combination of the conditions codes. The destination operand is either one of the eight single-byte register
Instruction Synonym Effect Set Condition
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 ˜ CF & ˜ZF Above (Unsigned>)
setae D setnb D ˜ CF Above or Equal (Unsigned>=) setb D setnae D CF Below (Unsigned<)
setbe D setna D CF & ˜ZF Below or Equal (Unsigned<=)
Figure 3.9: ThesetInstructions. Each instruction sets a single byte to 0 or 1 based on some combination of the condition codes. Some instructions have “synonyms,” i.e., alternate names for the same machine instruction.
elements (Figure 3.2) or a memory location where the single byte is to be stored. To generate a 32-bit result, we must also clear the high-order 24 bits. A typical instruction sequence for a C predicate such asa<bis therefore as follows
Note: a is in %edx, b is in %eax 1 cmpl %eax,%edx Compare a:b
2 setl %al Set low order byte of %eax to 0 or 1 3 movzbl %al,%eax Set remaining bytes of %eax to 0
using themovzblinstruction to clear the high-order three bytes.
For some of the underlying machine instructions, there are multiple possible names, which we list as “syn- onyms.” For example both “setg” (for “SET-Greater”) and “setnle” (for “SET-Not-Less-or-Equal”) refer to the same machine instruction. Compilers and disassemblers make arbitrary choices of which names to use.
Although all arithmetic operations set the condition codes, the descriptions of the differentsetcommands apply to the case where a comparison instruction has been executed, setting the condition codes according to the computationt=a-b. For example, consider thesete, or “Set when equal” instruction. Whena=b, we will havet=0, and hence the zero flag indicates equality.
Similarly, consider testing a signed comparison with thesetl, or “Set when less,” instruction. Whena and bare in two’s complement form, then fora < bwe will have a b < 0if the true difference were computed. When there is no overflow, this would be indicated by having the sign flag set. When there is positive overflow, because a bis a large positive number, however, we will have t < 0. When there is negative overflow, because a bis a small negative number, we will have t > 0. In either case, the sign flag will indicate the opposite of the sign of the true difference. Hence, the EXCLUSIVE-OR of the overflow and sign bits provides a test for whether a <b. The other signed comparison tests are based on
other combinations ofSF ˆ OFandZF.
For the testing of unsigned comparisons, the carry flag will be set by thecmplinstruction when the integer differencea bof the unsigned argumentsaandbwould be negative, that is, when(unsigned) a <
(unsigned) b. Thus, these tests use combinations of the carry and zero flags.
Practice Problem 3.7:
In the following C code, we have replaced some of the comparison operators with “__” and omitted the data types in the casts.
1 char ctest(int a, int b, int c)
2 {
3 char t1 = a __ b;
4 char t2 = b __ ( ) a;
5 char t3 = ( ) c __ ( ) a;
6 char t4 = ( ) a __ ( ) c;
7 char t5 = c __ b;
8 char t6 = a __ 0;
9 return t1 + t2 + t3 + t4 + t5 + t6;
10 }
For the original C code,GCCgenerates the following assembly code
1 movl 8(%ebp),%ecx Get a 2 movl 12(%ebp),%esi Get b 3 cmpl %esi,%ecx Compare a:b
4 setl %al Compute t1
5 cmpl %ecx,%esi Compare b:a 6 setb -1(%ebp) Compute t2 7 cmpw %cx,16(%ebp) Compare c:a 8 setge -2(%ebp) Compute t3 9 movb %cl,%dl
10 cmpb 16(%ebp),%dl Compare a:c
11 setne %bl Compute t4
12 cmpl %esi,16(%ebp) Compare c:b 13 setg -3(%ebp) Compute t5 14 testl %ecx,%ecx Test a
15 setg %dl Compute t4
16 addb -1(%ebp),%al Add t2 to t1 17 addb -2(%ebp),%al Add t3 to t1 18 addb %bl,%al Add t4 to t1 19 addb -3(%ebp),%al Add t5 to t1 20 addb %dl,%al Add t6 to t1
21 movsbl %al,%eax Convert sum from char to int
Based on this assembly code, fill in the missing parts (the comparisons and the casts) in the C code.
Instruction Synonym Jump Condition Description
jmp Label 1 Direct Jump
jmp *Operand 1 Indirect Jump
je Label jz ZF Equal / Zero
jne Label jnz ˜ZF Not Equal / Not Zero
js Label SF Negative
jns Label ˜SF Nonnegative
jg Label jnle ˜(SF ˆ OF)& ˜ZF Greater (Signed>)
jge Label jnl ˜(SF ˆ OF) Greater or Equal (Signed>=)
jl Label jnge SF ˆ OF Less (Signed<)
jle Label jng (SF ˆ OF)| ZF Less or Equal (Signed<=) ja Label jnbe ˜CF & ˜ZF Above (Unsigned>)
jae Label jnb ˜CF Above or Equal (Unsigned>=)
jb Label jnae CF Below (Unsigned<)
jbe Label jna CF & ˜ZF Below or Equal (Unsigned<=)
Figure 3.10: The jump Instructions. These instructions jump to a labeled destination when the jump condition holds. Some instructions have “synonyms,” alternate names for the same machine instruction.