{{Use American English|date = January 2019}} {{Short description|Processor flag indicating whether unsigned arithmetic overflow has occurred}} {{primary sources|date=December 2018}} In computer processors, the '''carry flag''' (usually indicated as the '''C flag''') is a single bit in a system status register/flag register used to indicate when an arithmetic carry or borrow has been generated out of the most significant arithmetic 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 the least 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, via digital logic or microcode, where some processors have wider registers and arithmetic instructions than (combinatorial, or "physical") ALU.<ref>The well known Z80 and 68000 are examples of such CPUs, among many others.</ref> It is also used to extend bit 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 the 6502 and the PIC) instead reset the carry flag on borrow (and vice versa).
== Uses == 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. In assembly languages these instructions are represented by mnemonics such as <code>ADD/SUB</code>, <code>ADC/SBC</code> (<code>ADD/SUB</code> including carry), <code>SHL/SHR</code> (bit shifts), <code>ROL/ROR</code> (bit rotates), <code>RCR/RCL</code> (rotate through carry), and so on.<ref name = "intel">{{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}}</ref> The use of the carry flag in this manner enables multi-word add, subtract, shift, and rotate operations.
Consider adding 255 + 255 (<code>11111111</code> + <code>11111111</code>). The result should be 510, which is the 9-bit value <code>111111110</code> in binary. However, if using an 8-bit register, only the 8 least significant bits will be stored in the register, binary <code>11111110</code> (decimal 254 decimal). Since there is carry out of bit 7 (the eighth 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.
The other common ALU status register bits will be <code>Sign_Flag</code> set, <code>Zero_flag</code> clear, and <code>Overflow_Flag</code> clear.
If <code>11111111</code> represents two's complement signed integer −1 (x86 instruction <code>ADD al,-1</code>), then the interpretation of the result is −2 because <code>Overflow_Flag</code> is clear, and <code>Carry_Flag</code> is ignored. The sign of the result is negative, because <code>Sign_Flag</code> is set. <code>11111110</code> is the two's complement form of signed integer −2.
If <code>11111111</code> represents unsigned integer binary number 255 (x86 instruction <code>ADD al,255</code>), then the interpretation of the result would be 254, which is not correct, because the most significant bit of the result went into the <code>Carry_Flag</code>, which therefore cannot be ignored. The <code>Overflow_Flag</code> and the <code>Sign_Flag</code> are ignored.
Another example may be an 8-bit register with the bit pattern <code>01010101</code> and the carry flag set; if we execute a ''rotate left through carry'' instruction, the result would be <code>10101011</code> 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).
Most microprocessors, all the way back to the Intel 4004 and Intel 8008, have specific instructions to set and reset the carry flag. One exception was the Intel 8080 (and its derivative Z80), which did not include an explicit reset carry opcode, as the operation could be performed by any of the bitwise logical instructions (AND, OR, XOR), which cleared the carry flag.
The carry flag is also often used following comparison instructions, which are typically implemented as subtraction operations, to allow a decision to be made about which of the two compared values is less than (or greater or equal to) the other. Branch instructions which examine the carry flag are often represented by mnemonics such as <code>BCC</code> and <code>BCS</code> 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 the overflow flag which provides a mechanism for comparing the values as signed integer values.
== Vs. borrow flag == {{Refimprove section|date=July 2015|talk=How to improve references in "Carry flag vs. borrow flag"}} 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 if ''a''<''b'' when computing ''a''−''b'', and a borrow must be performed. If ''a''≥''b'', the bit is cleared. A '''subtract with borrow''' (<code>SBB</code>) instruction will compute ''a''−''b''−''C'' = ''a''−(''b''+''C''), while a subtract without borrow (<code>SUB</code>) acts as if the borrow bit were clear. The 6800, 680x0, 8051, 8080/Z80, and x86<ref name=intel/> families (among others) use a borrow bit.
The second uses the identity that −''x'' = (not ''x'')+1 ''directly'' (i.e. without storing the carry bit inverted) 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 result is that the carry bit is set if ''a''≥''b'', and clear if ''a''<''b''. The System/360,<ref>{{cite book |title=IBM System/360 Principles of Operation |url=http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/360/princOps/A22-6821-0_360PrincOps.pdf |page=28 |id=IBM Form A22-6821-0}}</ref> ARM, POWER/PowerPC, 6502, MSP430, COP8, Am29000, i960, and 88000 processors use this convention. The 6502 is a particularly well-known example because it does not have a subtract ''without'' carry operation, so programmers must ensure that the carry flag is set before every subtract operation where a borrow is not required.<ref>{{cite journal |title=SBC, TSX and TXS instructions of the 6800 and 6502 |first=B.T.G. |last=Tan |journal=Dr. Dobb's Journal |issue=79 |date=May 1983 |pages=67–68 |url=http://archive.6502.org/publications/dr_dobbs_journal_selected_articles/sbc_tsx_txs_instructions.pdf }}</ref> {| class="wikitable" style="margin: 0 auto; text-align: center;" |+ Summary of different uses of carry flag in subtraction |- ! Carry or<br/>borrow bit ! Subtract without<br/>carry/borrow ! Subtract<br/>with borrow ! Subtract<br/>with carry |- ! ''C'' = 0 |rowspan=2 | ''a'' − ''b''<br/>= ''a'' + not(''b'') + '''1''' | ''a'' − ''b'' − '''0'''<br/>= ''a'' + not(''b'') + '''1''' | ''a'' − ''b'' − '''1'''<br/>= ''a'' + not(''b'') + '''0''' |- ! ''C'' = 1
| ''a'' − ''b'' − '''1'''<br/>= ''a'' + not(''b'') + '''0''' | ''a'' − ''b'' − '''0'''<br/>= ''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. The VAX, NS320xx, Fairchild Clipper and Atmel AVR architectures use the borrow bit convention, but call their ''a''−''b''−''C'' operation "subtract with carry" (<code>SBWC</code>, <code>SUBC</code>, <code>SUBWC</code> and <code>SBC</code>). The PA-RISC and PICmicro architectures use the carry bit convention, but call their ''a''+not(''b'')+''C'' operation "subtract with borrow" (<code>SUBB</code> and <code>SUBWFB</code>). Still others, such as the H8, call theirs "subtract extended" (<code>SUBX</code>). SPARC uses the borrow convention, the <code>SUBX</code> mnemonic, and the "subtract with carry" name.
The Motorola 6809 uses the borrow bit convention and ''both'' nomenclatures, calling the operation "subtract with borrow", but assigning it the mnemonic abbreviation <code>SBC</code>.<ref>{{cite book |title=MC6809-MC6809E Microprocessor Programming Manual |author=Motorola Inc. |date=March 1981 |page=A-61 |id=M6809PM/AD |url=https://www.maddes.net/m6809pm/appendix_a.htm |archive-url=https://web.archive.org/web/20250123202438/https://www.maddes.net/m6809pm/appendix_a.htm#page=109 |archive-date=2025-01-23 |url-status=bot: unknown |access-date=2025-01-03 }}</ref>
The ST6 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.<ref>{{cite web |title=ST6 Family Programming Manual |version=Revision 2.0 |date=October 2004 |publisher=STMicroelectronics |url=http://www.st.com/content/ccc/resource/technical/document/programming_manual/4d/05/d1/a5/a0/9e/40/8b/CD00004606.pdf/files/CD00004606.pdf/jcr:content/translations/en.CD00004606.pdf#page=21 |pages=21–22,42 |accessdate=2017-02-28}}</ref> <!-- To summarize, and support the assertion "most common", here's a chart of processor families:
Convention Name "Borrow" "Carry" Naming unclear Borrow Intel 8051 VAX MC6809 8080/Z80/x86 NS32k ST62/ST63 Motorola 6800 AVR H8 calls it SUBX = subtract extended Motorola 68000 Fairchild Clipper NEC μPD78C1xA STM8 PDP/11 has a borrow bit, but no subtract using it. 78K/RL78 SPARC?
Carry PA-RISC S/360 ST60 PICmicro 6502 ARM COP8 MSP430 POWER/PowerPC 88000 Am29000 i960
The borrow convention gets called "subtract with carry" a lot, but the "borrow" terminology is used by the 600-pound gorilla of x86, and also the historically important Z80 and 68k families. The most popular architecture calling it "carry" is the AVR of Arduino fame. (Although VAX and SPARC are historically important.)
The carry convention is pretty consistently called "carry" (including by Intel and Motorola), with ARM, POWER, and S/360 all being current, and the 6502 being historically important. The most popular architecture calling it "borrow" is PICmicro series. -->
==See also== * Binary arithmetic * Half-carry flag * Status register
==References== {{reflist}}
==External links== * [http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt Carry Flag and Overflow Flag in binary arithmetic] * [https://brodowsky.it-sky.net/2013/12/22/carry-bit-how-does-it-work/ Carry Bit: How does it work?]
{{X86 assembly topics}}
Category:Computer arithmetic