Carry flag

Carry flag

In computer processors the carry flag (usually indicated as the 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.

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).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 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):
* 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

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 "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, and x86 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.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Carry Flag —   [dt. »Überlaufanzeige«], ein Bit, das beim Überlauf eines Registers gesetzt wird (Flag) …   Universal-Lexikon

  • Carry-Flag — Das Übertragsbit (engl. Carry Bit) ist ein Begriff aus der Informatik. Er bezeichnet ein Bit, welches den Übertrag einer Addition oder Subtraktion von Bits auf das nächst höherwertige Bit enthält. Die für eine elementare Berechnung in einem… …   Deutsch Wikipedia

  • Carry — or carrying may refer to: *Carry (arithmetic), when a digit becomes bigger than limit and the extra is moved to the left **Carry flag, the equivalent in calculation in a computer *Carrying (basketball), a rule breach in basketball *Carry… …   Wikipedia

  • Flag —   [dt. »Flagge«], eine zweiwertige Variable innerhalb eines Registers in einem Prozessor, die zur Kennzeichnung eines bestimmten Zustands verwendet wird. I. d. R. handelt es sich um ein einzelnes Bit. So bezeichnet z. B. das sog. Carry Flag bei… …   Universal-Lexikon

  • Carry (arithmetic) — In elementary arithmetic a carry is a digit that is transferred from one column of digits to another column of more significant digits during a calculation algorithm. It is a central part of traditional mathematics, but is often omitted from the… …   Wikipedia

  • Flag byte — A flag byte is a generic term for a byte used to indicate conditions within a binary computer. In particular, the byte can be used to show up to 8 discrete conditions. Essentially, each bit represents one flag, capable of showing two states.… …   Wikipedia

  • Flag (Informatik) — Mit Flag [flæg] wird eine binäre Variable oder ein Statusindikator bezeichnet, welcher als Hilfsmittel zur Kennzeichnung bestimmter Zustände benutzt werden kann. Ein Flag kann gesetzt, gelöscht oder ausgelesen werden. Flags im Prozessor Ein Flag… …   Deutsch Wikipedia

  • Carry-Bit — Das Übertragsbit (engl. Carry Bit) ist ein Begriff aus der Informatik. Er bezeichnet ein Bit, welches den Übertrag einer Addition oder Subtraktion von Bits auf das nächst höherwertige Bit enthält. Die für eine elementare Berechnung in einem… …   Deutsch Wikipedia

  • Carry Bit — Das Übertragsbit (engl. Carry Bit) ist ein Begriff aus der Informatik. Er bezeichnet ein Bit, welches den Übertrag einer Addition oder Subtraktion von Bits auf das nächst höherwertige Bit enthält. Die für eine elementare Berechnung in einem… …   Deutsch Wikipedia

  • Flag of Japan — Name Nisshōki[1] or Hinomaru[2] Use …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”