When compiling the small program below with "gcc -O -o test test.c" or similar, the program created will run fine. When using "gcc -O -funroll-loops -o test test.c" however, it crashes. When examining the assembly output, it seems the algorithm used by the compiler to unroll the loop is something like:
A=0; V=0;
SRAM[V++]=A++;
do { SRAM[V]=A; SRAM[V+1]=A+1; SRAM[V+2]=A+2; SRAM[V+3]=A+3;
A+=4; V+=4; } while (V);
Which will cause V never to reach 0. You get similar results when using integers instead of bytes
Program listing follows:
#include <stdio.h>
typedef unsigned char byte;
typedef unsigned short word;
byte SRAM[256];
int main (void)
{
byte V;
word A;
A=0;V=0;
do SRAM[V++]=A++; while (V);
printf ("Done\n");
}