为什么当减数为零时在减法期间设置进位标志? [英] Why is the Carry Flag set during a subtraction when zero is the minuend?

查看:261
本文介绍了为什么当减数为零时在减法期间设置进位标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我编写了自己的小库,用于非常大的无符号整数的算术和逻辑运算.为了提高性能,我决定在汇编中实现一些功能.所以这是我的问题. 当我减去两个无符号整数时,当我从0中减去任何数字时,进位标志就会置位.

At the moment I write my own little library for arithmetic and logical operations for very big unsigned integers. To improve performance I decided to implement some functions in assembly. So here is my question. While subtracting two unsigned integers the Carry Flag is set when I subtract any number from 0.

但是为什么在这种情况下设置了进位标志?进位标志仅在发生溢出时置位,但是如果我从零中减去任何数字,则不会溢出. 还是我错了?

But why is the Carry Flag set in this situation? The Carry Flag is only set when an overflow occurs, but if I subtract any number from zero I don't get an overflow. Or I am wrong?

推荐答案

运载标志是从最高有效位(MSb)进位或借位的:

Carry flag is carry or borrow out of the Most Significant bit (MSb):

CF(位0)进位标志-如果算术运算从结果的最高有效位产生进位或借位,则设置该位;否则,该位为1.否则清除.该标志指示以下情况的溢出条件: 无符号整数算法.它也用于多精度算术.

CF (bit 0) Carry flag — Set if an arithmetic operation generates a carry or a borrow out of the mostsignificant bit of the result; cleared otherwise. This flag indicates an overflow condition for unsigned-integer arithmetic. It is also used in multiple-precision arithmetic.

不要将CF与符号位相关联,在减法中,只要被视为无符号的被减数小于被视为无符号的减数,就设置CF.
这等效于溢出条件,对于带符号的数字,等效标志为OF.

Don't associate the CF with the sign bit, in a subtraction CF is set whenever the minuend, treated as unsigned, is less than the subtrahend, treated as unsigned.
This is equivalent to an overflow condition, for signed numbers the equivalent flag is OF.

对于(不必要的?)视觉线索,在此4-5操作中,设置CF的是第二个借项(红色借项)

For an (unnecessary?) visual clue, in this 4-5 operation, it is the second borrow, the red one, that set the CF

如果您不从零中减去,自然就不会有任何数字,但对于零本身,您将始终具有CF设置,因为subtrahend至少设置了一位.

Not if you subtract from zero, it comes naturally that for any number, but zero itself, you'll always have the CF set, as the subtrahend has at least one bit set.

最后,一些指令使您可以在不影响CF的情况下更改符号位(例如,参见逻辑操作或neg的行为).

Finally, some instructions let you change the sign bit without affecting the CF (see for example the logic operations or the behavior of neg).

这篇关于为什么当减数为零时在减法期间设置进位标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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