关于汇编条件代码寄存器
假设我们使用addl 执行等效指令 C 表达式“t=a+b”的,其中 a,b,t 是类型变量 int,则条件代码将根据以下C设置 表达式:
CF:(无符号t)< (无符号a)无符号溢出
ZF:(t==0) 零
SF:(t<0) 负
OF: (a<0 == b<0) && (t<0 != a<0) 有符号溢出
引自计算机系统教科书。
- CF是进位标志。
- ZF 是零标志。
- SF是标志位。
- OF 是溢出标志。
我不明白为什么这些C表达式具有上面提到的效果。例如,为什么表达式(t<0)会设置SF标志? t<0 要么是 true,要么是 false(因为教科书只告诉了这些变量的类型),但是为什么设置了符号标志?我真的很困惑,请帮助..谢谢!
suppose we use the addl
instruction to perform the equivalent
of the C expression "t=a+b",where
a,b,t are variables of type
int,then the conditional code will be set according to the following C
expression:CF: (unsigned t) < (unsigned a) Unsigned Overflow
ZF: (t==0) Zero
SF: (t<0) Negative
OF: (a<0 == b<0) && (t<0 != a<0) Signed Overflow
Quted from a computer system textbook.
- CF is the carry flag.
- ZF is the zero flag.
- SF is the sign flag.
- OF is the overflow flag.
i can't figure out why these C expressions have the effect mentioned above.for example,why the expression (t<0) will set the SF flag?? t<0 is either true,or false(since the textbook only told the type of these variables),but why the sign flag has been set?I'm really confused,please help..thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CPU 在执行算术运算后设置这些标志。 您所谓的“C 表达式”实际上是对设置各种 CPU 标志的条件的描述。 例如,如果结果为 0,则将设置零标志。
或者为了解决您的具体问题,如下:
SF:(t<0) 负数
意味着如果算术运算的结果为负数,则 CPU 设置 SF 标志。 't<0' 不是 C 表达式 - 它只是解释何时设置标志。
稍后可以使用根据标志值有条件分支的指令,将标志用于控制流。
The CPU sets those flags after carrying out arithmetic operations. What you call "C expressions" are really a description of the conditions under which the various CPU flags are set. For example, if the result is 0, the zero flag will be set.
Or to address you specific question, the following:
SF: (t<0) Negative
means that if the result of an arithmetic operation is negative, the CPU sets the SF flag. The 't<0' is not a C expression - it just explains when the flag is set.
The flags can be used later for control flow, using instructions that branch conditionally on the value of the flags.
CF、ZF、SF 和 OF 是 CC(条件代码)寄存器中的单个位。 还有在其他条件下设置的其他位。 每当CPU执行某些指令(包括
add
和sub
)时,它都会根据操作结果设置这些位。 指令cmp
和test
的功能分别与sub
和and
指令相同,只是它们完全丢弃结果,唯一的输出是条件标志。假设我们有以下 C 代码:
一个简单的编译器可能会这样编译:
但是,一个更聪明的编译器会意识到
sub
指令已经设置了条件代码,因此它可以像这样编译它:请注意,
jl
指令(小于则跳转)当且仅当 SF ≠ OF 时才进行跳转。 也就是说,如果结果为负且未发生溢出,或者如果结果为正且发生溢出,则跳转。 这对于确保差异溢出时获得正确的结果是必要的(例如,将INT_MIN
与INT_MAX
进行比较)。CF, ZF, SF, and OF are singular bits within the CC (condition code) register. There are also other bits which are set under other conditions. Whenever the CPU executes certain instructions (including
add
andsub
), it sets those bits according to the result of the operation. The instructionscmp
andtest
function identically to thesub
andand
instructions respectively, except they completely discard the result, and the only output is the condition flags.Suppose we have the following C code:
A naïve compiler might compile this as so:
A smarter compiler, though, would realize that the
sub
instruction already sets the condition codes, so it could compile it like this:Note that the
jl
instruction (jump if less than) takes the jump if and only if SF ≠ OF. That is, it jumps if the result is negative and overflow did not occur, or if the result is positive and overflow did occur. This is necessary to ensure that the correct result is obtained when the difference overflows (e.g. comparingINT_MIN
withINT_MAX
).这仍然不是一个明确的问题,但这是我收集到的。 我想你想知道“当我所做的只是 t=a+b 时,为什么要设置符号标志(SF)?” 我会回答这个问题。
简单的答案是“int”C 类型是有符号的,您必须说“unsigned int”才能获得无符号版本。 因此,在t=a+b的情况下,这些变量都是有符号的。 现在解释为什么可以设置该标志:
如果您想了解更多有关 C 中出现的数字符号的信息,那么我建议您查看 Ones Complement 和 Twos Complement。
我希望这能回答这个问题。
It's still not a clear question but here is what I gleaned. I think you want to know "Why is the Sign Flag (SF) set when all I'm doing is t=a+b?" I'll answer that.
The simple answer is that the 'int' C type is signed, you have to say 'unsigned int' to get the unsigned version. Therefore, in the situation t=a+b, those variables are all signed. Now for why the flag could be set:
If you want to know more about signs on numbers as they appear in C then I would suggest that you look at Ones Complement and Twos Complement.
I hope that answers the question.
事实恰恰相反。 当你写 t < C 中的 0,这被编译为以 S 标志为条件的分支(通常也称为 N 表示否定)。 在处理器中,S 标志只是从结果的最高位复制而来。
It is the other way round. When you write t < 0 in C, this is compiled to a branch which is conditional on the S flag (also often called N for Negative). In the processor the S flag is just copied from the highest bit of the result.