Carry Flag Vs. Borrow Flag
While the carry flag is well-defined for addition, there are two possible ways to use the carry flag for subtractive operations.
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 = a−(b+C), while a subtract without borrow (SUB) acts as if the borrow bit were clear. The 8080, Z80, x86 and 68k families (among others) 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 where a borrow is not required.
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 and NS320xx architectures use the borrow bit convention, but call their 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
).
Read more about this topic: Carry Flag
Famous quotes containing the words carry, flag and/or borrow:
“God: My presence will go with you, and I will give you rest.
Moses: If your presence will not go, do not carry us up from here. For how shall it be known that I have found favor in your sight, I and your people, unless you go with us?”
—Bible: Hebrew, Exodus 33:14-16.
“Up rose old Barbara Frietchie then,
Bowed with her fourscore years and ten;
Bravest of all in Frederick town,
She took up the flag the men hauled down;”
—John Greenleaf Whittier (18071892)
“Ellen, I was crazy to read your book; but I never found anybody I could borrow it from!”
—Anonymous Woman (c. 1870?)