Ok, so we started off with &quot;switch statements are inefficient - what else could I use&quot; and have ended up with Richard giving us a current and future solution that keeps SWITCH. <br><br>So if SWITCH is ok, you should know about Protothreads which manipulate code threading using the switch statement and avoids layers of enumerated states.
<br><br><a href="http://www.sics.se/~adam/pt/">http://www.sics.se/~adam/pt/</a><br><br>Very cool, very elegant, very useful.<br><br><div><span class="gmail_quote">On 4/13/07, <b class="gmail_sendername">Richard</b> &lt;<a href="mailto:richard-lists@imagecraft.com">
richard-lists@imagecraft.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">The ICC compilers DO use jump table, but only case 12 or so more, due to
<br>the overhead of fetching from the program memory.<br><br>Actually, Jodan&#39;s post made me realize that we can generate shorter code<br>sequence for the jump table. Currently, the jump table stores only the<br>program addresses and not &quot;rjmp address&quot; Changing it will reduce the
<br>tradeoff point. Unfortunately, this will screw up the code compressor so<br>the changes must be done with care.<br><br>The 8 bit optimization will generate byte comparison for 8 bit switch<br>variable if the case values fit.
<br><br>At 03:55 AM 4/13/2007, Johan H. Bodin wrote:<br>&gt;Yes, the C compiler I use has a special skip statement.<br>&gt;In its simplest form, it uses the fact that a value can<br>&gt;be added to the program counter (works within any 256
<br>&gt;code word boundary). Asm:<br>&gt;<br>&gt;&nbsp;&nbsp; ; here register W is loaded with jump offset, 0..N<br>&gt;&nbsp;&nbsp; ...<br>&gt;&nbsp;&nbsp; addwf&nbsp;&nbsp;PCL,f&nbsp;&nbsp;&nbsp;&nbsp;; add 0..N to PC (&quot;skip&quot;) so that the<br>&gt;&nbsp;&nbsp; goto&nbsp;&nbsp; label0&nbsp;&nbsp; ; next instruction executed will be
<br>&gt;&nbsp;&nbsp; goto&nbsp;&nbsp; label1&nbsp;&nbsp; ; one of the gotos<br>&gt;&nbsp;&nbsp; ...<br>&gt;&nbsp;&nbsp; goto&nbsp;&nbsp; labelN<br>&gt;<br>&gt;The AVR cannot do this but is possible to use the IJMP<br>&gt;instruction to do something similar. When/if such a<br>&gt;&quot;non-standard&quot; mechanism is included in the C compiler,
<br>&gt;it will be possible to write very efficient code without<br>&gt;using assembly language...<br>&gt;<br>&gt;The idea is to avoid multiple comparisons when the<br>&gt;&quot;switch variable&quot; is known to be a contigous range
<br>&gt;of numbers.<br>&gt;<br>&gt;AVR:<br>&gt;<br>&gt;; The table<br>&gt;<br>&gt;jumptable:<br>&gt;&nbsp;&nbsp; rjmp label0<br>&gt;&nbsp;&nbsp; rjmp label1<br>&gt;&nbsp;&nbsp; ...<br>&gt;&nbsp;&nbsp; rjmp labelN<br>&gt;<br>&gt;; The jumping code:<br>&gt;<br>&gt;&nbsp;&nbsp; ...
<br>&gt;&nbsp;&nbsp; load Z with address of jumptable<br>&gt;&nbsp;&nbsp; add &quot;switch variable&quot; to Z, (using carry for MSByte!)<br>&gt;&nbsp;&nbsp; IJMP&nbsp;&nbsp;; jump to selected line in the jump table<br>&gt;<br>&gt;; The jump destinations:<br>&gt;
<br>&gt;label0:<br>&gt;&nbsp;&nbsp; ...<br>&gt;label1:<br>&gt;&nbsp;&nbsp; ...<br>&gt;labelN:<br>&gt;<br>&gt;<br>&gt;<br>&gt;Bengt Ragnemalm skrev:<br>&gt;&gt;I could not find &quot;skip&quot; in my book so the only special about this code is
<br>&gt;&gt;the skip statement, all the rest with goto and labels is standard C. The<br>&gt;&gt;code will also not differ in any way from a switch with just the difference<br>&gt;&gt;that the testing of the skip will be done using char instead of int.
<br>&gt;&gt;But have this PIC C-compilator included this special skip statement just for<br>&gt;&gt;this purpose??? In my opinion it would have been much better to invent for<br>&gt;&gt;example something like switch_char(char) and using the same syntax as
<br>&gt;&gt;normal. I guess this is just a special usage of skip.<br>&gt;&gt;/Bengt<br>&gt;<br>&gt;_______________________________________________<br>&gt;Icc-avr mailing list<br>&gt;<a href="mailto:Icc-avr@imagecraft.com">
Icc-avr@imagecraft.com</a><br>&gt;<a href="http://dragonsgate.net/mailman/listinfo/icc-avr">http://dragonsgate.net/mailman/listinfo/icc-avr</a><br><br>// richard (This email is for mailing lists. To reach me directly, please
<br>use richard at <a href="http://imagecraft.com">imagecraft.com</a>)<br><br>_______________________________________________<br>Icc-avr mailing list<br><a href="mailto:Icc-avr@imagecraft.com">Icc-avr@imagecraft.com</a><br>
<a href="http://dragonsgate.net/mailman/listinfo/icc-avr">http://dragonsgate.net/mailman/listinfo/icc-avr</a><br></blockquote></div><br><br clear="all"><br>-- <br>John C. Edwards