Why is the carry flag set during subtraction when zero is a thumbnail?

Right now I am writing my small library for arithmetic and logic operations for very large unsigned integers. To improve performance, I decided to implement some functions in the assembly. Here is my question. 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 set only during overflow, but if I subtract any number from zero, I do not get overflow. Or I'm wrong?

+7
assembly x86 flags
source share
2 answers

The carry flag is carried or borrowed from the most significant bit (MSb):

CF (bit 0) carry flag - set if the arithmetic operation generates a carry or borrow from the most significant bit of the result; cleared otherwise. This flag indicates the overflow condition for an unsigned integer arithmetic. It is also used in multi-point arithmetic.

Do not associate CF with a sign bit, in CF subtraction it is set whenever minuend, processed as unsigned, less than subtracted, is processed as unsigned.
This is equivalent to an overflow condition, for signed numbers the equivalent flag is OF.

For a visual clue (unnecessary?) In this operation 4-5, this is the second loan, red, which sets CF

Borrow and CF

No, if you subtract from zero, naturally, for any number, but zero itself, you will always have a CF-set, since subtraction has at least one bit.


Finally, some instructions allow you to change the sign bit without affecting CF (see, for example, logical operations or neg behavior).

+10
source share

We know from school that a - b = a + (-b). And that’s how we do not subtract logic, add a negative one. We also know, starting with the beginning programming classes, that with two additions, in order to get negative, you invert and add one. a - b = a + (~ b) + 1. We also know the concept of transfer from the school class. 9 + 3 = 2. The same thing in binary format, with two operands, you can have 1 + 1 = 0. Therefore, each column in the logic needs to be carried. Each of them consists of three bits in two bits, two operands plus transfer and carry out and result. Since each of these logic blocks has an input bit that carries, the normal addition that first carries is zero, but for subtraction we can do this transfer to 1 and invert the second operand to get a + b = a + (~ b) + one

Thus, subtraction is a complement if you work with a few simple examples, or it is better to try each three-bit combination of operands yourself. You will see that there is no such thing as signed or unsigned addition (or subtraction), the beauty of coding with two additions.

Knowing all this, subtraction is a complement, with the addition we get execution at UNSIGNED overflow, the overflow bit is signed when the transfer and execution of msbit do not match, usually presented as flag V. Now some architectures, since they already invert operand b on the path and on the way, they invert the execution on the output. SOME DEBT. Therefore, you need to take a look at your specific architecture to see if this overflow is running without an inscription or if it is an activity. or not to borrow or something else.

zero minus something will not always be done to add.

 0b000 - 0b111 0001 000 + 000 ===== 001 

The completion of the addition is zero. Your architecture may prefer to leave it that way, or it may choose to invert it and name it a loan.

Inside the architecture family. All x86 or all ARMs are likely to continue to do the same forever. But there is no reason to expect that ARM and MIPS and x86 and XYZ will do everything the same.

Inverting and defining it as a loan makes sense in terms of terminology.

Please note that all (signed / unsigned) values ​​are greater than, greater than or equal to, less than or equal to, are based on the choice of carry / borrow for this architecture, you cannot translate these flag comparisons by architectures if they have the same definition.

+1
source share

All Articles