Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Programming: Program Construct

Status
Not open for further replies.

eblc1388

Active Member
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?
 

Attachments

  • logic_choice_.gif
    logic_choice_.gif
    20.7 KB · Views: 1,098
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 :D
 
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.
 
Hi Jay,

It turns out that a mixture of different type of tests yield the simplest codes.
 

Attachments

  • logic_choice_1.gif
    logic_choice_1.gif
    10.2 KB · Views: 1,037
Joel Rainville said:
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.
 
Nigel Goodwin said:
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.
 
Jay.slovak said:
eblc1388 said:
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.
 
Nigel Goodwin said:
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...
 
eblc1388 said:
Joel Rainville said:
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"...
 
Joel Rainville said:
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.
 
eblc1388 said:
Jay.slovak said:
eblc1388 said:
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...
 
I would very much like to see how you implement the coding using DECFSZ instruction. Yes, MIN and SEC all >0 upon entry.
 
eblc1388 said:
I would very much like to see how you implement the coding using DECFSZ instruction. Yes, MIN and SEC all >0 upon entry.

Here :

Code:
[...]
	movf	SEC, w
	btfsc	STATUS, Z
	goto 	sec_zero
loop_sec:
	decfsz	SEC, f
	goto	loop_sec
sec_zero:	
[...]

That version would be faster if I am not mistaken. You could say there's a hidden "goto sec_zero" in the decfsz instruction...
 
In fact, here's the whole routine to make it clearer :

Code:
Timer 		; SEC or MIN > 0

	movf	SEC, w
	btfsc	STATUS, Z
	goto 	sec_zero
loop_sec:
	decfsz	SEC, f
	goto	loop_sec
sec_zero:	
	movf	MIN, w
	btfsc 	STATUS, Z ; <MIN != 0>?
	return
	decf	MIN, f
	movlw	D'59'
	movwf	SEC
	goto 	loop_sec
 
Joel Rainville said:
That version would be faster if I am not mistaken. You could say there's a hidden "goto sec_zero" in the decfsz instruction...

Of course it is faster, because you keep decreasing the SEC variable in a loop until it is zero!!! :twisted:

I thought it is clear in the context, by using the SEC and MIN variable name, that the code would be executed once every second.
 
eblc1388 said:
Joel Rainville said:
That version would be faster if I am not mistaken. You could say there's a hidden "goto sec_zero" in the decfsz instruction...

Of course it is faster, because you keep decreasing the SEC variable in a loop until it is zero!!! :twisted:

I thought it is clear in the context, by using the SEC and MIN variable name, that the code would be executed once every second.

Then just replace "goto loop_sec" by a "return". It doesn't change anything. Or am I missing something?
 
Joel Rainville said:
eblc1388 said:
Joel Rainville said:
That version would be faster if I am not mistaken. You could say there's a hidden "goto sec_zero" in the decfsz instruction...

Of course it is faster, because you keep decreasing the SEC variable in a loop until it is zero!!! :twisted:

I thought it is clear in the context, by using the SEC and MIN variable name, that the code would be executed once every second.

Then just replace "goto loop_sec" by a "return". It doesn't change anything. Or am I missing something?

Ok, now that it's clear what the algorithm is used for :oops::lol:, my code isn't any faster than Nigel's suggestion of decrementing then testing.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top