- Carry flag
In
computer processor s the carry flag (usually indicated as the C flag) is a singlebit in a system status (flag) register used to indicate when anarithmetic 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 theoverflow flag . In some machines such as theMOS Technology 6502 , after a subtract operation the carry flag indicates the absence of a borrow.Uses
In
x86 assembly language the carry flag can be used by many instructions. These include ADC ("Add with Carry"), thelogical 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).cite web |url=http://download.intel.com/design/PentiumII/manuals/24319102.PDF|format = PDF| title=Intel Architecture Software Developer's Manual, Volume 2: Instruction Set Reference Manual|accessdate=2007-10-25] 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 unsignedinteger s. 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-bittwo'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):
* All arithmetic operations;
* Compare instructions (equivalent to a subtract without storing the result).
* STC, the set carry flag, instructionThe 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, instructionCarry 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 "a"−"b", and a borrow must be performed. A subtract with borrow (SBB) instruction will compute "a"−"b"−"C", while a subtract without borrow (SUB) acts as if the borrow bit were clear. The
68k , andx86 family of processors use a borrow bit.The other takes advantage of the identity that −"x" = not("x")+1 and computes "a"−"b" 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 "a"−"b"−"C" operation "subtract with carry" (SBWC
).PA-RISC uses a carry bit convention, but calls its "a"+not("b")+"C" operation "subtract with borrow" (SUBB
).References
Wikimedia Foundation. 2010.