Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to call inline assembly [duplicate]

Tags:

c++

assembly

I'm just exploring including assembly inside C++ source code. It seems it starts with a call to asm() but I've also seen _asm() and __asm(). What are the differences between the underscores? If relevant I'm most interested in GNU compiler.

EDIT: from this forum

_asm - simply invokes the inline assembler
__asm - is treated like an intrinsic function call

Not sure if this is true or baloney?

like image 838
Celeritas Avatar asked Mar 12 '16 02:03

Celeritas


People also ask

What is __ asm __ in C?

The __asm keyword invokes the inline assembler and can appear wherever a C or C++ statement is legal. It cannot appear by itself. It must be followed by an assembly instruction, a group of instructions enclosed in braces, or, at the very least, an empty pair of braces.

What is inline assembly explain with an example?

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

What is inline assembly in C++?

Inline assembly (typically introduced by the asm keyword) gives the ability to embed assembly language source code within a C program.

What is inline assembly in Arduino?

An inline assembly statement is a string of characters which specifies assembler code. The string can contain any instructions recognized by the assembler, including directives, but we will not discuss assembler directives here.


2 Answers

C++ doesn't have a standard, portable way to include inline assembly. Inline assembly is, almost by definition, a non-standard, non-portable thing.

However, if you've profiled your application and discovered that it needs tuning in a particular area that isn't served well enough by optimized C++ and/or intrinsics, I'd recommend putting the assembly into its own file(s) that are conditionally assembled by the appropriate tool for each platform the code is intended to run on. You would also want a native C++ implementation for platforms whose assembly language you don't support.

As an aside, I've used GNU's inline asm variants in the past, and I have to say they tend to make your code look really ugly and opaque to another programmer. If you're writing bare-metal code that simply has to have maximum bandwidth, well, okay, but if you want something long-term maintainable... maybe favor that over performance.

like image 134
Aiken Drum Avatar answered Nov 15 '22 09:11

Aiken Drum


With the GNU C/C++ compiler the asm, _asm, __asm, and __asm__ keywords all mean and do the same thing. The asm keyword is the one you should normally when writing inline assembly for GCC. In most cases where you see any of the other keywords used it's a mistake and the author is needlessly typing extra characters. The __asm__ keyword exists only for headers files that need to be compatible with when one of GCC's strict conformance options are used.

GCC's use of the asm keyword isn't compatible with the C and C++ standards. These standards allow programs to use asm as an identifier, for example, in variable or function names. Normally GCC doesn't allow this, but in it's strict conformance modes (eg. -std=c99) it doesn't treat asm a keyword so it can be used as identifier. Header files that use inline assembly and meant to be used in various different projects can use the __asm__ keyword instead of the asm.

The other two keywords _asm and __asm shouldn't be used at all as they don't seem to be formally documented.

like image 40
Ross Ridge Avatar answered Nov 15 '22 08:11

Ross Ridge