At 11:48 PM 2/16/2004 +0800, you wrote:
>Dear all,
>
>I read from datasheet regarding OV flag.
>
>"This bit is used for signed arithmetic. It indicates an overflow of the
>7-bit magnitude, which causes the sign bit to change."
>
>Is that mean whenever the MSB toggled (not include the result from bsf &
>bcf), this flag will be set?
No. One counterexample will suffice. Consider the case of:
movlw 0x20 ; 32
addlw 0x80 ; -128
result: 0xA0 (-96), the MSB of w has changed, but there is no OV
You can see that overflow is not possible if the signs of the two
numbers being added are different.
What Microchip means is that the sign of the result is always incorrect
when there has been overflow.
For example,
movlw 0x80 (-128)
addlw 0x80 (-128)
result 0x00 (0)
or:
movlw 0x7F (127)
addlw 0x7F (127)
result 0xFE (-2)
I'll leave checking the rest of the possibilities or finding a proof
as an exercise. ;-)
I'm glad they added the OV flag to some newer PICs, it's something I often
missed when writing math routines.
At 11:48 PM 2/16/2004 +0800, you wrote:
>Dear all,
>
>I read from datasheet regarding OV flag.
>
>"This bit is used for signed arithmetic. It indicates an overflow of the
>7-bit magnitude, which causes the sign bit to change."
>
>Is that mean whenever the MSB toggled (not include the result from bsf &
>bcf), this flag will be set?
No. One counterexample will suffice. Consider the case of:
movlw 0x20 ; 32
addlw 0x80 ; -128
result: 0xA0 (-96), the MSB of w has changed, but there is no OV
You can see that overflow is not possible if the signs of the two
numbers being added are different.
What Microchip means is that the sign of the result is always incorrect
when there has been overflow.
For example,
movlw 0x80 (-128)
addlw 0x80 (-128)
result 0x00 (0)
or:
movlw 0x7F (127)
addlw 0x7F (127)
result 0xFE (-2)
I'll leave checking the rest of the possibilities or finding a proof
as an exercise. ;-)
I'm glad they added the OV flag to some newer PICs, it's something I often
missed when writing math routines.
> I read from datasheet regarding OV flag.
>
> "This bit is used for signed arithmetic. It indicates an overflow
> of the 7-bit magnitude, which causes the sign bit to change."
>
> Is that mean whenever the MSB toggled (not include the result from
> bsf & bcf), this flag will be set?
No. The Overflow flag indicates that the result is too large to fit
in 8 bits; it should be set whenever:
1. There was an internal carry generated between bit 6 and bit 7
of the result, but NO carry was generated out of bit 7 of
the result.
OR
2. There was NO internal carry generated between bit 6 and bit
7 of the result, but there WAS a carry generated out of bit
7.
-Andy
=== Andrew Warren -- .....aiwKILLspam.....cypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
> I read from datasheet regarding OV flag.
>
> "This bit is used for signed arithmetic. It indicates an overflow
> of the 7-bit magnitude, which causes the sign bit to change."
>
> Is that mean whenever the MSB toggled (not include the result from
> bsf & bcf), this flag will be set?
No. The Overflow flag indicates that the result is too large to fit
in 8 bits; it should be set whenever:
1. There was an internal carry generated between bit 6 and bit 7
of the result, but NO carry was generated out of bit 7 of
the result.
OR
2. There was NO internal carry generated between bit 6 and bit
7 of the result, but there WAS a carry generated out of bit
7.
-Andy
=== Andrew Warren -- aiwspam_OUTcypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
> Dear all,
>
> I read from datasheet regarding OV flag.
>
> "This bit is used for signed arithmetic. It indicates an overflow of the
> 7-bit magnitude, which causes the sign bit to change."
>
> Is that mean whenever the MSB toggled (not include the result from bsf &
> bcf), this flag will be set?
>
> Thanks & regards.
>
> WH Tan
>
I am assuming you are referring to an 18F series PIC.
Actually, the description is true, but rather poorly worded. What it could
say is:
This OV bit is used for signed arithmetic. When adding or subtracting two
eight-bit signed values it is possible for the correct result to not be
representable as an eight bit signed value. This bit is set in those cases.
For example:
+100 decimal
+100 decimal
------
+200 OV will be set, because +200 is not a valid eight-bit signed
value.
The same this will happen with
-100 decimal
-100 decimal
-----
-200
Note that it is never possible to get an overflow if the operands are of
opposite signs (for addition), or the same sign (for subtraction).
Bob Ammerman
RAm Systems
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
> Dear all,
>
> I read from datasheet regarding OV flag.
>
> "This bit is used for signed arithmetic. It indicates an overflow of the
> 7-bit magnitude, which causes the sign bit to change."
>
> Is that mean whenever the MSB toggled (not include the result from bsf &
> bcf), this flag will be set?
>
> Thanks & regards.
>
> WH Tan
>
I am assuming you are referring to an 18F series PIC.
Actually, the description is true, but rather poorly worded. What it could
say is:
This OV bit is used for signed arithmetic. When adding or subtracting two
eight-bit signed values it is possible for the correct result to not be
representable as an eight bit signed value. This bit is set in those cases.
For example:
+100 decimal
+100 decimal
------
+200 OV will be set, because +200 is not a valid eight-bit signed
value.
The same this will happen with
-100 decimal
-100 decimal
-----
-200
Note that it is never possible to get an overflow if the operands are of
opposite signs (for addition), or the same sign (for subtraction).
Bob Ammerman
RAm Systems
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads