I developed this answer after linking to the dmckee answer , but it takes a different approach than his / her answer.
Attribute documentation from GCC:
noinline This function attribute does not allow the function to be considered for embedding. If a function has no side effects, there are optimizations other than nesting that force optimization of function calls, although the function call is active. To avoid repeating such calls, put asm ("");
This gave me an interesting idea ... Instead of adding the nop instruction to the inner loop, I tried adding empty assembly code there, for example:
unsigned char i, j; j = 0; while(--j) { i = 0; while(--i) asm(""); }
And it worked! This loop has not been optimized, and no additional nop instructions have been added.
What else, if you use volatile , gcc will store these variables in RAM and add a bunch of ldd and std to copy them to temporary registers. On the other hand, this approach does not use volatile and does not create such overhead.
Update. If you compile code using -ansi or -std , you must replace the asm keyword with __asm__ , as described in the GCC Documentation .
Alternatively, you can also use __asm__ __volatile__("") if your statement
Denilson Sá Maia Aug 16 '11 at 19:55 2011-08-16 19:55
source share