Reading a register value into a C variable [duplicate]

Editor’s note: this way of using a local register-asm variable is now documented by GCC as “not supported”. It still usually happens to work on GCC, but breaks with clang. (This wording in the documentation was added after this answer was posted, I think.) The global fixed-register variable version has a large performance cost for … Read more

What is the difference between ‘asm’, ‘__asm’ and ‘__asm__’?

There’s a massive difference between MSVC inline asm and GNU C inline asm. GCC syntax is designed for optimal output without wasted instructions, for wrapping a single instruction or something. MSVC syntax is designed to be fairly simple, but AFAICT it’s impossible to use without the latency and extra instructions of a round trip through … Read more

What does __asm__ __volatile__ do in C?

The __volatile__ modifier on an __asm__ block forces the compiler’s optimizer to execute the code as-is. Without it, the optimizer may think it can be either removed outright, or lifted out of a loop and cached. This is useful for the rdtsc instruction like so: __asm__ __volatile__(“rdtsc”: “=a” (a), “=d” (d) ) This takes no … Read more

Why does adding inline assembly comments cause such radical change in GCC’s generated code?

The interactions with optimisations are explained about halfway down the “Assembler Instructions with C Expression Operands” page in the documentation. GCC doesn’t try to understand any of the actual assembly inside the asm; the only thing it knows about the content is what you (optionally) tell it in the output and input operand specification and … Read more