Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling assembly in Visual Studio

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.

like image 663
bobobobo Avatar asked Dec 28 '10 19:12

bobobobo


People also ask

How do I run assembly code in Visual Studio?

In the Solution Explorer window, click the + symbol next to the item named Project to expand it. Double-click the file named main. asm to open it in the editing window. (Visual Studio users may see a popup dialog asking for the encoding method used in the asm file.


2 Answers

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".
like image 55
Hans Passant Avatar answered Oct 03 '22 11:10

Hans Passant


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)

like image 20
bobobobo Avatar answered Oct 03 '22 13:10

bobobobo