Getting Started With MASM and Visual Studio 2019
Getting Started With MASM and Visual Studio 2019
Getting Started With MASM and Visual Studio 2019
Updated 6/28/2019
This tutorial assumes that you are using either the 7th or 8th Edition of Assembly
Language for x86 Processors, and you are using Visual Studio 2019. We tested these
pages with the Community Edition, and have every reason to assume the Enterprise
Edition will work the same way.
Topics:
After you have downloaded and installed the VS 2019 Community Edition, you may
need to install the Visual C++ language option. First, let's see if it has already been
installed (as often happens in college computer labs). Select File >> New >> Project
from the Visual Studio menu. You will see this Create a new project dialog window.
Look for C++ in the Language dropdown list:
Note: If you do not see Visual C++ in the list, close Visual Studio and run a separate
program named the Visual Studio Installer. If your computer is in a college
laboratory, your account may not have sufficient privileges to run this program, so
you can ask your lab supervisor to do this.
(If you run the VS installer, select the Desktop development with C++ button in the
installer window, look at the Summary list on the right side to verify that VC++ is
selected, and click the Modify button in the lower right corner of the window.)
The Visual C++ language includes the Microsoft Assembler (MASM). To verify that
MASM is installed, open a Windows Explorer window and look for the file
named ml.exe in the Visual Studio installation directory, such as C:\Program Files
(x86)\Microsoft Visual
Studio\2019\Community\VC\Tools\MSVC\14.xx.xxxx\bin\HostX64\x86. (The "x"
characters above indicate digits in the version number of your current VS
installation.)
At the top of this document, we explained how to download the file named Irvine.zip
and extract it into the C:\Irvine folder. Unless you have some objection to using that
location, do not alter the path. (Note to lab administrators: you can designate
c:\Irvine directory as read-only.).
Filename Description
b16.asm, Blank templates for 16-bit and 32-bit assembly language
b32.asm source files
GraphWin.inc Include file for writing Windows applications
Include file used with the Irvine16 link library (16-bit
Irvine16.inc
applications)
Irvine16.lib 16-bit link function library used with this book
Include file used with the Irvine32 link library (32-bit
Irvine32.inc
applications)
Irvine32.lib Irvine's 32-bit link library
Kernel32.lib 32-bit link library for Windows API
Link16.exe 16-bit Microsoft linker
Macros.inc Irvine's macro include file (see Chapter 10)
make16_vs2019.
Visual Studio 2019 batch file for building 16-bit applications
bat
Small-sized include file containing MS-Windows definitions,
SmallWin.inc
used by Irvine32.inc
User32.lib MS-Windows basic I/O link library
VirtualKeys.inc Keyboard code definitions file, used by Irvine32.inc
A subdirectory named Examples will contain all the example programs shown in the
book, source code for the book's 16-, 32-, and 64-bit libraries, and two sample
projects for earlier versions of Visual Studio.
Visual Studio supports multiple programming languages and application types. The
C++ programming language configuration most closely matches that of assembly
language programming, so we suggest the following steps:
Opening a Project
Visual Studio requires assembly language source files to belong to a project, which is
a kind of container. A project holds configuration information such as the locations of
the assembler, linker, and required libraries. A project has its own folder, and it holds
the names and locations of all files belonging to it.
If you have not already done so,Right-click here to download a zip file containing an
up-to-date Visual Studio 2019 project that has been configured for assembly
language. After downloading this file, un-zip it into your working directory. It
contains a sample asm test file named AddTwo.asm.
.386
.model flat,stdcall
.stack 4096
ExitProcess
proto,dwExitCode:dword
.code
main proc
mov eax,5
add eax,6
invoke ExitProcess,0
main endp
end main
In the future, you can use this file as a starting point to create new programs by
copying it and renaming the copy in the Solution Explorer window.
Adding a File to a Project: If you need to add an .asm file to an open project, do
the following: (1) Right-click the project name in the Visual Studio window, select
Add, select Existing Item. (2) In the Add Existing Item dialog window, browse to the
location of the file you want to add, select the filename, and click the Add button to
close the dialog window.
Now you will build (assemble and link) the sample program. Select Build
Project from the Build menu. In the Output window for Visual Studio at the bottom
of the screen, you should see messages similar to the following, indicating the build
progress:
If you do not see these messages, the project has probably not been modified since
it was last built. No problem--just select Rebuild Project from the Build menu.
The easiest way to run your first program is to use the debugger. First, you must set
a breakpoint. When you set a breakpoint in a program, you can use the debugger to
execute the program a full speed (more or less) until it reaches the breakpoint. At
that point, the debugger drops into single-step mode. Here's how to do it:
1. Make sure the ASM source code file is open in the editor window.
2. Click the mouse along the border to the left of the mov eax,5 statement. A
3. Select Start Debugging from the Debug menu. The program should run and
pause on the line with the breakpoint. (Optionally, you can close the
4. Press the F10 key (called Step Over) to execute the current statement.
the invoke statement.
5. A small black window icon should appear on either your Windows desktop or
status bar. The window should be blank because this program does not
You can remove a breakpoint by clicking its dot with the mouse. Take a few minutes
to experiment with the Debug menu commands. Set more breakpoints and run the
program again.
Here's what your program will look like when paused at the breakpoint:
To remove a source file from the Visual Studio window, right-click its filename and
select Remove. The file will not be deleted from the file system. On the other hand,
if you want to delete the file, select it and press the Del key.
Registers
Soon you will want to display CPU registers when debugging your programs. Here's
how to make them visible: First, under the Tools >> Options menu, select Debbuging
in the left panel, and select Enable address-level debugging. Next, set a breakpoint in
your source code on an executable statement, run your program in Debug mode,
select Windows from the Debug menu, and then select Registers from the drop-down
list.
If you do not see the Registers command in the Debug >> Windows drop-down
menu (which seems to be the case for the VS2019 Community Edition, there is a way
to add a Registers command button to your Debug toolbar. Here's how to do it:
The Registers window may appear docked to the top of the workspace, but you may
find it helpful to float the window on top of your workspace. Just grab the window
header with the mouse and pull it to the center area. You will also want to display the
CPU flags. To do that, right click inside the Registers window and check the
word Flags from the popup menu.
A reminder, you might want to review our tutorial: Using the Visual Studio debugger
Suppose you want to run another example program, or possibly create your own
program. You can remove the existing assembly language file from the Solution
Explorer window and insert a new .asm file into the project.
To remove a program from a project without deleting the file, right-click its
name in the Solution Explorer window. In the context menu, select Remove.
If you change your mind and decide to add it back to the project, right-click
in the same window, select Add, select Existing item, and select the file you
want to add.
An easy way to add an assembly language source file to an open project is to drag its
filename with the mouse from a Windows Explorer window onto the name of your
project in the Solution Explorer window. The physical file will not be copied--the
project only holds a reference to the file's location. Try this now:
One way to make a copy of an existing source code file is to use Windows Explorer to
copy the file into your project directory. Then, right-click the project name in Solution
Explorer, select Add, select Existing Item, and select the filename.
Return to top
ExitProcess proto
.data
sum qword 0
.code
main proc
mov rax,5
add rax,6
mov sum,rax
mov ecx,0
call ExitProcess
main endp
end
(Notice that the program's entry point is the main procedure. If you wish to use a
different name for your startup procedure in your own programs, you can modify this
option by selecting Properties from the Project menu, and then selecting Linker /
Advanced / Entry Point.)
Select Build Project from the Build menu. You should see text written to Visual
Studio's output window like the following:
1>------ Build started: Project: Project, Configuration: Debug x64
------
1> Assembling AddTwoSum_64.asm...
1> Project64_VS2019.vcxproj ->
...\Project64_VS2019\x64\Debug\Project.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped
==========
If you do not see these messages, the project has probably not been modified since
it was last built. No problem--just select Rebuild Project from the Build menu.
You use the same Visual Studio commands to run and debug 64-bit programs as you
would for 32-bit programs.
Return to top
If you're interested in running 16-bit programs under 64-bit Windows, you will need
to enable a feature named NTVDM.) Click here to read a web site with instructions on
how to do this. Another alternative you may wish to explore is to install a virtual
machine (using a free program named VirtualBox from Oracle) and install 32-bit
Windows on the virtual machine.
The book's example programs in Chapters 1-13 have been successfully tested in 32-
bit Windows 7,8, and 10. On the other hand, many programs in Chapters 14-17 will
not run in any Microsoft OS later than Windows 98, because they rely on direct
access to hardware and system memory. You cannot directly run 16-bit applications
in any 64-bit version of Windows.
If you plan to build 16-bit applications, you need to add two new commands to the
Visual Studio Tools menu. To add a command, select External Tools from the Tools
menu. The following dialog will appear, although many of the items in your list on the
left side will be missing. Download the batch file here (rename the .txt extension
to .bat after downloading): make16_vs2019.txt.
Click the Add button again, and create a new command named Run 16-bit ASM:
Uncheck the "Close on exit" option and click the OK button to save the command and
close the External Tools dialog.
To test your new 16-bit commands, close any Visual Studio project that happens to
be open. Then, select File | Open | File from the menu and choose the file
named 16-bit.asm from the ch03 folder in the book's example programs.
Select Build 16-bit ASM from the Tools menu. The following command window
should appear, showing the successful execution of the assembler and linker,
followed by a listing of all files related to this program:
Press a key to close the window. Next, you will run the program. Select Run 16-bit
ASM from the Tools menu. The following window will appear, although the contents
of all registers except EAX will be different:
You have completed the setup for building and running 16-bit assembly language
programs.
Return to top
When a text editor uses syntax highlighting, language keywords, strings, and other
elements appear in different colors. Visual Studio highlights MASM reserved words
and strings, as shown in the following example:
This won't happen automatically, but you can create a syntax definition file named
Usertype.dat that contains MASM keywords. Then when Visual Studio starts, it reads
the syntax file and highlights MASM keywords.
If you decide to use Visual Studio's built-in MASM syntax highlighter, here are the
required steps to set it up:
4) Open Visual Studio, select Options from the Tools menu, select Text Editor, and
select File Extension. On the right side of the dialog (shown below), enter asm as
the extension, select Microsoft Visual C++ from the Editor list, and click
the Add button. Click the OK button to save your changes.
Open your project and display an ASM file. You should see syntax highlighting in the
editor. There is a glitch in the highlighting--assembly language comment line starts
start with a semicolon, which C++ doesn't recognize. But this is a simple
workaround: add an extra // right after the semicolon, like this, which will cause the
comments to appear in their usual green color:
Return to top
Return to top