The most useful condition codes are:
CF
: Carry Flag. The most recent operation generated a carry out of the most significant bit. Used to detect
overflow for unsigned operations.
ZF
: Zero Flag. The most recent operation yielded zero.
SF
: Sign Flag. The most recent operation yielded a negative value.
OF
: Overflow Flag. The most recent operation caused a two’s complement overflow—either negative or
positive.
1. The leal instruction does not alter any condition codes.
2. Otherwise, all of the instructions listed in Figure 3.6 cause the condition codes to be set.
3. For the logical operations, such as xorl, the carry and overflow flags are set to 0.
4. For the shift operations, the carry flag is set to the last bit shifted out, while the overflow flag is set to 0.
The cmpb, cmpw, and cmpl instructions set the condition codes according to the difference of their two
operands. With GAS format, the operands are listed in reverse order, making the code difficult to read. These
instructions set the zero flag if the two operands are equal. The other flags can be used to determine ordering
relations between the two operands.
The testb, testw, and testl instructions set the zero
and negative
flags based on the AND
of their
two operands. Typically, the same operand is repeated (e.g., testl %eax,%eax to see whether %eax is
negative, zero, or positive), or one of the operands is a mask indicating which bits should be tested.
Accessing the Condition Codes
根据condition code, 设置某个寄存器。注意,下面的指令,只设置了一个字节。如果需要得到四个字节的结果,需要手动设置。
上面表格的最后两栏,一个对于有符号数的,另一个是对于无符号数的。
对有符号数 举个例子 : 假设数字只有4比特。那表示的范围是 -8~7
a=-8, b=1时 a-b就会负溢出 结果为 -9(10111)
此时, SF为0,OF为1
a=7, b=-1时,a-b就会正溢出,结果为 8(01000)
此时,SF为1,OF为0
没有溢出时,SF为1,OF为0.
结合以上三种情况,凡是a<b都有 SF^OF=1
所以对于有符号数, 判断SF^OF就得出a b值的大小了。
对于无符号数,判断进位位 CF就可以了。
jmp的类似与set,条件是一样的。