cancel
Showing results for 
Search instead for 
Did you mean: 

[MDK5] Strange optimization

stanzanim
Associate III
Posted on November 24, 2015 at 17:01

Hi there

I am struggling with a strange behaviour of Keil MDK (MDK5 to be precise)

   161:                     startAttemptsLeft --; 

 

0x08001B12 1E7F      SUBS     r7,r7,#1

 

0x08001B14 B2FF      UXTB     r7,r7

 

   162:                     shaftHaltedCnt = 0; 

 

   163:                     setPointIndex = 0;          

 

   164:                     setPointPeriod = setPointLUT[setPointIndex]; 

 

   165:                     delayBeforeShot = delayBeforeShot - (delayBeforeShot>>4); 

 

   166:         measuredPeriod = setPointPeriod ; 

 

   167:         hallToggleCnt  = 0; 

 

   168:                                 hallToggleCntRestartAttempts = 0; 

 

0x08001B16 2400      MOVS     r4,#0x00

 

This is a copy-paste from debugger disassembly window. It looks like C lines from 162 to 166 has been stripped out by the compiler, i.e. no assembly generated. This make no sense since the variables are used in the code afterwards. I tried to change the optimization level w/o success.

I am using Nucleo64 with STM32L0 MCU and MDK 5

I can post the full code but I do not want to begin with this.

Thanks to who'll help on this black magic

2 REPLIES 2
Posted on November 25, 2015 at 01:03

There is typically a one-to-many association between C and assembler lines. The compiler can rearrange the output code, and split it up. Variable initialization is likely folded into the stack frame creation in the function's prologue code, or deferred to immediately before use if a register is being used, or the scope can be narrowed.

The line number info is too course to handle all this muddling up of the instructions, and the linear flow of the processor dictates what you actually get to see and step through.

If it makes you dizzy turn off optimization, or step through the assembler and not the C.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stanzanim
Associate III
Posted on November 25, 2015 at 10:32

Thanks clive

I check the disassembled and disabled mixed mod  and the generated assembly looks correct

Disabling optimization (O0) in the compler option does not help :(

I then did a simple check. The program behaves this vay

main()

{

  int a,b;

  ....

  while(1)

  {

     int c,d;

    switch(b)

      case 0:

         a=0;

         c=0;

         d=0;

         break;

      case 1:

      default;

         c++;

         d++;

         a=1;

         break;

   }

  } /// end while

b and c are declared (and assigned, modified) in the body of the while (since they are not used elsewhere), a and b outside: c and d are optimed somehow (I cannot see them in the debug disassenbly windows.

Doing this way

main()

{

  int a,b,c,d;

  ....

  while(1)

  {

    switch(b)

      case 0:

         a=0;

         c=0;

         d=0;

         break;

      case 1:

      default;

         c++;

         d++;

         a=1;

         break;

   }

  } /// end while

things works fine (i.e. can see in line assembly) . Also doing this other way (commenting out while(1) things works

main()

{

  int a,b;

  ....

  //while(1)

  {

     int c,d;

    switch(b)

      case 0:

         a=0;

         c=0;

         d=0;

         break;

      case 1:

      default;

         c++;

         d++;

         a=1;

         break;

   }

  } /// end while

It looks like the while(1) statement has some influence ...