Sometimes I need some code that will be executed by the processor in the same way as I put it in the source code. But any C compiler has optimization algorithms, so I can expect some tricks. For example:
unsigned char flag=0; interrupt ADC_ISR(){ ADC_result = ADCH; flag = 1; } void main(){ while(!flag); echo ADC_result; }
Some compilers will definitely make the while(!flag); infinitive while(!flag); since it is suppose flag is false ( !flag therefore always true).
Sometimes I can use the volatile keyword. And sometimes it can help. But actually in my case (AVR GCC) the volatile keyword causes the compiler to find the variable in SRAM instead of registers (which is bad for some reason). In addition, many articles on the Internet suggest using the volatile keyword with great care, as the result may become unstable (depending on the compiler, its optimization parameters, platform, etc.).
Therefore, I would definitely prefer to somehow indicate the instruction on the source code and tell the compiler that this code should be compiled exactly as it is. For example: volatile while(!flag);
Is there any standard C instruction for this?
c compiler-optimization gcc
Vlada katlinskaya
source share