Forum: Compiler & IDEs Atmega 64, GCC optimiert zuviel weg!


von Martin Raffelsieper (Gast)


Lesenswert?

Hallo!

warum wird eigentlich die Zählvariable "loopcnt ", die die Routine 8
mal durchlaufen soll, einfach wegoptimiert?

Als Folge kommt eine Falsche CRC-Summe unten raus. wie kann man das
verhindern?



u16 CRC(unsigned int crc, unsigned char ch)
{
#define CRC_POLYNOME 0x8005

u08 loopcnt;
u08 carry;

   for(loopcnt=0;loopcnt<7;loopcnt++);
   {

      carry = ch >> 7;  // Zeichen links ins Carry schieben
      ch    = ch << 1;

      if(crc & BIT0)
      {
         crc = crc << 1;
         crc = crc | carry;
         crc = crc ^ CRC_POLYNOME;
      }
      else
      {
         crc = crc << 1;
         crc = crc | carry;
      }
   }

   return(crc);
}



so wirds compiliert:


   for(loopcnt=0;loopcnt<7;loopcnt++);
   {

      carry = ch >> 7;  // Zeichen links ins Carry schieben
 54a:  66 1f         adc  r22, r22
 54c:  66 27         eor  r22, r22
 54e:  66 1f         adc  r22, r22
      ch    = ch << 1;

      if(crc & BIT0)
 550:  80 ff         sbrs  r24, 0
 552:  0b c0         rjmp  .+22       ; 0x56a
      {
         crc = crc << 1;
 554:  22 0f         add  r18, r18
 556:  33 1f         adc  r19, r19
         crc = crc | carry;
 558:  86 2f         mov  r24, r22
 55a:  99 27         eor  r25, r25
 55c:  28 2b         or  r18, r24
 55e:  39 2b         or  r19, r25
         crc = crc ^ CRC_POLYNOME;
 560:  85 e0         ldi  r24, 0x05  ; 5
 562:  90 e8         ldi  r25, 0x80  ; 128
 564:  28 27         eor  r18, r24
 566:  39 27         eor  r19, r25
 568:  06 c0         rjmp  .+12       ; 0x576
      }
      else
      {
         crc = crc << 1;
 56a:  22 0f         add  r18, r18
 56c:  33 1f         adc  r19, r19
         crc = crc | carry;
 56e:  86 2f         mov  r24, r22
 570:  99 27         eor  r25, r25
 572:  28 2b         or  r18, r24
 574:  39 2b         or  r19, r25
      }
   }

   return(crc);
}
 576:  c9 01         movw  r24, r18
 578:  08 95         ret




Gruß Martin

von Peter D. (peda)


Lesenswert?

for(loopcnt=0;loopcnt<7;loopcnt++);

Das Semikolon !!!


Peter

von Martin Raffelsieper (Gast)


Lesenswert?

ach du sch......

werde ganz rot und nehme alles zurück!!!!!

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.