Help in How to detect an Overflow.

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Help in How to detect an Overflow.

Post by Francesco.C » Mon Jul 06, 2009 7:17 pm

Hi guys,

I need some advice regarding this:
I have a counter, X, as a Byte. (It needs to be a Byte)

The counter increments and eventually it will be greater than 255.

I want to detect this overflow. How do I do this without using assembly code?

Thanks in advance.

Regards

Francesco C.

MattH
Registered User
Registered User
Posts: 51
Joined: Mon Jan 01, 2007 8:03 pm
Location: Allentown, PA

Post by MattH » Tue Jul 07, 2009 12:56 am

If x > 255 then x = 0 end if // just change it to what ever you want to do with it...

nigle
Registered User
Registered User
Posts: 36
Joined: Tue Aug 12, 2008 3:13 pm
Location: West London

Post by nigle » Tue Jul 07, 2009 10:49 am

replace the Inc( X ) instruction with the following:

Code: Select all

If X = 255 Then
	Overflow = true
Else
	Inc( X )
EndIf
Remember to set Overflow to false at the beginning!

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Tue Jul 07, 2009 9:20 pm

Hi guys,

I need to clarify my question.

X is defined as 'Byte'. This mean it can only hold 255 max.

I x is incremented by 2.
Therefore when x=254 and the counter increments by 2, x=0.

Hence I cannot say 'If x > 255...'.

Thats why i need to check if it has excedded 255 (overflow).

Any idea?

Regards


Francesco C.

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Tue Jul 07, 2009 9:43 pm

you can not do it with a byte, which you answered yourself.
It would need a word.

If the value is just steps of two you could simply increment a counter and multiply the answer by 2 to get X.

Code: Select all

dim loop as byte
dim x as byte
dim overflow as boolean

loop=0
x=0
overflow=false

while overflow=false
   X=loop*2
   inc(loop)
   if loop=128 then
        overflow=true
   endif
wend

dman776
Posts: 115
Joined: Mon May 28, 2007 3:59 pm
Location: Texas

Post by dman776 » Wed Jul 08, 2009 4:43 am

could you just use:
inc(yourVar)
If STATUS.0 = 1 then
' do stuff because the carry bit is set
endif

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Wed Jul 08, 2009 11:18 am

And I would have thought that if you test before the incrementing you can anticipate/pre-empt overflow?
If MyCurrentBYte > (255-increment_value) then the next increment will overflow. Or something like that.

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Wed Jul 08, 2009 7:17 pm

dman776,

Thank you for the help

Yes the solution was to check the carry flag in the status register.

........
If Status.0=1 then /// the carry flag is set indicating an overflow!

do something

End If
...................

Not being very familiar with a basic compiler, I was not aware you could
check the status register so easily.

Thanks again.

Francesco C.

dman776
Posts: 115
Joined: Mon May 28, 2007 3:59 pm
Location: Texas

Post by dman776 » Wed Jul 08, 2009 10:02 pm

no problem.
And don't be afraid of ASM. It's not that bad. ;-)

This would be the equivalent in ASM+Swordfish...

---------------------------
asm
btfsc STATUS,0
endasm
myOverflowSub()
------------------------------

:-)

Post Reply