If i use global register variables that are used and updated inside an interrupt the optimizer produces broken code. Code example: register volatile unsigned char timer0_h8 asm("r3"); volatile unsigned char timer0_h8_buf = 0; void foo(void) { timer0_h8_buf = timer0_h8; return; } ISR(TIMER0_OVF_vect) { timer0_h8++; } produces: 2432 0b5a 1092 0000 sts timer0_h8_buf,__zero_reg__ ; timer0_h8_buf, instead of this when no optimization is used: 3248 104a 832D mov r24,r3 ; timer0_h8.47, timer0_h8 3249 104c 8093 0000 sts timer0_h8_buf,r24 ; timer0_h8_buf, timer0_h8.47
Unfortunately, we don't see the exact command line. I don't see this for 4.5.0, tried -O, -O2, -Os, -O3. All gives foo: /* prologue: function */ /* frame size = 0 */ /* stack size = 0 */ .L__stack_usage = 0 sts timer0_h8_buf,r3 /* epilogue start */ ret Note that volatile on register variables does not work as expected. volatile is meaningful for memory locations only. As the addresses in your snipp indicate there is much more code in the module, so presumably we don't see the proper source.
Created attachment 24828 [details] C test case
Created attachment 24829 [details] Test case from attachment 24828 [details] as compiled with avr-gcc 4.4.2 For compiler options, see the attachment: -Os -mmcu=atmega8.
Closed as WORKSFORME. avr-gcc 4.4.2 compiles the test case correctly.