C++ | asm declaration - GeeksforGeeks

10 Sep, 2019
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:
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
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
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

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

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

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

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

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

#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;  }
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;} 
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.
CPP-Basics
linux-command
C++
Linux-Unix
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Sed Command in Linux/Unix with examples
AWK command in Unix/Linux with examples
grep command in Unix/Linux
cut command in Linux with examples
TCP Server-Client implementation in C