Condition Codes

本文详细介绍了处理器中常用的条件码,如进位标志(CF)、零标志(ZF)、符号标志(SF)及溢出标志(OF),并讨论了它们如何在不同的运算指令中被设置。此外,还探讨了如何利用这些条件码进行数值比较,以及通过示例解释了有符号和无符号数的溢出检测方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,条件是一样的。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值