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.
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.
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:
.asm
file, Properties, change Item Type to "Microsoft Macro Assembler".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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With