# ov flag in MC 8051

#### bleach

##### New Member
can some one explain me pleas the idea of overflow flag with example(i am new in Microcontroller) thanks

#### ericgibbs

##### Well-Known Member
can some one explain me pleas the idea of overflow flag with example(i am new in Microcontroller) thanks
hi,
When using the internal counters/timers in a PIC they have a finite count capacity.

For example if a counter was 8 bits long, thats 0 thru 255 counts, on the next count bit the counter would 'overflow' and start counting from zero again and so on.
Each time it overflows [ or sometimes called 'rolls over'] a flag is set, which the program tests.

Is this what you mean.?

Last edited:

#### bleach

##### New Member
thanks, for the overflow flag its written in my book that if we add or subbtract any 2 number, the result of this 2 number will decide wether the OV will be set or not, under the condition -128<result>127, take this example
0FH + 7FH = 8E (I REALLY DON'T KNOW HOW HE GOT 8EH) also indecimal it will be 15 + 127 = 124
the idea isn't clear to me please can you make it easy for me
thanks

#### ericgibbs

##### Well-Known Member
thanks, for the overflow flag its written in my book that if we add or subbtract any 2 number, the result of this 2 number will decide wether the OV will be set or not, under the condition -128<result>127, take this example
0FH + 7FH = 8E (I REALLY DON'T KNOW HOW HE GOT 8EH) also indecimal it will be 15 + 127 = 124
the idea isn't clear to me please can you make it easy for me
thanks
Hi,
Write it out as a bit pattern.

0Fh = 00001111
7Fh = 01111111

8Eh = 10001110

Your sum 15+ 127 is NOT equal to 124, its 142.

So 142 decimal = 8Eh = 10001110

OK.?

EDIT: the 'OV' will not be set in the above case.

If you added say 0Fh and FFh then the OV bit will be set, as the sum exceeds FFh [ 255 decimal]

Last edited:

#### Pommie

##### Well-Known Member
The overflow flag is set when there is a carry (or borrow) from bit 6 to bit 7. This may not sound very useful until you consider signed maths. In signed maths bit 7 is the sign bit.

0x7f = +127
0x0f = +15
Add them together and we get 0x8e which is -114.
This is clearly not the right answer because there has been a carry into the sign bit.
Luckily, the overflow flag will be set and so we can detect this error and correct it.
The simplest way to correct the error is to do the whole thing again but with 16 bit variables.

Mike.

#### bleach

##### New Member
ericgibbs, thanks a lot i had a problem in coversion H to D and binary, its clear now
1- what do we mean by signed number are add or subtracted and unsigned number?
how i decide wither ov is set or not please give me examples
thanks

mr. Mike, how did you get -114, and in my book its written for the same example 8EH responses -116 how i get this responses

thanks every body

#### Pommie

##### Well-Known Member
mr. Mike, how did you get -114, and in my book its written for the same example 8EH responses -116 how i get this responses
Well, if you start with zero and subtract one then you end up with minus 1 which is 255 decimal or 0xff.

If you keep subtracting one then you get to 0x8E = -114. I assume the value in your book is a misprint.

There is also the ones complement system where 0x8E would be -113 but this is just so old that nobody uses it any more.

Mike.

Last edited:

#### bleach

##### New Member
pleeeeeeas

thanks man but still its not clear for me how we got it -114 can you please explain in more details, (this is my firs time in micro controller )

BEST REGARD

#### ericgibbs

##### Well-Known Member
thanks man but still its not clear for me how we got it -114 can you please explain in more details, (this is my firs time in micro controller )

BEST REGARD
Hi,
As Mike is in OZ,[bedtime] I'll help a little.

Look at this image.

#### Attachments

• 10 KB Views: 319

#### bleach

##### New Member

thanks a lot every body
i have seen the table and i have some questions about it:
1- here we added 0F + 7F, in the table why we start from 7F, what about 0F?
2- why we start with 0 digit directly after 7F why not 81 and then continue?
3- in the notes its written that (as a signed number,8EH represent -114, which is clearly not the correct result of 142; therefor, the OV bit is set) can you explain me this please
thanks

#### johnsmith123

##### Member
you should have a look at the 8051 ebook from Mazidi. Google it.
There are many ebooks available FREE on the internet.

#### ericgibbs

##### Well-Known Member
thanks a lot every body
i have seen the table and i have some questions about it:
1- here we added 0F + 7F, in the table why we start from 7F,
..Thats one of the numbers you wanted to add

2- why we start with 0 digit directly after 7F why not 81 and then continue?
Because 7Fh is a positive number.
If you add "1" to 7Fh you get 80h, not 81h

3- in the notes its written that (as a signed number,8EH represent -114,
In the 8Eh value, bit 7 [ the sign bit ] is SET, so it represents a negative value.

which is clearly not the correct result of 142; therefor, the OV bit is set) can you explain me this please
thanks
hi,
You are working with an 8 bit value as a SIGNED value.
Bit 7 determines whether the value is positive or negative.
So effectively you are working with only 7 bits as the 'absolute' value.
As its a signed value, it can only have a range of +127 thru 0 thru -128.

If we consider the value of '142' which you have quoted and subtract 128 we get 14 [ 0Eh].

Using 8bit signed values you cannot have a positive value of greater than 127.

Using UNSIGNED values 7Fh + 0Eh = 8Dh [141 decimal]

IIUC your book does say that is a maths 'error' condition that should be tested for.???

EDIT:

Last edited:

#### bleach

##### New Member
thanks a lot, some how i am getting the idea but still am having confusion,
If we consider the value of '142' which you have quoted and subtract 128 we get 14 [ 0Eh].

Using 8bit signed values you cannot have a positive value of greater than 127.

Using UNSIGNED values 7Fh + 0Eh = 8Dh [141 decimal]

IIUC your book does say that is a maths 'error' condition that should be tested for.???
can we take another example 55H + AAH
and FFH + 0FH
i am still having doubt about those two example,,,

thanks

#### Pommie

##### Well-Known Member
If you add unsigned numbers then
0x55 + 0xAA = 85 + 170 = 255
Doing the same with signed numbers,
0x55 + 0xAA = 85 + -86 = -1
Both of these are correct.

However,
0xff + 0x0f = 255 + 15 = 270
This result will not fit in a 8 bit register and so you end up with 14 in your register which is wrong. But when we do it signed,
0xff + 0x0f = -1 + 15 = 14
Which is correct.

Mike.

#### bleach

##### New Member
ov

If you add unsigned numbers then
0x55 + 0xAA = 85 + 170 = 255
Doing the same with signed numbers,
0x55 + 0xAA = 85 + -86 = -1
Both of these are correct.

However,
0xff + 0x0f = 255 + 15 = 270
This result will not fit in a 8 bit register and so you end up with 14 in your register which is wrong. But when we do it signed,
0xff + 0x0f = -1 + 15 = 14
Which is correct.

Mike.
the idea of signed number and unsigned number is not clear to me yet, in the

part (Doing the same with signed numbers,
0x55 + 0xAA = 85 + -86 = -1) why and how you put the (-)

also i have the condition in which if the result of adding or subtracting 2 number, is greater than 127 or less than -128, the OV will be set. is that the hexadecimal or decimal result that i decide upon..... please help me i am really confuse.

thanks