Either use __asm__ instead of asm with -std=c99 , or use -std=gnu99
From the GCC docs https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Extended-Asm.html.
The asm keyword is a GNU extension. When writing code that can be compiled with -ansi and the various -std options, use __asm__ instead of asm (see Alternative keywords).
and https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Alternate-Keywords.html#Alternate-Keywords :
-ansi and the various -std options disable certain keywords. This causes problems when you want to use the GNU C extensions or the general-purpose header file that should be used by all programs, including the ISO C programs. The keywords asm, typeof and inline are not available in programs compiled with -ansi or -std (although inline may used in a program compiled with -std = c99 or -std = c11). The ISO C99 keyword restriction is only available if -std = gnu99 (which will eventually be used by default) or -std = c99 (or equivalent -std = iso9899: 1999) or an option for a later standard version, is used.
The way to solve these problems is to put __ at the beginning and at the end of each problematic keyword. For example, use __asm__ instead of asm and __inline__ instead of inline.
Other C compilers do not accept these alternative keywords; if you want to compile with another compiler, you can define alternative keywords as macros to replace them with regular keywords. It looks like this:
#ifndef __GNUC__ #define __asm__ asm #endif
-pedantic and other options trigger warnings for many GNU C extensions. You can prevent such warnings within a single expression by writing before the __extension__ expression. __extension__ has no influence other than this.
-std=gnu99 includes GNU extensions such as asm , but retains the C99 language.
Standard C99
GCC works to meet the C99 standard. From the draft standard C99 N1256 7.1.3 "Reserved identifiers" 1:
Each heading announces or defines all the identifiers listed in the associated subclause, and, optionally, declares or defines the identifiers listed in the corresponding subclause of the future directions of the library, and identifiers that are always reserved either for any use or for use as area identifiers file.
- All identifiers that begin with an underscore, as well as a capital letter or other underscore, are always reserved for any use.
Otherwise, a legal program like:
int asm = 0;
will become illegal.
Test program
#include <assert.h> #include <stdint.h> int main(void) { uint32_t io = 0; __asm__ volatile ( "movl %0, %%eax;" "inc %%eax;" "movl %%eax, %0;" : "+m" (io) : : "%eax" ); assert(io == 1); }
Tested on Ubuntu 17.10, GCC 7.2.