# Carry flag in 8080/8085 subtraction

How is the carry flag set during a subtraction by the 8080 or 8085 processor?

For addition, the carry flag is set if the result is greater than 255 (i.e. if it overflows).

From the programmer's manual:

Notice that the SUB instruction complements the carry generated by the two's complement addition to form a 'borrow' flag.

Does this mean it simply flips the carry flag that would be generated by addition as described above? (I.e. sets it when the result does not overflow i.e. less than or equal to 255?)

The 8080 sets the carry flag when the unsigned value subtracted is greater than the unsigned value it is subtracted from. So for `SUB B` carry is set if and only if the unsigned value of `B` register is greater than the unsigned value of `A` register.

What the manual is getting at is that subtraction can be performed by adding the two's complement of the value being subtracted. In that case a carry would result when the subtracted value is smaller than or equal to the value being subtracted from. Thus they point out that the carry flag takes on the opposite of that carry from adding the 2's complement.

I can only guess that the manual mentions this because the 8080 performs the subtraction this way internally or that an electrical engineer would naturally think of performing subtraction in this way and might be surprised that carry is not what they might expect.

However, rest assured that it treats the carry flag consistently so that multi-byte subtraction can be performed with `SUB` and `SBB` instructions. For example, this code will subtract the 24 bit value in `BCD` from the 24 bit value in `EHL`:

``````   LD   A,L
SUB  D
LD   L,A
LD   A,H
SBB  C
LD   H,A
LD   A,E
SBB  B
LD   E,A
``````
• Suppose you want to subtract 5 from 255 (which is -1 if interpreted as signed). (I.e. `-1 - 5` ) Since 5 is less than 255, the carry bit won't be set? Mar 11, 2018 at 22:27
• Also, suppose you want to subtract 255(which is -1 if interpreted as signed) from 252(which is -4 if interpreted as signed) (i.e. `(-4) - (-1) `) Since 255 is greater than 252, the carry bit is set? Mar 11, 2018 at 22:32
• Yes in both cases. The carry is only determined based on the unsigned values. If you're working with signed values the analogous condition is overflow but the 8080 and 8085 don't have a flag for that. Mar 11, 2018 at 23:37