I just wanted to add that when using C/C++, using volatile should be enough to instruct the compiler to not optimize away any reads/writes to the variable nor cache it in the register. It also should not be moved around (at least according to the
VC++ compiler documentation). IIRC, things are different when using inline assembly (at least in GCC):
Code: Select all
// Mostly plug from the wiki.
__asm__("cli" : : : ); // GCC may optimize away or move around.
__asm__ __volatile__("cli": : : ); // GCC may not optimize away, but may still move around.
__asm__ __volatile__("cli": : : "memory"); // GCC may not optimize away and may not move around.
To make things even more complicated, according to some docs,
__asm__ __volatile__ should be enough to instruct the compiler to also not move the instruction around. Take a look
at the wiki and also the forum thread linked at the bottom.
If this is indeed the case and the instruction may still be moved when specifying __volatile__ and not specifying "memory" in the clobber list, forgetting to add this could lead to serious issues during optimization (particularly when CLI and STI instructions are used e.g. to prevent context switches and where their order is very important).
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.