0
\$\begingroup\$

If a number is 8 bit, the maximum value it can have is 2^8 or (1111 1111). In the OV flag, the last digit or the MSB decides the sign, but how does the overflow flag know if it is the "signed MSB" or a normal MSB ?

eg. (1111 1111) is the number 256, but I read somewhere that as the MSB is set to 1, it is a negative number. I do not understand this as it is very confusing if the MSB is used as a sign bit or as an ordinary number with a place value.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ binary 11111111 represents -1 only in you head ... btw, start thinking in hexadecimal, not decimal ... 0xFF + 0xFF = 0x1FE ... if you considered 0xFF to be positive, then the result is correct ... if you considered 0xFF to be negative then the result -2 is also correct \$\endgroup\$
    – jsotola
    Commented May 15, 2021 at 17:26

2 Answers 2

2
\$\begingroup\$

The operations for signed and unsigned numbers are the same, but signed numbers overflow between bit 6 and 7, while unsigned numbers overflow between bit 7 and 8.

The OV flag represents the overflow into bit 7, while the CY flag represents the overflow into bit 8, so for a signed operation, you'd check the OV flag, while for an unsigned operation you'd check the CY flag after the operation.

\$\endgroup\$
0
\$\begingroup\$

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).

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.