Inline assembler
Encyclopedia
In computer programming
, the inline assembler is a feature of some compiler
s that allows very low level code written in assembly
to be embedded in a high level language like C
or Ada. This embedding is usually done for one of three reasons:
instructions. This is faster than using the floating-point operations that would be emitted by the compiler, and it allows the programmer to make use of the
possible on the x86 architecture.
is used to make requests to the operating system. This is rarely a feature in a higher-level language, and so wrapper function
s for system calls are written using inline assembler
.
The following C code are samples including a system call wrapper in AT&T assembler syntax with the GNU Assembler
. They are normally written with the aid of macros; the full code is included for clarity.
The format of basic inline assembly is very much straightforward. Its basic form is
Example:
OR
Both
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, the inline assembler is a feature of some compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
s that allows very low level code written in assembly
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...
to be embedded in a high level language like C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
or Ada. This embedding is usually done for one of three reasons:
- OptimizationOptimization (computer science)In computer science, program optimization or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources...
: when assembly language is inlined for optimization, the most performance-sensitive parts of an algorithmAlgorithmIn mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning...
are replaced by hand-written assembly. This allows the programmer to use the full extent of his ingenuity, without being limited by a compiler's higher-level constructs. - Access to processor specific instructions: some processors offer special instructions, such as Compare and SwapCompare-and-swapIn computer science, the compare-and-swap CPU instruction is a special instruction that atomically compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value...
and Test and SetTest-and-setIn computer science, the test-and-set instruction is an instruction used to write to a memory location and return its old value as a single atomic operation. If multiple processes may access the same memory, and if a process is currently performing a test-and-set, no other process may begin...
— instructions which may be used to construct semaphoresSemaphore (programming)In computer science, a semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment....
or other synchronization and locking primitives. Nearly every modern processor has these or similar instructions, as they are necessary to implement multitaskingComputer multitaskingIn computing, multitasking is a method where multiple tasks, also known as processes, share common processing resources such as a CPU. In the case of a computer with a single CPU, only one task is said to be running at any point in time, meaning that the CPU is actively executing instructions for...
. To name a few, specialized instructions are found in the SPARCSPARCSPARC is a RISC instruction set architecture developed by Sun Microsystems and introduced in mid-1987....
VISVisual Instruction SetVisual Instruction Set, or VIS, is a SIMD instruction set for SPARC V9 microprocessors developed by Sun Microsystems. There are three versions of VIS: VIS 1, VIS 2 and VIS 2+...
, Intel MMX and SSEStreaming SIMD ExtensionsIn computing, Streaming SIMD Extensions is a SIMD instruction set extension to the x86 architecture, designed by Intel and introduced in 1999 in their Pentium III series processors as a reply to AMD's 3DNow! . SSE contains 70 new instructions, most of which work on single precision floating point...
, and MotorolaMotorolaMotorola, Inc. was an American multinational telecommunications company based in Schaumburg, Illinois, which was eventually divided into two independent public companies, Motorola Mobility and Motorola Solutions on January 4, 2011, after losing $4.3 billion from 2007 to 2009...
AltivecAltiVecAltiVec is a floating point and integer SIMD instruction set designed and owned by Apple, IBM and Freescale Semiconductor, formerly the Semiconductor Products Sector of Motorola, , and implemented on versions of the PowerPC including Motorola's G4, IBM's G5 and POWER6 processors, and P.A. Semi's...
instruction setInstruction setAn instruction set, or instruction set architecture , is the part of the computer architecture related to programming, including the native data types, instructions, registers, addressing modes, memory architecture, interrupt and exception handling, and external I/O...
s. - System callSystem callIn computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...
s: high-level languages rarely have a direct facility to make system calls, so assembly code is used.
Example of optimization and processor-specific instructions
This example of inline assembly is from the D programming language and computes the tangent of x using the x86's FPUFloating point unit
A floating-point unit is a part of a computer system specially designed to carry out operations on floating point numbers. Typical operations are addition, subtraction, multiplication, division, and square root...
instructions. This is faster than using the floating-point operations that would be emitted by the compiler, and it allows the programmer to make use of the
fldpi
instruction, which loads the closest approximation of piPi
' is a mathematical constant that is the ratio of any circle's circumference to its diameter. is approximately equal to 3.14. Many formulae in mathematics, science, and engineering involve , which makes it one of the most important mathematical constants...
possible on the x86 architecture.
Example of a system call
Calling an operating system directly is generally impossible in the presence of protected memory. The OS runs at a more privileged level (kernel mode) than the user (user mode); a (software) interruptInterrupt
In computing, an interrupt is an asynchronous signal indicating the need for attention or a synchronous event in software indicating the need for a change in execution....
is used to make requests to the operating system. This is rarely a feature in a higher-level language, and so wrapper function
Wrapper function
A wrapper function is a function in a computer program whose main purpose is to call a second function with little or no additional computation. This is also known as method delegation. Wrapper functions can be used for a number of purposes....
s for system calls are written using inline assembler
Inline assembler
In computer programming, the inline assembler is a feature of some compilers that allows very low level code written in assembly to be embedded in a high level language like C or Ada...
.
The following C code are samples including a system call wrapper in AT&T assembler syntax with the GNU Assembler
GNU Assembler
The GNU Assembler, commonly known as GAS , is the assembler used by the GNU Project. It is the default back-end of GCC. It is used to assemble the GNU operating system and the Linux kernel, and various other software. It is a part of the GNU Binutils package.GAS' executable is named after as, a...
. They are normally written with the aid of macros; the full code is included for clarity.
The format of basic inline assembly is very much straightforward. Its basic form is
Example:
OR
Both
asm
and __asm__
are valid. __asm__
can be used if the keyword asm
conflicts with something in your program.