
MASM Assembly in Visual Studio 2010
Recently I have been doing some WIn32 assembly language programming, extending a simple program with some new functionality. As the program grew in length and complexity I began to miss the syntax highlighting, project management, and debugging abilities of Visual Studio.
Googling about suggesed that it was possible to get VS2010 to do what I wanted but it really wasn’t so easy to get it all set up the first time around.
In order to save myself figuring this out again, and maybe help one of you dear readers, I’m putting a step by step guide up here.
Before you start it makes a lot of sense to install support for Assembly Language Syntax Highlighting which you can find on this CodePlex project. It’s a simple download and run installer.
Step 1 : Create a clean project
File | New | Project…
Expand the ‘Other Project Types‘ tree, Select ‘Visual Studio Solutions‘, and create a new ‘Blank Solution‘.
File | Add | New Project…
Expand the ‘Other Languages‘, ‘Visual C++‘, ‘General‘ section and create a new ‘Empty Project‘
Step 2: Acquire the MASM options.
Now right click on the Project in the Solution Explorer and select ‘Build Customizations…‘
Tick the ‘masm‘ box and say OK.
Add a new file to the project with the .asm extension by right clicking on the Project in the Solution Explorer and selecting ‘Add | New Item…‘ then ‘Text File‘. Enter a filename ending with .asm (e.g. speedy.asm). Say OK.
Now (and if you skipped the last steps this won’t work) right click on the Project and select ‘Properties‘. You should see a dialog like this (Note the MASM item at the bottom of the tree). If you don’t then something went wrong.
Step 3: Configure the linker
There are a few critical things to set up in the Linker options in order to get it to work:
Set the following property to Windows or Console as appropriate
Configuration Properties > Linker > System> SubSystem
Set the entry point to the name of your main method (as per the END directive – see code)
Configuration Properties > Linker > Advanced > EntryPoint
Step 4: Write some code & Run it
Lets write a very simple assembly language program to test this out (if you want to learn about assembler you could well try Iczelions’ tutorials and the MASM Forum.
.586 .model flat, stdcall option casemap :none ; To get unicode support include \masm32\macros\ucmacros.asm include \masm32\include\windows.inc include \masm32\include\kernel32.inc includelib \masm32\lib\kernel32.lib include \masm32\include\user32.inc includelib \masm32\lib\user32.lib .data ; WSTR gets you a unicode string definition WSTR wstrTitle, "Hello" WSTR wstrMessage, "World" .code main: invoke MessageBoxW, NULL, ADDR wstrMessage, ADDR wstrTitle, MB_OK invoke ExitProcess, eax end main
NOTE: Possibly the most important thing to note here is the ‘end main’ directive. This directive must be present and the name must match the label where you expect execution to kick off and the ‘EntryPoint’ we defined in step 3. Otherwise things simply won’t work.
Hit Ctrl + Shift + B to build (or use the menus etc) and it should build and run showing a simple windows message box.
Boring but proves it’s working.
Step 5: Set break points and debug it đ
The really cool thing is that now you can set break points and step through your code much as you are used to doing with C++ or C# đ
Side Note: File extensions
A small problem that you might run into is that if you move any macro definitions into their own file you need to be absolutely sure NOT to call the file .asm. If you do the linker will get horribly confused and go on and on and on about not being able to find the EntryPoint. I lost hours trying to figure that one out! Call it something .inc instead and all will be good.
The other thing is that Visual Studio seems to create a larger executable (even in release mode) than using masm on the command line. It seems to be something to do with the way it interprets the WSTR macro but I’m not 100% certain. Still if it becomes a huge issue I can always compile on the command line just before release and I get to enjoy nice debugging in the meantime.
So, there you have it. VS2010 compiling Win32 Assembler by way of the included MASM compiler.
-
On 28 June 2015 Steyn
said...
You saved me hours of headache…i was going crazy trying to assemble and it was just a few configuration changes.
THANK YOU!!!!
-
On 27 January 2015 William
said...
Unfortunately the example you have posted depends on the MASM32 SDK, which is under a restrictive license which among other things prohibits open source development.
-
On 28 February 2014 Laura
said...
Great tutorial. Helped me set up my MASM project. I created an .inc file with all my PROCs for my project, but VS2012 gives me this error when I try to build my project:
fatal error A1000: cannot open file : Hex.inc
I don’t know how to fix this. The .inc file is in the same directory as the rest of the project.
What am I missing? I JUST started programming in assembly and using VS last month and have no clue how to get this working… Can you help?
-
On 13 November 2013 Junga
said...
i get two errors:
1. error A1000: cannot open file : \masm32\macros\ucmacros.asm
line 6
2. error MSB3721: The command “ml.exe /c /nologo /Zi /Fo”Debug\speedy.obj” /W3 /errorReport:prompt /Taspeedy.asm” exited with code 1.
line 49Maybe you know what i did wrong?
-
On 21 September 2012 Wang
said...
How it works in visual studio 2012 ďź
It seems this way does not work in visual studio 2012. -
On 29 May 2012 Tim
said...
All I get is this error on compile. My dummy test file was called texst66.asm
Tried several simple MASM sources that work fine under QEditor that comes with MASM. Every single one gives this line. Looked up Error 1- and the MSB3721- but not much out there- some CUDA devs using VS2010 for MASM-
any thoughts ?
Nice tutorial though, just didn’t work for me.
With VS2010
Error 1 error MSB3721: The command “ml.exe /c /Zi /Fo”Debug\texst66.obj” /W3 /errorReport:prompt /Tatexst66.asm” exited with code 1. C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations\masm.targets 49 6 test1
-
On 14 May 2012 xcrypt
said...
Nvm, the problem I was having was solved by D-side’s comment
-
On 14 May 2012 xcrypt
said...
Very helpful tutorial, but I’m still getting a few errors:
CODE:
.586
.model flat, stdcall
option casemap :none; To get unicode support
include \masm32\macros\ucmacros.asminclude \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.libinclude \masm32\include\user32.inc
includelib \masm32\lib\user32.lib.data
; WSTR gets you a unicode string definition
WSTR wstrTitle, “Hello”
WSTR wstrMessage, “World”.code
main:
invoke MessageBoxW, NULL, ADDR wstrMessage, ADDR wstrTitle, MB_OK
invoke ExitProcess, eax
end mainDEBUG INFO:
1>_MASM:
1> Assembling [Inputs]…
1>test.asm(19): error A2008: syntax error : WSTR
1>test.asm(20): error A2008: syntax error : WSTR
1>test.asm(25): error A2006: undefined symbol : wstrTitle
1>test.asm(25): error A2114: INVOKE argument type mismatch : argument : 3
1>test.asm(25): error A2006: undefined symbol : wstrMessage
1>test.asm(25): error A2114: INVOKE argument type mismatch : argument : 2
1>\masm32\macros\ucmacros.asm(13): error A2052: forced error
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations\masm.targets(49,5): error MSB3721: The command “ml.exe /c /nologo /Zi /Fo”Debug\test.obj” /W3 /errorReport:prompt /Tatest.asm” exited with code 1.
1>
1>Build FAILED.I’m also wondering where these \masm32 includes come from, I can’t seem to find any directory on my computer like that?
Thanks in advance
-
On 27 April 2012 Duncan
said...
I only looked at 32-bit.
You must be logged in to post a comment.