Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C++
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Interoperability between C++ and Assembly==== C++ provides two primary methods of integrating ASM code. 1. Standalone assembly files β Assembly code is written separately and linked with C++ code.<ref>{{cite web |url=https://wiki.osdev.org/C%2B%2B_to_ASM_linkage_in_GCC |title=C++ to ASM linkage in GCC |website=OSDev Wiki |access-date=1 April 2025}}</ref> 2. [[Inline assembly]] β Assembly code is embedded within C++ code using compiler-specific extensions. ;Example Code for ASM Compatibility * When calling an assembly function from C++, use <syntaxhighlight lang="C++" inline>extern "C"</syntaxhighlight> to prevent C++ name mangling. <syntaxhighlight lang="cpp" line="1"> //main.cpp import std; extern "C" int add_asm(int, int); // Declare the assembly function int main() { int result = add_asm(5, 7); std::println("Result from ASM: {}", result); return 0; } </syntaxhighlight> <syntaxhighlight lang="cpp" line="1"> #asm code using RISC-V architecture .section .text .global add_asm add_asm: add a0, a0, a1 # Add first argument (a0) and second argument (a1), store in a0 ret # Return (a0 holds return value) </syntaxhighlight> *Global variables in assembly must be declared as <syntaxhighlight lang="C++" inline>extern</syntaxhighlight> in C++ and marked <code>.global</code> in assembly. <syntaxhighlight lang="cpp" line="1"> // main.cpp import std; extern "C" int global_var; // Declare global variable from assembly int main() { std::println("Global variable from ASM: {}", global_var); return 0; } </syntaxhighlight> <syntaxhighlight lang="cpp" line="1"> #asm using RISC-V architecture .section .data .global global_var .align 4 global_var: .word 42 # Define integer value </syntaxhighlight> * Inline assembly allows embedding ASM directly in C++ using the <syntaxhighlight lang="C++" inline>asm</syntaxhighlight> keyword. <syntaxhighlight lang="cpp" line="1"> //main.cpp (using GCC/CLANG compiler) import std; int main() { int x = 10, y = 20, sum; asm volatile ( "add %0, %1, %2" : "=r" (sum) // Output operand (stored in a register) : "r" (x), "r" (y) // Input operands (stored in registers) ); std::println("Sum using inline ASM: {}", sum); return 0; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)