How to Run Assembly within C++ code
A useful resource for extreme situations.
C++ is a comprehensive and powerful programming language, but there are few highly specialized situations that it cannot solve.
This option is to use the
__asm__() instruction or just
asm(). In other words, the Assembly language can be incorporated directly into the C++ program.
Basic example of use
In this example, it can be seen that through the Assembly code:
- Moves immediate value
- Move immediate value 6 to register
- The added value is stored in
After compiling and running:
The output will be the sum:
If you want to avoid using underlines it will work the same way:
The fact of using
__asm__ like this is that the programmer has greater control of the native resources of C++.
For more information visit: https://en.cppreference.com/w/cpp/language/asm.