Article by Ayman Alheraki on January 11 2026 10:34 AM
To write a comprehensive guide on how to compile an assembly helper program and link it with a C++ program using Visual Studio 2022 (VC++ 2022), the following steps, along with examples and necessary settings, can be followed:
Open Visual Studio 2022.
From the main menu, select File -> New -> Project.
Choose Console App (C++), name the project, and click Create.
Start by writing a simple C++ program that will call the helper function written in assembly. For example:
// Declaration of the assembly functionextern "C" int add_two_numbers(int a, int b);
int main() { int result = add_two_numbers(5, 7); std::cout << "The result is: " << result << std::endl; return 0;}To create an assembly file, you can add an .asm file to your project:
In your C++ project, right-click on Source Files.
Select Add -> New Item, then choose ASM File or manually add an .asm file.
Write the assembly function:
section .text global add_two_numbers
add_two_numbers: mov eax, edi ; Move the first argument (a) into eax add eax, esi ; Add the second argument (b) to eax ret ; Return with the result in eaxTo successfully compile assembly files alongside your C++ project, some settings need to be adjusted:
Right-click on the project and select Properties.
Under Configuration Properties :
In General, ensure that Configuration Type is set to Application (.exe).
In VC++ Directories, add the path to your assembly libraries if necessary.
In C/C++ -> General, add the path to header files if applicable.
In Linker -> Input, make sure to include the compiled object files for linking.
Under Configuration Properties -> Custom Build Tool :
Add the assembler tool such as ml.exe or nasm as needed.
In Outputs, specify the compiled .obj files.
Once your project is set up:
Click Build to compile the program.
If linking is successful, you can run the program using Debug -> Start Without Debugging.
ml /c /Fo [output file] [input file]: This option compiles the assembly file without linking.
ml /link /ENTRY:main: This option links the compiled object files with the C++ project.
extern "C" int add_two_numbers(int a, int b);
int main() { int result = add_two_numbers(5, 7); std::cout << "The result is: " << result << std::endl; return 0;}section .text global add_two_numbers
add_two_numbers: mov eax, edi add eax, esi retC++ Linker: Ensure the linking with the obj files generated from the assembly code.
Assembler Options: Make sure to use ml.exe or the appropriate assembler tool configured in the project.
By following these steps, you can successfully link an assembly program with a C++ program in Visual Studio 2022.