DEV Community

Cover image for Inline Assembly Language. What is that?
A Serputov
A Serputov

Posted on

Inline Assembly Language. What is that?

An inline assembler is a part of some compilers that permits low-level code written in assembly language to be embedded within a program, among code that otherwise has compiled from a higher-level language such as C.

The embedding of assembly language code is usually done for one of these reasons:

  • Optimization.
  • Pass to processor-specific instructions.
  • Pass to certain calling conventions not yet supported by the compiler.
  • System calls and interrupts: High-level languages rarely have a direct facility to make arbitrary system calls, so assembly code is used.
  • To emit special directives for the linker or assembler, such as changing sectioning, macros, or making symbol aliases.

Examples:

Demonstrates two kinds of inline assembly syntax offered by the GCC compiler. This program will only work correctly on the x86-64 platform under Linux.

#include <stdio.h>

extern int func(void);
// the definition of func is written in assembly language
__asm__(".globl func\n\t"
        ".type func, @function\n\t"
        "func:\n\t"
        ".cfi_startproc\n\t"
        "movl $7, %eax\n\t"
        "ret\n\t"
        ".cfi_endproc");

int main(void)
{
    int n = func();
    // gcc's extended inline assembly
    __asm__ ("leal (%0,%0,4),%0"
           : "=r" (n)
           : "0" (n));
    printf("7*5 = %d\n", n);
    fflush(stdout); // flush is intentional

    // standard inline assembly in C++
    __asm__ ("movq $60, %rax\n\t" // the exit syscall number on Linux
             "movq $2,  %rdi\n\t" // this program returns 2
             "syscall");
}
Enter fullscreen mode Exit fullscreen mode

Output

7*5 = 35
Enter fullscreen mode Exit fullscreen mode

_Inline assembler poses a direct concern for the compiler as it confuses the breakdown of what is done to individually variable, a crucial piece of register allocation. It represents the performance might fall. The inline assembler also complicates future porting and maintenance of a program.

Alternative structures are often provided to simplify the job for both the compiler and the programmer. Most compilers offer intrinsic functions for special instructions, and C-function wrappers for arbitrary system calls are available on every Unix platform._

Usually, "intrinsics" guides to built-in functions -- i.e. most common library functions that the compiler can/will cause inline rather than calling a fundamental function in the library. For instance, a call like memset(array1, 10, 0) could compile for an x86 as something like:

mov ecx, 10
 xor eax, eax
 mov edi, offset FLAT:array1
 rep stosb
Enter fullscreen mode Exit fullscreen mode

Intrinsics like this are purely an optimization. "Needing" intrinsics would most likely be when the compiler helps intrinsics that allow you to develop code that the compiler can't (or usually won't) generate directly. For an obvious example, quite a few compilers for x86 have "MMX Intrinsics" that let you use "functions" that are just direct representations of MMX instructions.

Are there Intrinsics or inline assembly with JavaScript.

Honestly, I searched a lot about this question and found only this information. I will leave this blog post as a work in progress for now. If you have comments, please share them. Thank you, I appreciate any help with this research.

https://github.com/WebAssembly/design/issues/1206
https://webassembly.org
https://stackoverflow.com/questions/12168575/executing-generated-assembler-inline

Resources

https://wiki.cdot.senecacollege.ca/wiki/Inline_Assembly_Language
https://en.cppreference.com/w/c/language/asm
https://en.wikipedia.org/wiki/Inline_assembler
https://stackoverflow.com/questions/2268562/what-are-intrinsics
https://www.codeproject.com/Articles/15971/Using-Inline-Assembly-in-C-C
https://stackoverflow.com/questions/48518225/what-are-intrinsic-types-in-javascript
https://stackoverflow.com/questions/12168575/executing-generated-assembler-inline
https://github.com/v8/v8/blob/master/src/objects/dictionary.h

Conclusion

⚠️ Computer Architecture Blog Post: Link

Links

🖇 Follow me on GitHub

🖇 Follow me on Twitter

_p.s This post was made for my Software Portability and Optimization class.

Top comments (0)