My lecturer said that for signed numbers, carry flag is not relevant. Now I get it that the carry flag does not mean anything but it can be set or 1 right?
The same was said for unsigned numbers that overflow flag is not relevant. Now I get it that it is not relevant but it can be set right?
LDI R16,0x84
LDI R17,0x85
ADD R16,R17
For signed numbers:
When I add 10000100 which is -124 to 10000100 which is -123, the result is 9 which is not right and hence the overflow flag is set as sum of two negative numbers cannot be a negative number. As I get a carry in this Math while solving which is 100001001 , the 1 is dropped out showing 00001001 , but the carry flag will be on right?
For unsigned numbers :
When I perform the same calculation: that is adding 10000100 to 10000100 which in decimal means 133 and 132 respectively for unsigned numbers and the sum is 100001001 , the 1 as we know is the carry giving us 00001001. Here we have a carry, so will carry bit be set as well?
CodePudding user response:
The computer does not know how you interpret the numbers. It is up to you as the programmer to interpret the numbers as signed or unsigned.
The carry flag is set when the produced result of the arithmetic operation exceeds the bits of the register. Which indicates that if you are interpreting the value as unsigned than the output result is wrong and you should take some measurements to fix it.
The overflow flag is set as the "XOR" operation of the carry going into the MSB and the carry coming out of the MSB. Meaning during operation, If a carry goes into the MSB but does not come out of the MSB, the overflow flag would be set, or if a carry does not go into the MSB(The bits before the msb does not produce a carry) but a carry is produced by the MSB then the Overflow Flag would also be set. This would indicate that if you are interpreting the numbers as signed, then you cannot accept the produced results as correct and should take measurements to produce the correct value.
There are situations when both can be true. For example: 0111111111111110 0111111111111110 will set overflow flag but not the carry flag. For 1111111111111110 1111111111111110 the overflow flag would not be set even though the value exceeds the register and only the carry flag will be set. 1011111111111110 1011111111111110 will set both Overflow and Carry flag. I hope this will help you find out when the carry flag and overflow flag will be set at the same time and not in the future.
PS: I am new to answering questions on stack overflow. Kindly let me know If I have made any mistakes.