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:
- Register Naming: Register names are prefixed with %.
For example- registers are %eax, %cl etc, instead of just eax, cl.#include<bits/stdc++.h>usingnamespacestd;intmain() {intres;// 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;return0;}chevron_rightfilter_noneOutput: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>usingnamespacestd;intmain() {intres;// source 5 is written first// then destination register is written// this will move 5 to register__asm__ ("movl $5, %%eax;":"=a"(res));cout<<res;return0;}chevron_rightfilter_noneOutput: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>usingnamespacestd;intmain() {intres;// 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;return0;}chevron_rightfilter_noneOutput: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.
Recommended Posts:
- Difference between Definition and Declaration
- Can C++ reference member be declared without being initialized with declaration?
- Synchronous Chatting Application using C++ boost::asio
- C/C++ For loop with Examples
- Keyboard Shortcuts for Ubuntu | Set - 1
- How to Fix Minimal BASH Like Line Editing is Supported GRUB Error In Linux?
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- Making an Alias to Shutdown Ubuntu
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Single and Multiple Inheritance in C++
- Difference between Abstraction and Encapsulation in C++
- return statement in C/C++ with Examples
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.



