Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 23rd September 2005, 04:06 PM   #1
Default PIC Programming: Program Construct

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?
Attached Thumbnails
PIC Programming: Program Construct-logic_choice_.gif  
__________________
L.Chung
eblc1388 is offline  
Old 23rd September 2005, 05:09 PM   #2
Default

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
__________________
"I share, thus I am"
Jay.slovak
Read this!
ICD2 Clone
Best PIC/DsPIC Bootloader

Read my Inchworm ICD2 review!
Jay.slovak is offline  
Old 23rd September 2005, 05:21 PM   #3
Default

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.
__________________
Time is nature\'s way of keeping everything from happening at once.
http://membres.lycos.fr/jrainville/
Joel Rainville is offline  
Old 23rd September 2005, 05:25 PM   #4
Default

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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now  
Old 23rd September 2005, 05:44 PM   #5
Default

Hi Jay,

It turns out that a mixture of different type of tests yield the simplest codes.
Attached Thumbnails
PIC Programming: Program Construct-logic_choice_1.gif  
__________________
L.Chung
eblc1388 is offline  
Old 23rd September 2005, 05:54 PM   #6
Default

Quote:
Originally Posted by eblc1388
Hi Jay,

It turns out that a mixture of different type of tests yield the simplest codes.
I can see that you saved 2 jumps...
__________________
"I share, thus I am"
Jay.slovak
Read this!
ICD2 Clone
Best PIC/DsPIC Bootloader

Read my Inchworm ICD2 review!
Jay.slovak is offline  
Old 23rd September 2005, 05:56 PM   #7
Default

Quote:
Originally Posted by Joel Rainville
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.
__________________
L.Chung
eblc1388 is offline  
Old 23rd September 2005, 05:58 PM   #8
Default

Quote:
Originally Posted by Nigel Goodwin
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.
__________________
Time is nature\'s way of keeping everything from happening at once.
http://membres.lycos.fr/jrainville/
Joel Rainville is offline  
Old 23rd September 2005, 06:00 PM   #9
Default

Quote:
Originally Posted by Jay.slovak
Quote:
Originally Posted by eblc1388
Hi Jay,

It turns out that a mixture of different type of tests yield the simplest codes.
I can see that you saved 2 jumps...
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.

Edited: Clarify meaning in above sentence.
__________________
L.Chung
eblc1388 is offline  
Old 23rd September 2005, 06:09 PM   #10
Default

Quote:
Originally Posted by Nigel Goodwin
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...
__________________
Time is nature\'s way of keeping everything from happening at once.
http://membres.lycos.fr/jrainville/
Joel Rainville is offline  
Old 23rd September 2005, 06:25 PM   #11
Default

Quote:
Originally Posted by eblc1388
Quote:
Originally Posted by Joel Rainville
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.
Paper? What's that? :P

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"...
__________________
Time is nature\'s way of keeping everything from happening at once.
http://membres.lycos.fr/jrainville/
Joel Rainville is offline  
Old 23rd September 2005, 07:27 PM   #12
Default

Quote:
Originally Posted by Joel Rainville
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"...
No. You can use any instruction you like. But you are likely to need to do something at zero, not "skipping something" at zero.
__________________
L.Chung
eblc1388 is offline  
Old 23rd September 2005, 07:40 PM   #13
Default

Quote:
Originally Posted by eblc1388
Quote:
Originally Posted by Jay.slovak
Quote:
Originally Posted by eblc1388
Hi Jay,

It turns out that a mixture of different type of tests yield the simplest codes.
I can see that you saved 2 jumps...
It seems that it is always good to do nothing(except a GOTO) on one choice of a logical test and keep all codes to the other choice.
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 :

Code:
[...]
loop:
	movf	SEC, w
	btfss 	STATUS, Z ; <SEC == 0>?
	goto	decf_sec
	goto 	sec_zero
decf_sec:
	decf	SEC
	goto	loop
sec_zero:
[...]
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...
__________________
Time is nature\'s way of keeping everything from happening at once.
http://membres.lycos.fr/jrainville/
Joel Rainville is offline  
Old 23rd September 2005, 07:46 PM   #14
Default

I would very much like to see how you implement the coding using DECFSZ instruction. Yes, MIN and SEC all >0 upon entry.
__________________
L.Chung
eblc1388 is offline  
Old 23rd September 2005, 07:47 PM   #15
Default

Quote:
Originally Posted by eblc1388
No. You can use any instruction you like.
Aw, crap. :lol:
__________________
Time is nature\'s way of keeping everything from happening at once.
http://membres.lycos.fr/jrainville/
Joel Rainville is offline  
Reply

Tags
construct, pic, program, programming

Thread Tools
Display Modes




All times are GMT. The time now is 03:14 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker