This is the beauty of twos complement. For addition and subtraction, you the programmer cares about signed vs unsigned it is the same adder logic. Subtraction is done by addition as well x = a - b = a + (-b) as we learned in grade school. And -b = invert and add one = ~b + 1. So you invert the second operand and set or invert the carry in to the lsbit.
The carry flag means the carry out of the msbit. It indicates an unsigned overflow with respect to addition. The overflow flag represents a signed overflow. You examine the carry in and carry out of the msbit, if they do not match then signed overflow. In both cases indicating that there are not enough bits to store the result, so the result is incorrect, it is the answer with a bit trimmed off.
Four bits would give us 0x0 to 0xF 0 - 15 unsigned or 0x0 to 0x7, positive 0 - 7 and 0xF, 0xE, 0xD..0x8 -1, -2, -3 to -8.
We know that 10 + 7 = 17 and wont fit 10 indicating that this is unsigned so
0
1010
+ 0111
========
11100
1010
+ 0111
========
0001
The answer would be 0x11 or 17 but this is a four bit example so we have 0x1 with a carry out of 1 indicating unsigned overflow.
For signed math we know that we are limited to -8 to +7 for four bits so 5 + 4 would be a signed overflow.
01000
0101
+0100
======
1001
the carry in to the msbit is a 1, the carry out is a 0, they dont match so this is a signed overflow. 5 + 4 != -7 but the same bits same logic 5 + 4 = 9 unsigned and the carry out is a zero so it is not an unsigned overflow.
4 - 5 = -1. In grade school you would do a 5 - 4 and then deal with the sign. But in logic we do not need to do that, we use twos complement notation. so 4 - 5 = 4 + (-5) = 4 + ~5 + 1 or
1 <- plus one
0100
+ 1010 <- invert
===========
00001
0100
+ 1010
===========
1111
Some processors invert the carry out and call it a borrow, I do not know off hand what the 8051 does. But the carry out is an inverted borrow, so if you see a 0 that means there was a borrow, and unsigned you can see we are subtracting 10 from 4 so there is definitely a borrow happening, w could do long subtraction just like in grade school but using binary and see the borrow (because also 4 - 5 is doing a borrow).
-4 - 5 = -9 this would be a signed overflow as we cant represent -9 in 4 bits.
10001
1100
+1010
======
0111
again note the carry out is a 1 the carry in to the msbit is a 0, they do not match so this is a signed overflow. -4 - 5 = 0x17 in 5 bits but in four bits it is a 0x7 with a signed overflow.
A very simple exercise for the reader but if you go through the 8 combinations of carry in and the two operand bits for the msbit, the cases where the carry in and carry out do not match are when the operand bits match but the result bit does not match them so operand msbits are both 1 and the result bit is a 0 as shown above or the operand msbits are both 0 and the result msbit is a 1 as shown above with the 5+4 = 9 if the operand bits do not match you cannot get a signed overflow. so you will see some logic written to examine the msbits to determine the signed overflow and you will see some examine the carry in and carry out, both work equally well (functionally).