The difference between asm, asm volatile and clobbering memory

See the “Extended Asm” page in the GCC documentation. You can prevent an asm instruction from being deleted by writing the keyword volatile after the asm. […] The volatile keyword indicates that the instruction has important side-effects. GCC will not delete a volatile asm if it is reachable. and An asm instruction without any output … Read more

Referencing memory operands in .intel_syntax GNU C inline assembly

Compile with gcc -masm=intel and don’t try to switch modes inside the asm template string. AFAIK there’s no equivalent before clang14 (Note: MacOS installs clang as gcc / g++ by default.) Also, of course you need to use valid GNU C inline asm, using operands to tell the compiler which C objects you want to … Read more

Why can’t local variable be used in GNU C basic inline asm statements?

You can use local variables in extended assembly, but you need to tell the extended assembly construct about them. Consider: #include <stdio.h> int main (void) { int data1 = 10; int data2 = 20; int result; __asm__( ” movl %[mydata1], %[myresult]\n” ” imull %[mydata2], %[myresult]\n” : [myresult] “=&r” (result) : [mydata1] “r” (data1), [mydata2] “r” … Read more

In GNU C inline asm, what are the size-override modifiers for xmm/ymm/zmm for a single operand?

From the file gcc/config/i386/i386.c of the GCC sources: b — print the QImode name of the register for the indicated operand. %b0 would print %al if operands[0] is reg 0. w — likewise, print the HImode name of the register. k — likewise, print the SImode name of the register. q — likewise, print the … Read more

How to access C variable for inline assembly manipulation?

In GNU C inline asm, with x86 AT&T syntax: (But https://gcc.gnu.org/wiki/DontUseInlineAsm if you can avoid it). // this example doesn’t really need volatile: the result is the same every time asm volatile(“movl $0, %[some]” : [some] “=r” (x) ); after this, x contains 0. Note that you should generally avoid mov as the first or … Read more

x86/x64 CPUID in C#

I’m fairly certain you’re being blocked by DEP. The x_CPUIDy_INSNS byte arrays are in a segment of memory marked as data and non-executable. EDIT: That being said, I’ve gotten a version that compiles and runs, but I don’t think gets the right values. Perhaps this will get you along your way. EDIT 2: I think … Read more

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