Clever Geek Handbook
πŸ“œ ⬆️ ⬇️

Assembly insert

In programming , assembly insertion refers to the ability of the compiler to embed low-level code written in assembler into a program written in a high-level language , such as C or Ada . The use of assembler inserts may serve the following purposes:

  • Optimization : For this purpose, assembler code is written manually that implements the most critical parts of the algorithm with respect to performance. This allows the programmer to take full advantage of his ingenuity, not limited to compiler designs.
  • Access to specific processor instructions : Some processors support special instructions, such as comparison with exchange and test-and-set - instructions that can be used to implement semaphores or other synchronization and locking primitives. Almost all modern processors have these or similar instructions, since they are necessary for multitasking . Special instructions can be found in the command systems of the following processors: SPARC VIS , Intel MMX and SSE , Motorola AltiVec .
  • System calls : High-level programming languages ​​rarely provide a direct opportunity to make system calls; assembler code is used for this purpose [1] .

Content

An example of optimizing and using special processor instructions

This example of assembly language insert in programming language D , which implements the calculation of the tangent x, uses x86 architecture FPU instructions . This code runs faster than code that could be generated by the compiler. Also, the fldpi instruction is used here, which loads the closest approximation of the numberΟ€ {\ displaystyle \ pi}   for x86 architecture.

  // Compute the tangent of x
 real 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 denormals
 SC18 : 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 system call

Direct access to the operating system is usually not possible with protected memory. The OS runs at a more privileged level (kernel mode) than the user (user mode). In order to make requests in the OS, software interrupts are used. Rarely do high-level languages ​​support this feature, so system call interfaces are written using assembler inserts [1] .

The following CI example contains a system call interface written using the AT&T GNU Assembler syntax . First, let's look at the assembler insert format using a simple example:

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

The identifiers asm and __asm__ equivalent. Another example of a simple insert:

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

Example implementation of a system call interface:

  extern int errno ;

 int funcname ( int arg1 , int * arg2 , int arg3 )
 {
   int res ;
   __asm__ volatile (
     "int $ 0x80" / * make the request to the OS * /
     : "= a" ( res ), / * return result in eax ("a") * /
       "+ b" ( arg1 ), / * pass arg1 in ebx ("b") * /
       "+ c" ( arg2 ), / * pass arg2 in ecx ("c") * /
       "+ d" ( arg3 ) / * pass arg3 in edx ("d") * /
     : "a" ( 128 ) / * pass system call number in eax ("a") * /
     : "memory" , "cc" );  / * announce to the compiler that the memory and condition codes have been modified * /

   / * The operating system will return a negative value on error;
 * wrappers return -1 on error and set the errno global variable * /
   if ( - 125 <= res && res < 0 ) {
     errno = - res ;
     res = - 1 ;
   }  
   return res ;
 }

Notes

  1. ↑ 1 2 "Programming in Linux" Chapter 5. How system calls work (unspecified) . Opennet

Links

  • GCC-Inline-Assembly-HOWTO
  • GNAT Inline Assembler
  • Clang inline assembly
Source - https://ru.wikipedia.org/w/index.php?title=Assembler Insert&oldid = 87693863


More articles:

  • Afremov House
  • Greater Kosogol
  • Chemyakina (Irkutsk Oblast)
  • Far Zakora
  • 41 (number)
  • Aleksich, Ivan
  • Turk, Vasily Ivanovich
  • Litvinovskaya parish (Starobelsky district)
  • Mosty volost (Starobelsky uyezd)
  • Another day

All articles

Clever Geek | 2019