4

There are two forms of inline assembly, one is

asm{
    //...
}

the other

asm(
    "..."//work in my clang compiler
);

and __asm and so on, so what is the difference? definitely not just about curly braces and brackets, i hope

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • That's the great thing about standards: there are so many to choose from. – Bathsheba Nov 21 '18 at 11:56
  • also valid: __asm__() – Mike Nov 21 '18 at 12:01
  • @Mike I know that, but I just want to know what differs – bright yang Nov 21 '18 at 12:04
  • 1
    The first kind is MSVC style inline assembly while the second one is gcc style inline assembly. Entirely different approaches, the second one is usually superior for the situations where you'd use inline assembly in production code today. – fuz Nov 21 '18 at 12:06
  • I've tagged lawyer to increase the possibility of someone submitting an answer superior to mine. – Bathsheba Nov 21 '18 at 12:09
  • the behavior is the same, It depends on your compiler which one is accepted – Mike Nov 21 '18 at 12:09
  • @fuz You should make that an answer and maybe explain *why* one is superior. – Caleb Nov 21 '18 at 12:15
  • @Caleb If one compiler gives you apples and another gives you oranges, while what the OP wanted was bananas (standardization), then writing an answer about why apples are better than oranges doesn't seem meaningful. – Lundin Nov 21 '18 at 12:18
  • 2
    @Lundin if one compiler gives you hammer, and other gives you toolbox (including hammer), then the other is superior. (which is basically the analogy to MSCC inline asm vs gcc/clang inline asm) ... also you basically shouldn't use either, unless you really know what you are doing: https://gcc.gnu.org/wiki/DontUseInlineAsm – Ped7g Nov 21 '18 at 12:46
  • 1
    @Ped7g These aren't the only forms, as there's more to programming than your PC. There's also hammers with wooden shafts, hammers with metal shaft, hammers with metal shaft + rubber for handle, hammers that can remove nails etc etc. Now, what I would like is a standardized saw. Which one of these hammer types is best for sawing? – Lundin Nov 21 '18 at 13:02
  • 1
    @Caleb I don't quite feel like making an answer right now, but gcc-style syntax is superior because it allows the programmer to specify a lot of metadata the compiler can use for optimisations. Also, gcc-style syntax allows the compiler to place variables in registers which isn't possible with MSVC-style assemble. While the latter might be a bit more user-friendly, it is pretty much obsolete with today's reasons why people write inline assembly at all. – fuz Nov 21 '18 at 17:44
  • @fuz : although more flexible (and superior), GCC's extended inline assembly comes at a cost - very easy to get things wrong (less forgiving) and introduce often hard to track bugs into the code. It should be used sparingly. If there is an intrinsic then use it. If one doesn't fully understand the nuances of GCC's extended inline it may be better to put the code in a separate assembly module. The latter may be less optimal, but it may be safer. – Michael Petch Nov 21 '18 at 19:00

2 Answers2

3

The C language does not support inline assembler, so either form is non-standard. Every compiler tends to do this differently and therefore the syntax will vary.

Some compilers use double underscore __ to let through all non-standard keywords even when compiling in C standard mode.

Unlike C, C++ does support it through standardization, in the form of asm("instruction");.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

Annex J of the C standard is informative and states that if asm is supported (and it doesn't have to be), then it ought to be of the form

asm ( character-string-literal );

In other words, the notation using the braces is arguably less conventional than the one using the parentheses.

(The requirements in C++ are stronger.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • C does't support `asm`. Annex J is not normative and it only gives an example of the most common form of non-standard extensions - the same one as standardized by C++. – Lundin Nov 21 '18 at 12:12
  • 1
    J.5.10 says "The most common implementation is via a statement of the form asm(...)" How do you read it to say that it must be of this form, or that any other form isn't standard? – Paul Hankin Nov 21 '18 at 12:19
  • 1
    @PaulHankin The key is C11 p554: "Annex J (informative)". Nothing in that annex contains language requirements, it is just extra information. – Lundin Nov 21 '18 at 12:21