越位越大,如何设置进位标志? [英] How is Carry Flag set when subtrahend is larger?

查看:95
本文介绍了越位越大,如何设置进位标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道SUB期间的进位标志会在被减数小于subtrahend时设置,并且需要借位,但是找不到任何更详细的解释方法.

I know the Carry flag during SUB is set whenever the minuend is smaller than the subtrahend and a borrow is required, but haven't been able to find anything explaining this in more detail.

既然减法实际上只是加上二进制补码,CPU如何知道减法数更大并且发生借位了?

Since subtraction is actually just adding with two's complement, how does the CPU know that the subtrahend is larger and a borrow has occurred?

我唯一想到的是,只要将subtrahend转换为其2的补码,就可以在SUB期间自动设置Carry标志.然后,除非再次发生进位(每次被加数大于次要价位时,在进位时都应该这样做),否则它将保持打开状态.

The only thing I can think of is that maybe the Carry flag is set automatically during SUB, whenever converting the subtrahend to its 2's complement. Then unless another Carry occurs (as it should during the addition whenever the minuend is larger than the subtrahend) to switch it back off, it stays on.

还是我要离开?

推荐答案

您似乎混淆了进位和溢出标志.
进位标志检查无符号的溢出,而溢出标志检查有符号的溢出.
起作用的方式是,在减去之前对(无符号)数字进行比较,并且如果b> a则设置CF.
如果第一个操作数的符号更改,则设置溢出.

You seem to confuse the carry and overflow flags.
The carry flag checks unsigned overflow and the overflow flag checks signed overflow.
The way this works is that prior to subtraction the (unsigned) numbers are compared and if b > a then CF is set.
If the sign of the first operand changes overflow is set.

在减法期间,CPU不在乎操作数是否为负.
如果无符号基数小于要减去的无符号数,则会产生进位.

During the subtraction the CPU does not care whether the operands are negative.
If the unsigned base is smaller than the unsigned number being subtracted then a carry occurs.

例如(刚刚在调试器的CPU视图中进行了测试)

E.g. (just tested in the CPU view of my debugger)

 0 - -1 = 1  ->> CF = 1, because 0xFFFFFFFF > 0  
10 - -1 = 11 ->> CF = 1, same reason
-2 -  4 = -6 ->> CF = 0, because 0x4 < 0xFFFFFFFE  

请记住,在带符号算术中,这些结果是正确的,但在无符号算术中,它们相差*方式*,因此CF = 1.

进位标志不知道或不关心符号,它(仅)用于无符号溢出. 请记住,CPU无法知道您是要执行签名操作还是未签名操作.由应用程序测试相关标志以解释结果,这就是CPU提供两组溢出标志的原因.

The carry flag does not know or care about sign, it is (only) meant for unsigned overflow. Remember the CPU has no way of knowing whether you want to perform signed or unsigned operations. It is up to the application to test the relevant flags to interpret the results, this is why the CPU provides two sets of overflow flags.

以下是最常见的标志及其功能的列表:

Here's a list of the most common flags and their function:

code | descripton     |name     | When set
---+++----------------+---------+-------------------
CF  unsigned overflow  Carry      If unsigned over-/underflow occurs
OF  signed overflow    Overflow   If sign bit (MSB) flips
SF  Sign flag          Sign       If MSB is set, i.e. is result is negative
ZF  Zero flag          Zero       If Result is zero

这两个标志都没有太大的智能"功能.
您需要记住,这些标志是在晶体管供不应求时设计的.

Neither of these flags have much in the way of 'intelligence'.
You need to remember that these flags were designed when transistors where in short supply.

关于2的补码
2的补码的设计使加法和减法无需了解从正向负的转换.
您只需加减即可,而不必担心操作数或结果是正数还是负数.
唯一的警告发生在溢出时,这正是进位标志测试的内容.

About 2's complement
2's complement is designed so that addition and subtraction don't need to know about the conversion from positive to negative.
You can just add and subtract without care if the operand or the result will be positive or negative.
The only caveats occur on overflow, which is exactly what the carry flag tests.

如果您在哪里使用普通反演(即1的补码)作为负数,那么您将需要各种加法和减法保护措施;这就是为什么普遍使用2的补码的原因.

If you where to use plain inversion (i.e. 1's complement) as negative numbers then you'd need all sorts of safeguards around addition and subtraction; this is why 2's complement is universally used.

进一步阅读
http://teaching.idallen.com/dat2343/10f/notes/040_overflow. txt
感谢彼得

Further reading
http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt
Thanks Peter

这篇关于越位越大,如何设置进位标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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