C++ | asm declaration

As we know C++ is a comprehensive and powerful programming language but there are few highly specialized situations that it cannot handle. For those situations, C++ provides an option using which one can drop an assembly code at any time. This option is the use of the ‘asm’ statement. Using asm statement, the assembly language can be embedded directly into the C++ program. The asm keyword takes a single field which must be a string literal.
The general form of asm keyword is:

asm("op-code");

op-code: This is assembly language instruction that will be included in the program.

Some of the compilers uses the following form of asm statement:



asm instruction;
asm instruction newline
asm {instruction sequence }

Some GCC Inline Assembly syntax are as follows:

  1. Register Naming: Register names are prefixed with %.
    For example- registers are %eax, %cl etc, instead of just eax, cl.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include<bits/stdc++.h>
    using namespace std;
      
    int main() {
    int res;
    // move value to register %eax
    // move value to register %ebx
    // subtracting and storing result in res
    __asm__ ( "movl $20, %%eax;"
                    "movl $10, %%ebx;"
                    "subl %%ebx, %%eax ":"=a"(res));
        cout<<res;
       return 0;
      
    }

    chevron_right

    
    

    Output:

    10
    
  2. Ordering of operands:Unlike Intel convention (the first operand is destination), the order of operands is the source(s) first, and destination last.
    For example, Intel syntax “mov eax, edx” will look like “mov %edx, %eax” in AT&T assembly.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include<bits/stdc++.h>
    using namespace std;
      
    int main() {
    int res;
    // source 5 is written first
    // then destination register is written
    // this will move 5 to register
    __asm__ ( "movl $5, %%eax;": "=a"(res));
        cout<<res;
       return 0;
      
    }

    chevron_right

    
    

    Output:

    5
    
  3. Immediate Operand: Immediate operands are marked with a $ prefix.For example- as in “addl $5, %eax”, which means add immediate long value 5 to register %eax.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include<bits/stdc++.h>
    using namespace std;
      
    int main() {
    int res;
    // move immediate value 50 to register eax
    // move immediate value 20 to register ebx
    // the added value is stored in res for output
    __asm__ ( "movl $50, %%eax;"
                    "movl $20, %%ebx;"
                    "addl %%ebx, %%eax ":"=a"(res));
        cout<<res;
       return 0;
      
    }

    chevron_right

    
    

    Output:

    70
    

Basically this shows the use of inline assembly in c++. The term inline is used to instruct the compiler to insert the code of a function into the code of its caller at the point where the actual call is made. Such functions are called “inline functions”. It reduces the function call overhead. Using the asm keyword the assembly codes are written as inline functions.
Simple example of asm keyword use:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<bits/stdc++.h>
using namespace std;
int main()
{
  // generates interrupt 5
  asm int 5;  
  
  return 0;

chevron_right


When running under DOS, this program generates an INT 5 instruction, which invokes the print-screen function.


The inline assembly can be used in following two formats:

asm("assembly code") or __asm__("assembly code")

Program to illustrate the use of asm keyword:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int val1,val2, add, sub, mul;
  
   val1=100;val2=20;
    asm( "addl %%ebx, %%eax;" : "=a" (add) : "a" (val1) , "b" (val2) );
    asm( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (val1) , "b" (val2) );
    asm( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (val1) , "b" (val2) );
 printf( "%d + %d = %d\n ", val1, val2, add );
    printf( "%d - %d = %d ", val1,val2, sub );
    printf( "%d * %d = %d ", val1, val2, mul );
  
  return 0;

chevron_right


Output:

100 + 20 = 120
 100 - 20 = 80 100 * 20 = 2000

Examples:

Input : 10 20 
Output :10+20=30 10-20=-10 10*20=200  


Input : 30 20
Output :30+20=50 30-20=10 30*20=600

The above program performs the addition, subtraction and multiplication operation using the inline assembly with the help of asm keyword.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.