Hi!

The while loop is handled in an inefficient way. Lets have an example:
/*********************************************************************************/
#include <stdlib.h>
#include <msp430x14x.h>

int copy_i(int in);

int main (void) {
    unsigned int i;
    /**/
    i = copy_i(1);    /*used to prevent compiler optimisation*/
    P1OUT = 0;
    while (i<0x8000) {
        P1OUT++;
        i <<= 1;
    }
    return CPUOFF;
}

int copy_i(int in) {
    return in;
}
/*********************************************************************************/

Objdump outputs the following (fragment):
/*********************************************************************************/
    i = copy_i(1);    /*used to prevent compiler optimisation*/
    1144:    1f 43           mov    #1,    r15    ;r3 As==01
    1146:    b0 12 68 11     call    #4456        ;#0x1168
    P1OUT = 0;
    114a:    c2 43 21 00     mov.b    #0,    &0x0021    ;r3 As==00
    while (i<0x8000) {
    114e:    3f 90 00 80     cmp    #-32768,r15    ;#0x8000
    1152:    06 2c           jc    $+14         ;abs 0x1160
        P1OUT++;
    1154:    d2 53 21 00     inc.b    &0x0021        ;
        i <<= 1;
    1158:    0f 5f           rla    r15        ;
    115a:    3f 90 00 80     cmp    #-32768,r15    ;#0x8000
    115e:    fa 2b           jnc    $-10         ;abs 0x1154
    }
/*********************************************************************************/

As can be seen, at address 0x114e the condition is tested as well as at 0x115a. Its not nessecary to test this twice.
Two optimal solutions exist:
1) Test at the end of the loop: When entering the while loop, JMP to the
   test.

   JMP while_condition
   while_body_start:   <while_body>
   while_condition:    <test while_condition>
                       <conditionally jump to while_body_start>

2) Test at the beginning of the loop: When hitting the end of the loop,
   JMP to the test.

   while_condition:    <test while_condition>
                       <conditionally jump to while_end>
   <while_body>
   JMP while_condition:
   while_end:


I would suggest to think about the 1st solution, because a do-while loop would then be equal to a while loop without the initial JMP.

Ralf

Reply via email to