How to Prevent Compiler from Rearranging/Optimizing Inline Assembly Code

Hi all,

I have a piece of code that has some commands in inline assembly and I would like to be 100% sure that the compiler does not mess with the code as I need it to run in exactly that order.

So far, I have tried formatting each line as

__asm__ volatile("...code...\n\t":::"memory");

and I have tried to change the compiler optimization settings to -O0 and -Og.

However, for some reason, it seems that the compiler is still "optimizing"/rearranging the code.

Any ideas of what this might be or suggestions for how to remedy this would be greatly appreciated.

(PS. I understand that the compiler optimizations are useful for making code more efficient and faster, but that is not something I am concerned with in this case. The timing of this code is absolutely crucial and so it is important that the code remains unaltered.)

Thank you all for your help

The compiler can't rearrange assembly. It doesn't even know how to read assembly. It just passes that straight through to the output. It may rearrange the C code around the assembly or the storage of the variables but your code should be written to account for that.

Without seeing the code, your specific problem cannot be solved.