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. This embedding is usually done for one of three reasons:

* Optimization: when assembly language is inlined for optimization, the most performance-sensitive parts of an algorithm 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 Swap and Test and Set — instructions which may be used to construct semaphores or other synchronization and locking primitives. Nearly every modern processor has these or similar instructions, as they are necessary to implement multitasking. To name a few, specialized instructions are found in the SPARC VIS, Intel MMX and SSE, and Motorola Altivec instruction sets.
* System calls: 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 FPU 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 pi possible on the x86 architecture.

// Compute the tangent of xreal tan(real x){ asm { fld x [EBP] ; // load x fxam ; // test for oddball values fstsw AX ; sahf ; jc trigerr ; // x is NAN, infinity, or empty // 387's can handle denormalsSC18: fptan ; fstp ST(0) ; // dump X, which is always 1 fstsw AX ; sahf ; jnp Lret ; // C2 = 1 (x is out of range) // Do argument reduction to bring x into range fldpi ; fxch ;SC17: fprem1 ; fstsw AX ; sahf ; jp SC17 ; fstp ST(1) ; // remove pi from stack jmp SC18 ; }trigerr: return real.nan;Lret: ;}

Example of a system call

Directly calling an operating system 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) interrupt is used to make requests to the operating system. This is rarely a feature in a higher-level language, and so wrapper functions for system calls are written using inline assembler.

The following C code are samples including a system call wrapper. 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

asm("assembly code");

Example:

asm("movl %ecx %eax"); /* moves the contents of ecx to eax */

OR

__asm__("movb %bh (%eax)"); /* moves the byte from bh to the memory pointed by eax */

Both asm and __asm__ are valid. __asm__ can be used if the keyword asm conflicts with something in your program.

extern int errno;

int funcname(int arg1, int *arg2, int arg3) { long __res; __asm__ volatile ("movl $128, %%eax;" /* call system call 128 */ "movl %1, %%ebx;" /* pass arg1 as the first argument */ "movl %2, %%ecx;" /* arg2 */ "movl %3, %%edx;" /* arg3 */ "int $0x80;" /* make the request to the OS */ "movl %%eax, %0" /* save the result in __res */ : "=m" (__res) /* bind %0 */ : "m" (arg1), "m" ((long) arg2), "m" (arg3) /* bind %1, %2, %3 */ : "%eax","%ebx","%ecx","%edx"); /* announce to the compiler that eax...edx are dirtied */

/* The operating system will return a negative value on error; wrappers return -1 on error and set the errno global variable */ if ((unsigned long)(__res) >= (unsigned long)(-125)) { errno = (int) -(__res); __res = -1; } return (int)__res;}

External links

* [http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html GCC-Inline-Assembly-HOWTO]
* [http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Inline-Assembler.html GNAT Inline Assembler]


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Inline-Assembler — Ein Inline Assembler oder integrierter Assembler ist ein Assembler, der in den Compiler einer höheren Programmiersprache integriert ist. Mittels einer speziellen Anweisung ist hierbei Assembler Quelltext direkt in die höhere Programmiersprache… …   Deutsch Wikipedia

  • Inline — may relate to:* Inline citation or reference* Inline engine* Computers ** Inline expansion ** Inline function ** Inline assembler ** inline tag (HTML element)* Audio Equipment ** Inline adapter* Internet Posting Style ** Inline replying* Inline… …   Wikipedia

  • Assembler (Informatik) — Ein Assembler (nach DIN 44300: Assemblierer) ist ein Hilfsprogramm der Programmierung (Programmierwerkzeug), das ein in maschinennaher Assemblersprache geschriebenes Computerprogramm in Maschinensprache (auch Maschinencode oder Nativer Code… …   Deutsch Wikipedia

  • Assembler-Code — Eine Assemblersprache ist eine spezielle Programmiersprache, welche die Maschinensprache einer spezifischen Prozessorarchitektur in einer für den Menschen lesbaren Form repräsentiert. Jede Computerarchitektur hat folglich ihre eigene… …   Deutsch Wikipedia

  • Inline-Code — Inline Code,   in den Quellcode eines Programms eingebetteter Code, der ohne Sprungbefehl direkt an der Stelle ausgeführt wird, wo er aufgerufen wird. Häufig handelt es sich um Anweisungen in einer Assembler oder Maschinensprache. Inline Code… …   Universal-Lexikon

  • Integrierter Assembler — Inline Assembler (oder auch integrierter Assemblercode) ist ein Assemblerspracheteil, der in einen in einer höheren Programmiersprache geschrieben Quelltext eines Programm integriert ist. Der Übergang wird Mittels einer speziellen Anweisung… …   Deutsch Wikipedia

  • Inline expansion — In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing… …   Wikipedia

  • Cross-Assembler — Ein Assembler (nach DIN 44300: Assemblierer) ist ein Hilfsprogramm der Programmierung (Programmierwerkzeug), das ein in einer einfachen, maschinennahen Assemblersprache geschriebenes Programm in Maschinensprache (auch Maschinencode oder Nativer… …   Deutsch Wikipedia

  • GNU Assembler — Entwickler GNU Projekt Aktuelle Version 2.21 (8. Dezember 2010) Betriebssystem Cross platform Programmier­sprache C …   Deutsch Wikipedia

  • Turbo Assembler — Der Turbo Assembler (TASM) ist ein Assembler für x86er Prozessoren auf dem Betriebssystem MS DOS. Er wurde 1989 von der Firma Borland als Bestandteil von Turbo C 2.0 vorgestellt, war aber auch als separates Paket zusammen mit dem Turbo Linker… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”