piclist 2001\03\01\120858a >
Thread: Switch / If Statement revisited
www.piclist.com/techref/microchip/devices.htm?key=pic
flavicon
face BY : Mike Mansheim email (remove spam text)



> Hey, would somebody with HI-TECH C try this and see how efficient it is:

> Fred = RB0 ? 20 : 10;

> In fact, a good compiler should be able to generate these sequences from
the
> original C:

> if (RB0)
>    Fred = 20;
> else
>    Fred =10;

I use CCS, not HI-TECH, but here's the results if anyone is interested.
Not
too bad, in my opinion.  It helps with the CCS compiler to declare RB0 as a
"short" - what CCS calls a 1 bit variable.  Another note: if Fred is
declared
at the end of a long list of variables, it could end up in another bank.
Then
all of these results are longer because of the bank switching.

void main(void)
{
    #byte PORTB = 0x06
    #bit RB0 = PORTB.0

    unsigned int Fred;

    // first technique
    Fred = RB0 ? 20 : 10;

    // second technique
    if (RB0)
       Fred = 20;
    else
       Fred =10;

    // space saver originally suggested
    Fred = 10;
    if (RB0) Fred = 20;
}

to save space, I've cleaned up the CCS .lst file output a bit for the
results:  Note:  the compiler assigned Fred to 0x21.

first technique (6 words):

     ....................  Fred = RB0 ? 20 : 10;
0007  BTFSS  06,0
0008  GOTO   00B
0009  MOVLW  14
000A  GOTO   00C
000B  MOVLW  0A
000C  MOVWF  21

second technique (7 words):

     ....................    if (RB0)
000D  BTFSS  06,0
000E  GOTO   012
     ....................       Fred = 20;
000F  MOVLW  14
0010  MOVWF  21
     ....................    else
0011  GOTO   014
     ....................       Fred =10;
0012  MOVLW  0A
0013  MOVWF  21


space saving suggestion (6 words):

     ....................    Fred = 10;
0014  MOVLW  0A
0015  MOVWF  21
     ....................    if (RB0) Fred = 20;
0016  BTFSS  06,0
0017  GOTO   01A
0018  MOVLW  14
0019  MOVWF  21

ideal result (4 words):
>   movlw  10
>   btfsc  RBO
>   movlw 20
>   movwf  Fred

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


<OFE7445741.6D829BA7-ON86256A02.005B676C@graco.com>

See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Switch / If Statement revisited

month overview.

new search...