Ok, so we started off with "switch statements are inefficient - what else could I use" 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> <<a href="mailto:richard-lists@imagecraft.com">
richard-lists@imagecraft.com</a>> 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'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 "rjmp address" 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>>Yes, the C compiler I use has a special skip statement.<br>>In its simplest form, it uses the fact that a value can<br>>be added to the program counter (works within any 256
<br>>code word boundary). Asm:<br>><br>> ; here register W is loaded with jump offset, 0..N<br>> ...<br>> addwf PCL,f ; add 0..N to PC ("skip") so that the<br>> goto label0 ; next instruction executed will be
<br>> goto label1 ; one of the gotos<br>> ...<br>> goto labelN<br>><br>>The AVR cannot do this but is possible to use the IJMP<br>>instruction to do something similar. When/if such a<br>>"non-standard" mechanism is included in the C compiler,
<br>>it will be possible to write very efficient code without<br>>using assembly language...<br>><br>>The idea is to avoid multiple comparisons when the<br>>"switch variable" is known to be a contigous range
<br>>of numbers.<br>><br>>AVR:<br>><br>>; The table<br>><br>>jumptable:<br>> rjmp label0<br>> rjmp label1<br>> ...<br>> rjmp labelN<br>><br>>; The jumping code:<br>><br>> ...
<br>> load Z with address of jumptable<br>> add "switch variable" to Z, (using carry for MSByte!)<br>> IJMP ; jump to selected line in the jump table<br>><br>>; The jump destinations:<br>>
<br>>label0:<br>> ...<br>>label1:<br>> ...<br>>labelN:<br>><br>><br>><br>>Bengt Ragnemalm skrev:<br>>>I could not find "skip" in my book so the only special about this code is
<br>>>the skip statement, all the rest with goto and labels is standard C. The<br>>>code will also not differ in any way from a switch with just the difference<br>>>that the testing of the skip will be done using char instead of int.
<br>>>But have this PIC C-compilator included this special skip statement just for<br>>>this purpose??? In my opinion it would have been much better to invent for<br>>>example something like switch_char(char) and using the same syntax as
<br>>>normal. I guess this is just a special usage of skip.<br>>>/Bengt<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><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