Jump to content

Carry flag: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
THB (talk | contribs)
cat
m indef article inserted
Line 12: Line 12:
The carry flag could be considered to be the unsigned equivalent of the [[overflow flag]].
The carry flag could be considered to be the unsigned equivalent of the [[overflow flag]].


The carry flag may be used to add numbers with more bits than processor can handle normally, for example adding two 128-bit values, using special processor instruction '''add with carry (ADC)'''. Subtract and shift instructions can be extended in similar ways.
The carry flag may be used to add numbers with more bits than processor can handle normally, for example adding two 128-bit values, using the special processor instruction '''add with carry (ADC)'''. Subtract and shift instructions can be extended in similar ways.





Revision as of 22:14, 3 October 2006

In computer processors carry 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. In some machines such as the MOS Technology 6502, after a subtract operation the carry flag indicates the absence of a borrow.

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 7, 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.

Carry flag is changed in the x86 processor family by the following instructions (referring to 80386 Intel manual):

  • All arithmetic operations;
  • Compare instructions (equivalent to a subtract without storing the result).

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

The carry flag could be considered to be the unsigned equivalent of the overflow flag.

The carry flag may be used to add numbers with more bits than processor can handle normally, for example adding two 128-bit values, using the special processor instruction add with carry (ADC). Subtract and shift instructions can be extended in similar ways.