And still this problem with assembler and overflow
ErNa
Posts: 1,753
Again and still I face this problem:
I want to make a timeout, and that looks like this:
TimedOut is the time to test
MaxDura is the duration of one period
Every time, TimeOut elapses, TimeOut is incremented by MaxDura
Now I compare TimedOut to the counter, whenever the counter exceeds TimeOut, I signal that event
Running in a loop:
The problem comes from when the TimedOut wraps around, because now TimedOut is small and Cnt still big.
Is there an elegant solution? Thanks in advance
I want to make a timeout, and that looks like this:
TimedOut is the time to test
MaxDura is the duration of one period
Every time, TimeOut elapses, TimeOut is incremented by MaxDura
Now I compare TimedOut to the counter, whenever the counter exceeds TimeOut, I signal that event
Running in a loop:
cmp TimedOut, Cnt wc ' see if timeout elapsed if_c add TimedOut, MaxDura ' next timeout time if_c wrlong MaxDura, pVsp_CnPtr ' store duration to global
The problem comes from when the TimedOut wraps around, because now TimedOut is small and Cnt still big.
Is there an elegant solution? Thanks in advance
Comments
It's simple, and may work for you.
You need a temporary register to calculate the difference of TimedOut and cnt first.
The difference is always correct, because both are 32bit registers, only the carry (borrow) is lost.
Then you can decide if cnt or TimedOut is higher by comparing to 0.
Andy
@LukeH: Yes, you are right, the cnt will also wrap, but there is still a gap, because duration wraps first and during this duration time, a small value of duration is compared to the high cnt value and this gives a wrong result. The way Andy's code solves this task works perfectly, as long as duration is less than 31 bit.
Have a great day. ErNa