Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is inline asm part of the ANSI C standard?

I always thought it was but many IDEs and syntax highlighting tools do not highlight ASM in C, but they always do with C++. Is inline assembly part of the C standard (ANSII or ISO) or not?

like image 464
Sloan Fitzgerald Avatar asked Dec 13 '12 23:12

Sloan Fitzgerald


2 Answers

It's not in the ISO C standard (n1570 draft of C2011) as such, but mentioned in annex J (common extensions):

J.5.10 The asm keyword

1 The asm keyword may be used to insert assembly language directly into the translator output (6.8). The most common implementation is via a statement of the form:

asm ( character-string-literal );

Annex J is informative, not normative, so an implementation need not provide inline assembly, and if it does it's not prescribed in which form. But it's a widespread extension, though not portable since compilers do indeed implement it differently.

In the C++ standard (n3376 draft of the C++11 standard), it is mentioned in the body of the standard

7.4 The asm declaration [dcl.asm]

1 An asm declaration has the form

asm-definition:

asm ( string-literal ) ;

The asm declaration is conditionally-supported; its meaning is implementation-defined. [ Note: Typically it is used to pass information through the implementation to an assembler. — end note ]

but also not mandatory, and with implementation-defined interpretation.

like image 88
Daniel Fischer Avatar answered Sep 22 '22 01:09

Daniel Fischer


Contrary to popular belief, asm is in the C++ standard proper, but support for it is conditional. §7.4/1:

An asm declaration has the form

asm-definition:

asm ( string-literal ) ;

The asm declaration is conditionally-supported; its meaning is implementation- defined.

That said, the "conditionally supported" means you can't depend on a particular compiler supporting this at all. Microsoft (for one obvious example) uses an _asm keyword instead, but with a completely different syntax (the assembly language is enclosed in braces instead of a string literal).

like image 26
Jerry Coffin Avatar answered Sep 24 '22 01:09

Jerry Coffin