RobertJoseph RobertJoseph - 1 month ago 8
C Question

Compile c source with 'asm' directive

I'm trying to compile this c source file using cl.exe (from Visual Studio). The specific code in question that fails to compile is:

#include <stdio.h>

static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
/* ecx is often an input as well as an output. */
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));

I'm seeing this failure:

C:\>cl sgx.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24213.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

sgx.c(7): error C2065: 'asm': undeclared identifier
sgx.c(7): error C2143: syntax error: missing ';' before 'volatile'


Instead of direct assembly, you can use compiler intrinsics. Microsoft compilers support __cpuid() documented here:

GCC instead supports __get_cpuid() via cpuid.h as described by this answer (