了解汇编中的符号和溢出标志 [英] Understanding sign and overflow flag in assembly

查看:46
本文介绍了了解汇编中的符号和溢出标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于汇编中的 cmp 指令的.我无法理解我的书如何推理 SFOF 标志.

This question is about the cmp instruction in assembly. I cannot understand how my books reasoning regarding the SF and OF flags.

 cmp vleft, vright

根据我的书:对于有符号整数,有三个重要的标志:零 (ZF) 标志、溢出 (OF) 标志和符号(SF) 标志.如果操作的结果上溢(或下溢),则设置上溢标志.如果运算结果为负,则设置符号标志.如果vleft = vright,则设置ZF(就像无符号整数一样).如果 vleft >vrightZF 未设置,SF = OF.如果 vleft ZF 未设置,SF != OF.不要忘记其他指令也可以改变FLAGS寄存器,而不仅仅是CMP.

According to my book: For signed integers, there are three flags that are important: the zero (ZF) flag, the overflow (OF) flag and the sign (SF) flag. The overflow flag is set if the result of an operation overflows (or underflows). The sign flag is set if the result of an operation is negative. If vleft = vright, the ZF is set (just as for unsigned integers). If vleft > vright, ZF is unset and SF = OF. If vleft < vright, ZF is unset and SF != OF. Do not forget that other instructions can also change the FLAGS register, not just CMP.

首先,让我们考虑 vleft >vright 案例.我的书是这样说的:

First, let's consider the vleft > vright case. My book says the following:

如果 vleft > vright 为什么 SF = OF?如果没有溢出,则差值将具有正确值且必须为非负值.因此,SF = OF = 0. 但是,如果有溢出,差异不会具有正确的值(实际上将是负值).因此,SF = OF =1.

第一部分我明白SF = OF = 0.例如,它可以是:

The first part i understand that SF = OF = 0. It could for example be:

0111 - 0101 = 0111 + 1010 + 1 = 10010 = 7 - 5 = 2

这不会设置 OFSF 标志.

This would not set the OF or SF flag.

这种情况也可能是:

1100 - 0101 = 1100 + 1010 + 1 = 10111 = -4 - 5 = 7(-9 如果我们有更多位)

这不会设置 SF 标志(因为答案是正 7)但会设置 OF = 1,因此 SF != OF.这显然违背了我的书的解释,即它们应该是平等的.

This would not set the SF flag (since the answer is the positive 7) but would set OF = 1, thus SF != OF. This clearly goes against the explanation of my book which says they should be equal.

我在这里遗漏了什么?

谢谢!

推荐答案

你要撞墙了,抱歉 :)

You are gonna bang your head on the wall, sorry about that :)

在您的示例中,-4 不大于 5!所以是的,OF 将是 1 并且 SF 将是 0,并且它们不会相等,并且这意味着 -4 小于 5,这是正确的.

In your example, -4 is not greater than 5! So yeah, OF will be 1 and SF will be 0, and they won't be equal, and that means -4 is less than 5, and that's correct.

为了说明 SF = OF = 1 的情况,将操作数反转:检查是否 5 >-4 通过做

To illustrate the SF = OF = 1 case reverse the operands: check if 5 > -4 by doing

5 - (-4) = 5 + 4 = 1001b = -7

这篇关于了解汇编中的符号和溢出标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆