I think the CPUID is actually clobbers EAX, EBX, ECX, EDX, so it is probably just a registration problem. The following code works fine with gcc 4.0.1 and 4.2.1 on Mac OS X:
#include <stdio.h> int CPUID_getL1CacheSize() { int l1CacheSize = -1; asm ( "mov $5, %%eax\n\t" // EAX=80000005h: L1 Cache and TLB Identifiers "cpuid\n\t" "mov %%eax, %0" // eax into l1CacheSize : "=r"(l1CacheSize) // output : // no input : "%eax", "%ebx", "%ecx", "%edx" // clobbered registers ); return l1CacheSize; } int main(void) { printf("CPUID_getL1CacheSize = %d\n", CPUID_getL1CacheSize()); return 0; }
Note that you need to compile with -fno-pic , since EBX is reserved when PIC is enabled. (Either this, or you need to take steps to save and restore EBX).
$ gcc-4.0 -Wall -fno-pic cpuid2.c -o cpuid2 $ ./cpuid2 CPUID_getL1CacheSize = 64 $ gcc-4.2 -Wall -fno-pic cpuid2.c -o cpuid2 $ ./cpuid2 CPUID_getL1CacheSize = 64 $
source share