This articlerelies excessively onreferences toprimary sources. Please improve this article by addingsecondary or tertiary sources. Find sources: "Carry flag" – news ·newspapers ·books ·scholar ·JSTOR(December 2018) (Learn how and when to remove this message) |
Incomputer processors, thecarry flag (usually indicated as theC flag) is a singlebit in a systemstatus register/flag register used to indicate when anarithmeticcarry or borrow has been generated out of themost significantarithmetic logic unit (ALU) bit position. The carry flag enables numbers larger than a single ALU width to be added/subtracted by carrying (adding) a binary digit from a partial addition/subtraction to theleast significant bit position of a more significant word. This is typically programmed by the user of the processor on the assembly or machine code level, but can also happen internally in certain processors, viadigital logic ormicrocode, where some processors have wider registers and arithmetic instructions than (combinatorial, or "physical") ALU.[1] It is also used to extendbit shifts and rotates in a similar manner on many processors (sometimes done via a dedicated flag). For subtractive operations, two (opposite) conventions are employed as most machines set the carry flag on borrow while some machines (such as the6502 and thePIC) instead reset the carry flag on borrow (and vice versa).
The carry flag is affected by the result of most arithmetic (and typically several bitwise) instructions and is also used as an input to many of them. Several of these instructions have two forms which either read or ignore the carry. Inassembly languages these instructions are represented bymnemonics such asADD/SUB,ADC/SBC (ADD/SUB including carry),SHL/SHR (bit shifts),ROL/ROR (bit rotates),RCR/RCL (rotate through carry), and so on.[2] The use of the carry flag in this manner enables multi-word add, subtract, shift, and rotate operations.
An example is what happens if one were to add 255 and 255 using8-bit registers. The result should be 510 which is the 9-bit value111111110 in binary. The 8 least significant bits always stored in the register would be11111110 binary (254 decimal) but since there is carry out of bit 7 (the eight bit), the carry is set, indicating that the result needs 9 bits. The valid 9-bit result is the concatenation of the carry flag with the result.
For x86 ALU size of 8 bits, an 8-bit two's complement interpretation, the addition operation11111111 +11111111 results in111111110,Carry_Flag set,Sign_Flag set, andOverflow_Flag clear.
If11111111 represents two's complement signed integer −1 (ADD al,-1), then the interpretation of the result is -2 becauseOverflow_Flag is clear, andCarry_Flag is ignored. The sign of the result is negative, becauseSign_Flag is set.11111110 is the two's complement form of signed integer −2.
If11111111 represents unsigned integer binary number 255 (ADD al,255), then the interpretation of the result would be 254, which is not correct, because the most significant bit of the result went into theCarry_Flag, which therefore cannot be ignored. TheOverflow_Flag and theSign_Flag are ignored.
Another example may be an 8-bitregister with the bit pattern01010101 and the carry flag set; if we execute arotate left through carry instruction, the result would be10101011 with the carry flag cleared because the most significant bit (bit 7) was rotated into the carry while the carry was rotated into the least significant bit (bit 0).
The early microprocessorsIntel 4004 andIntel 8008 had specific instructions to set as well as reset the carry flag explicitly. However, the laterIntel 8080 (andZ80) did not include an explicit reset carry opcode as this could be done equally fast via one of the bitwise AND, OR or XOR instructions (which do not use the carry flag).
The carry flag is also often used following comparison instructions, which are typically implemented by subtractive operations, to allow a decision to be made about which of the two compared values is lower than (or greater or equal to) the other. Branch instructions which examine the carry flag are often represented bymnemonics such asBCC andBCS to branch if the carry is clear, or branch if the carry is set respectively. When used in this way the carry flag provides a mechanism for comparing the values as unsigned integers. This is in contrast to theoverflow flag which provides a mechanism for comparing the values as signed integer values.
This sectionneeds additional citations forverification. Relevant discussion may be found on thetalk page. Please helpimprove this article byadding citations to reliable sources in this section. Unsourced material may be challenged and removed.(July 2015) (Learn how and when to remove this message) |
While the carry flag is well-defined for addition, there are two ways in common use to use the carry flag for subtraction operations.
The first uses the bit as a borrow flag, setting it ifa<b when computinga−b, and a borrow must be performed. Ifa≥b, the bit is cleared. Asubtract with borrow (SBB) instruction will computea−b−C =a−(b+C), while a subtract without borrow (SUB) acts as if the borrow bit were clear. The6800,680x0,8051,8080/Z80, andx86[2] families (among others) use a borrow bit.
The second uses the identity that −x = (notx)+1directly (i.e. without storing the carry bit inverted) and computesa−b asa+(notb)+1. The carry flag is set according to this addition, andsubtract with carry computesa+not(b)+C, while subtract without carry acts as if the carry bit were set. The result is that the carry bit is set ifa≥b, and clear ifa<b. TheSystem/360,[3]ARM,POWER/PowerPC,6502,MSP430,COP8,Am29000,i960, and88000 processors use this convention. The 6502 is a particularly well-known example because it does not have a subtractwithout carry operation, so programmers must ensure that the carry flag is set before every subtract operation where a borrow is not required.[4]
| Carry or borrow bit | Subtract without carry/borrow | Subtract with borrow | Subtract with carry |
|---|---|---|---|
| C = 0 | a −b =a + not(b) +1 | a −b −0 =a + not(b) +1 | a −b −1 =a + not(b) +0 |
| C = 1 | a −b −1 =a + not(b) +0 | a −b −0 =a + not(b) +1 |
The processors listed above, which include the most popular microprocessors of the last few decades, call these operations "subtract with borrow" and "subtract with carry", respectively, but the nomenclature is far from consistent. TheVAX,NS320xx,Fairchild Clipper andAtmel AVR architectures use the borrow bit convention, but call theira−b−C operation "subtract with carry" (SBWC,SUBC,SUBWC andSBC). ThePA-RISC andPICmicro architectures use the carry bit convention, but call theira+not(b)+C operation "subtract with borrow" (SUBB andSUBWFB). Still others, such as theH8, call theirs "subtract extended" (SUBX).SPARC uses the borrow convention, theSUBX mnemonic, and the "subtract with carry" name.
TheMotorola 6809 uses the borrow bit convention andboth nomenclatures, calling the operation "subtract with borrow", but assigning it the mnemonic abbreviationSBC.[5]
TheST6 8-bit microcontrollers go both ways in a different sense. Although they do not have any sort of "subtract with carry" instruction, they do have a carry bit which is set by a subtract instruction, and the convention depends on the processor model. The ST60 processor uses the "carry" convention, while the ST62 and ST63 processors use the "borrow" convention.[6]
{{cite book}}: CS1 maint: bot: original URL status unknown (link)