Carry flag

From Wikipedia, the free encyclopedia

In computer processors the carry flag (sometimes indicated as C flag) is a single bit in a system status (flag) register used to indicate when an arithmetic carry or borrow has been generated out of the most significant ALU bit position. When used after an arithmetic operation it could be considered to be the unsigned equivalent of the overflow flag. In some machines such as the MOS Technology 6502, after a subtract operation the carry flag indicates the absence of a borrow.

[edit] Uses

In x86 assembly language the carry flag can be used by many instructions. These include ADC (Add with Carry), the logical shift instructions: SHL (Shift Logical Left) and SHR (Shift Logical Right) and the rotate through carry instructions: RCR (Rotate Through Carry Right) and RCL (Rotate Through Carry Left). In these instructions, the carry flag can be used as either an input (i.e. the result depends on the value of the carry flag) or an output (i.e. the carry flag is changed by the instruction).[1] The use of the carry flag in this manner enables multi-word add, subtract, shift, or rotate operations.

An example of the use of the carry flag is what happens if you were to add 255 and 255 using 8-bit unsigned integers. The mathematical answer is 510, but in binary this is 1 1111 1110 which requires nine bits. The 8-bit result is 1111 1110, or 254. Since there is carry out of bit 8, the carry flag is set to 1, indicating that the result is invalid in an 8-bit unsigned interpretation. Alternatively, the valid 9-bit result is the concatenation of the carry flag with the result. Note that in an 8-bit two's complement interpretation, the operation is -1 + -1 and yields the correct result of -2, with no overflow.

As an example of the carry flag used as input, consider an 8-bit register with the bit pattern 0101 0101, with the carry flag set (value is 1) before a RCL instruction. The result is 1010 1011, and the carry flag is cleared because the most significant bit (a zero) was rotated into the carry bit.

The carry flag is changed in the x86 processor family by the following instructions (referring to the Intel Software Developer's Manual):[1]

  • All arithmetic operations;
  • Compare instructions (equivalent to a subtract without storing the result).
  • STC, the set carry flag, instruction

The carry flag is cleared in the x86 processor family by the following instructions:

  • Logical operations - XOR, AND, OR
  • TEST (equivalent to AND without storing the result).
  • CLC, the clear carry flag, instruction

[edit] Carry flag vs. Borrow flag

While the sense of the carry flag is well-defined in addition, there are two popular ways to interpret the carry flag when subtracting.

One uses the bit as a borrow flag, setting it if a<b when computing ab, and a borrow must be performed. A subtract with borrow (SBB) instruction will compute abC, while a subtract without borrow (SUB) acts as if the borrow bit were clear. The 68k, and x86 family of processors use a borrow bit.[1]

The other takes advantage of the identity that −x = not(x)+1 and computes ab as a+not(b)+1. The carry flag is set according to this addition, and subtract with carry computes a+not(b)+C, while subtract without carry acts as if the carry bit were set. The 6502 and PowerPC processors use this convention. The 6502 is a particularly well-known example because it does not have a subtract without carry operation, so software must ensure that the carry flag is set before every subtract operation.

The modern convention is to refer to the first alternative as a "borrow bit", while the second is called a "carry bit". However, there are exceptions in both directions; the VAX architecture uses the borrow bit convention, but calls its abC operation "subtract with carry" (SBWC). PA-RISC uses a carry bit convention, but calls its a+not(b)+C operation "subtract with borrow" (SUBB).

[edit] References

Languages