Open In App

C++ | asm declaration

Improve
Improve
Like Article
Like
Save
Share
Report

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.




    #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;
      
    }

    
    

    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.




    #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;
      
    }

    
    

    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.




    #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;
      
    }

    
    

    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:




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


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:




#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;


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.



Last Updated : 10 Sep, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads