Testing for transition 255 to 0

Status
Not open for further replies.

atferrari

Well-Known Member
Most Helpful Member
Micro: 18F Microchip family.

Given a common register, to detect the transition from 0 to 255 (DECF), what can I test? Is it any trick other than looking for the value 255 in the byte?
 
Going from 255 to 0 sets C but the opposite has not a single flag set, only for the case. At least, simulation shows that.
 
The DECF instruction on a reg with a value of zero to 255 sets the Negative flag (Status reg bit 4)
 
Wouldn't it also clear the Zero flag bit? Yep, the DECF instructions affects a whole bunch of STATUS bits. Check out the instruction set summary.

Mike
 
Last edited:
Mike said:
Wouldn't it also clear the Zero flag bit?

Yes, but you would have to check that it was set BEFORE you did the DECF, because it would be clear in all cases except one.
 
I wonder if the OP is trying to avoid decrementing a register if its value is 0? If that's the case, most of us recommend testing for that condition before performing the decrement operation. Isn't that right guys?

Mike
 
Mike said:
I wonder if the OP is trying to avoid decrementing a register if its value is 0? If that's the case, most of us recommend testing for that condition before performing the decrement operation. Isn't that right guys?

If that was the case, then yes!.
 
Mike said:
I wonder if the OP is trying to avoid decrementing a register if its value is 0? If that's the case, most of us recommend testing for that condition before performing the decrement operation. Isn't that right guys?

Mike

Yes, For that situation I have used:
tstfsz reg ; skip if the reg has already reached zero
decf reg,f

If he wants to test if a reg has reached 255, then he could use:
DECF reg,f
BN destination ; Branch if negative
 
How about
decf file,F
incfsnz file,W ; check if file was zero before the decrement
goto WasZero

Or OV=1 and N=1 gives the same thing.

Checking Z=0 will give all numbers except zero.
Checking N=1 will give all numbers from 0x80 to 0xff.
Checking just OV will give 255 and 127.

Someone with more knowledge about 18 code, does the Carry flag act as a borrow and is therefore reset only if there is a transition from 0 to 255? This would seem likely to me.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…