Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Inline assembler
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Syntax == === In language standards === The ISO C++ standard and ISO C standards (annex J) specify a conditionally supported syntax for inline assembler: <blockquote><poem> An asm declaration has the form ''asm-declaration'': {{mono|asm}} ( ''string-literal'' ) ; The asm declaration is conditionally-supported; its meaning is implementation-defined.<ref>C++, [dcl.asm]</ref> </poem></blockquote> This definition, however, is rarely used in actual C, as it is simultaneously too liberal (in the interpretation) and too restricted (in the use of one string literal only). === In actual compilers === In practical use, inline assembly operating on values is rarely standalone as free-floating code. Since the programmer cannot predict what register a variable is assigned to, compilers typically provide a way to substitute them in as an extension. There are, in general, two types of inline assembly supported by C/C++ compilers: * {{tt|asm}} (or {{tt|__asm__}}) in [[GNU Compiler Collection|GCC]]. GCC uses a direct extension of the ISO rules: assembly code template is written in strings, with inputs, outputs, and clobbered registers specified after the strings in colons. C variables are used directly while register names are quoted as string literals.<ref name=GCCEXT>{{cite web |title=Extended Asm - Assembler Instructions with C Expression Operands |url=https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html |website=Using the GNU C Compiler |access-date=15 January 2020}}</ref> * {{tt|__asm}} in Microsoft [[Visual C++]] (MSVC), Borland/Embarcadero C compiler, and descendants. This syntax is not based on ISO rules at all; programmers simply write ASM inside a block without needing to conform to C syntax. Variables are available as if they are registers and some C expressions are allowed.<ref name=msvc>{{cite web |title=Inline Assembler |url=https://docs.microsoft.com/en-us/cpp/assembler/inline/inline-assembler |website=docs.microsoft.com |date=3 August 2021 |language=en-us}}</ref> [[ARM Compiler]] used to have a similar facility.<ref>{{Cite web|url=https://www.keil.com/support/man/docs/armclang_mig/armclang_mig_ioz1485879652178.htm|title=Migration and Compatibility Guide: Inline assembly with Arm Compiler 6}}</ref> The two families of extensions represent different understandings of division of labor in processing inline assembly. The GCC form preserves the overall syntax of the language and compartmentizes what the compiler needs to know: what is needed and what is changed. It does not explicitly require the compiler to understand instruction names, as the compiler is only needed to substitute its register assignments, plus a few {{tt|mov}} operations, to handle the input requirements. However, the user is prone to specifying clobbered registers incorrectly. The MSVC form of an embedded [[domain-specific language]] provides ease of writing, but it requires the compiler itself to know about opcode names and their clobbering properties, demanding extra attention in maintenance and porting.<ref name=rust-asm>{{cite web |last1=d'Antras |first1=Amanieu |title=Rust RFC-2873: stable inline asm |url=https://rust-lang.github.io/rfcs/2873-inline-asm.html |access-date=15 January 2020 |date=13 December 2019|quote=However it is possible to implement support for inline assembly without support from the compiler backend by using an external assembler instead. }} [https://github.com/rust-lang/rfcs/pull/2850 Pull Request for status tracking]</ref> It is still possible to check GCC-style assembly for clobber mistakes with knowledge of the instruction set.<ref>{{cite web |title=β D54891 [RFC] Checking inline assembly for validity |url=https://reviews.llvm.org/D54891 |website=reviews.llvm.org}}</ref> GNAT (Ada language frontend of the GCC suite), and [[LLVM]] uses the GCC syntax.<ref>{{cite web |title=LLVM Language Reference: Inline assembly expressions |url=http://llvm.org/docs/LangRef.html#inline-assembler-expressions |website=LLVM Documentation |access-date=15 January 2020}}</ref><ref>{{cite web |title=Inline Assembly |url=https://doc.rust-lang.org/1.0.0/book/inline-assembly.html |website=Rust Documentation (1.0.0) |access-date=15 January 2020}}</ref> The [[D programming language]] uses a DSL similar to the MSVC extension officially for x86_64,<ref>{{cite web |title=Inline Assembler |url=https://dlang.org/spec/iasm.html |website=D programming language |access-date=15 January 2020}}</ref> but the LLVM-based LDC also provides the GCC-style syntax on every architecture.<ref>{{cite web |title=LDC inline assembly expressions |url=https://wiki.dlang.org/LDC_inline_assembly_expressions |website=D Wiki |access-date=15 January 2020}}</ref> MSVC only supports inline assembler on 32-bit x86.<ref name=msvc/> The Rust language has since migrated to a syntax abstracting away inline assembly options further than the LLVM (GCC-style) version. It provides enough information to allow transforming the block into an externally-assembled function if the backend could not handle embedded assembly.<ref name=rust-asm/>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)