The BTFSC and BTFSS instructions offer choices on testing variables.
The logic of the two flowcharts are equivalent. Variables "SEC" and "MIN" are two 8-bit variable to be decremented to zero.
In the context of PIC programming, which one would you choose and why? Is there a rule of thumb or just plain trial and error to see which uses less codes?
Hmm, few moments ago I was actually working on this too :lol:
They are both 100% equal, (I prefer to use SEC=0 condition) so don't waste your time and just do Rock Paper Scissors to choose one
To me, this is just a question of code readability. Depending on your algorithm, one might make more sense than the other. But since the logic is identical, the machine code should be equivalent, and there should be no efficiency advantage for one over the other.
I would recommend using neither of the two suggestions!, don't check for zero before doing anything, decrement the counter FIRST, then check if bit 7 of the counter is high (that is, the counter has decremented below zero). If it has, reset the counter and decrement the next counter in the series.
To me, this is just a question of code readability. Depending on your algorithm, one might make more sense than the other. But since the logic is identical, the machine code should be equivalent, and there should be no efficiency advantage for one over the other.
Try the (now 4, including Nigel's) suggested methods on paper and you will notice some difference in using BTFSS and BTFSC instructions. Some combinations might require more instructions.
I would recommend using neither of the two suggestions!, don't check for zero before doing anything, decrement the counter FIRST, then check if bit 7 of the counter is high (that is, the counter has decremented below zero). If it has, reset the counter and decrement the next counter in the series.
This depends entirely on how the code is used/called by the controlling program. It's up to the programmer to choose if he wants to check for 0 first...
In C, you do that by choosing a do-while loop instead of a while-do, which in some cases one makes no sense at all. One is not necessarily a substitute for the other.
It seems that it is always good to try to code only a single instruction on one of the two outcomes of a logical test and keep codes(more than one instruction) to the other outcome.
I would recommend using neither of the two suggestions!, don't check for zero before doing anything, decrement the counter FIRST, then check if bit 7 of the counter is high (that is, the counter has decremented below zero). If it has, reset the counter and decrement the next counter in the series.
In fact, assuming SEC and MIN are never both 0 when first entering the loop, you are right. But if both are 0, you've wasted time decrementing and testing instead of just testing for 0...
To me, this is just a question of code readability. Depending on your algorithm, one might make more sense than the other. But since the logic is identical, the machine code should be equivalent, and there should be no efficiency advantage for one over the other.
Try the (now 4, including Nigel's) suggested methods on paper and you will notice some difference in using BTFSS and BTFSC instructions. Some combinations might require more instructions.
Should I limit myself to just BTFSS and BTFSC for tests? Because it would seem that DECFSZ would help here, and I guess that's why Nigel suggested decrementing first, to take advantage of DECFSZ "power"...
Should I limit myself to just BTFSS and BTFSC for tests? Because it would seem that DECFSZ would help here, and I guess that's why Nigel suggested decrementing first, to take advantage of DECFSZ "power"...
Ah, I get it now. Of course, if you are forced to use one over the other (and not use DECFSZ), you are gonna end up with situations where 2 gotos follow a bit test, like this :
which you want to avoid. What you want is skip over a goto and branch to "non-goto" code, like this :
Code:
[...]
loop:
movf SEC, w
btfsc STATUS, Z ; <SEC != 0>?
goto sec_zero
decf SEC, f
goto loop
sec_zero:
[...]
That's what the question was about?, and that's what everyone's saying, right? I need to see code to understand :lol:
I guess I was looking at the diagrams from a high level language's point of view...
And of course, if you can assume that upon entry, SEC *or* MIN is necesseraly > 0, then the previous code would be better written using DECFSZ as Nigel suggested, but I assumed the requirement was to use BTFSC or BTFSS only...