Compiling assembly in Visual Studio

WindowsVisual Studio-2010Assembly

Windows Problem Overview


How do you compile assembly code using Visual Studio?

I want to compile and run an assembly source file in Visual Studio 2010.

I've created a Visual C++ project and inserted some assembly code in a file code.asm:

.586              ;Target processor.  Use instructions for Pentium class machines
.MODEL FLAT, C    ;Use the flat memory model. Use C calling conventions
.STACK            ;Define a stack segment of 1KB (Not required for this example)
.DATA             ;Create a near data segment.  Local variables are declared after
                  ;this directive (Not required for this example)
.CODE             ;Indicates the start of a code segment.

clear PROC
   xor eax, eax 
   xor ebx, ebx 
   ret 
clear ENDP 
END

However the problem is when you try and compile this, you get:

LINK : error LNK2001: unresolved external symbol _mainCRTStartup

I did go and enable the build customization masm.targets (right click project > Build Customizations..), but to no avail.

Windows Solutions


Solution 1 - Windows

Sounds to me like the custom build rules for .asm files isn't enabled. Right-click the project, Custom Build Rules, tick "Microsoft Macro Assembler". With the "END clear" directive and disabling incremental linking I'm getting a clean build.

It's different starting from VS2010:

  1. Right-click Project, Build customizations, tick "masm".
  2. Right-click the .asm file, Properties, change Item Type to "Microsoft Macro Assembler".

Solution 2 - Windows

#Command line:

Compile the code with:

ml /c /Cx /coff code.asm

You get code.obj as the output.

Link with:

link code.obj /SUBSYSTEM:console /out:go.exe /entry:clear

You can now run go.exe.

Alternatively, do it all in one go with:

ml /Cx /coff code.asm /link /SUBSYSTEM:console /link /entry:clear

#Visual Studio (not solved)

Solution 3 - Windows

Visual Studio includes the MASM macro assembler. Smaller fragments of assembler code are often written in inline assembly in a C or C++ program.

To integrate an assembler file in a Visual Studio project, create a regular C/C++ project (command line or GUI), and just add a file ending in .asm to the list of source files.

To specify clear as the entry point, follow these instructions:

  1. Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.

  2. Click the Linker folder.

  3. Click the Advanced property page.

  4. Modify the Entry Point property.

(It was taken from the Visual Studio documentation.)

I can confirm Hans Passant's instruction. In addition, according to this article, if you first add the "build customizations" masm checkbox, and then add the file, it will automatically be recognized as an assembler file. Furthermore, not specifying the entry point name in the END directive, but instead specifying it in the project settings also works for me.

Solution 4 - Windows

here is how to compile nasm assembly source code with vs20xx:

  1. "Excluded From Build" to "No"

  2. "Item Type" to "Custom Build Tool"

  3. Hit Apply

  4. Custom Build Tool -> General -> Command Line:

    c:\nasm\nasm -f win64 my_asm.asm

  5. Custom Build Tool -> General -> Outputs:

    my_asm.obj

  6. call the function like this:

    extern "C" int foo(void); // written in assembly!

https://www.cs.uaf.edu/2017/fall/cs301/reference/nasm_vs/

nasm tutorial:

http://cs.lmu.edu/~ray/notes/nasmtutorial/

Solution 5 - Windows

The problem is that your assembly code is just a function. To compile and link, you need to have a start procedure just like Main in C/C++. You can specify the start symbol by specifying in your END directive. Like:

END clear

Or if you want, you can link the .obj file generated with the C/C++ generated .obj one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionboboboboView Question on Stackoverflow
Solution 1 - WindowsHans PassantView Answer on Stackoverflow
Solution 2 - WindowsboboboboView Answer on Stackoverflow
Solution 3 - WindowsMartin v. LöwisView Answer on Stackoverflow
Solution 4 - Windowssailfish009View Answer on Stackoverflow
Solution 5 - WindowsMadhur AhujaView Answer on Stackoverflow