(英文版)使用Visual Studio 2015 编写 MASM 汇编程序!
原文地址:http://kipirvine.com/asm/gettingStartedVS2015/index.htm#CreatingProject
Getting Started with MASM and Visual Studio 2015 | |||||||||||||||||||||||||||||||||
Updated 10/3/2016
This tutorial assumes that you are using the Seventh Edition of Assembly Language for x86 Processors. We show you how to set up Visual Studio 2015. Topics:
The book's example programs in Chapters 1-13 have been successfully tested in both 32/64-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.
Found an error in this document? Please email the author. Except where noted, all instructions in this document apply equally to Visual Studio and Visual Studio Community Edition. Required Setup for 32-bit ApplicationsWhen installing Visual Studio, be sure to select Visual C++ under the Programming Languages category. (If you forget to do this, you can always rerun the install file and modify the existing installation.) Visual Studio includes Microsoft Assembler (MASM) version 14. Look for the file named ml.exe in the \vc\bin folder of your Visual Studio installation directory, such as c:\Program Files (x86)\Microsoft Visual Studio 14.0\vc\bin. Installing the Book's Example ProgramsClick this link to get the latest copy of the book's link libraries and example programs. The examples are stored in a Microsoft Install (.MSI) file that installs 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.) If you plan to change the installation location, read our instructions relating to Creating a Project from Scratch. The folllowing files will be copied into the c:\Irvine directory:
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. Setting up Visual StudioYou will only have to do these steps the first time you use Visual Studio. Add the Start Without Debugging command to the Debug menuIt's very useful to run programs without having to debug them. To do that, you will want to add a new command to the Debug menu: Start Without Debugging. Here's how to do it:
In fact, you can use the same sequence to customize any of the menus and toolbars in Visual Studio. Select the C++ ConfigurationVisual 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:
Set the Tab Size to 5(This is an optional step.) Start Visual Studio, and select Options from the Tools menu. Select Text Editor, Select All Languages, and select Tabs. Optionally, you may want to select the Insert spaces radio button: Set the Tab Size and Indent Size to 5. Tutorial: Building a 32-Bit Assembly Language ProgramNow you're ready to open and build your first 32-bit project. Opening a ProjectVisual 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. Right-click here to download a zip file containing an up-to-date Visual Studio 2015 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. Do the following steps, in order:
You should see the following program in the editor window:
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 ever 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.
Build the ProgramNow 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: 1>------ Build started: Project: Project, Configuration: Debug Win32 ------ 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. Run the ProgramSelect Start without Debugging from the Debug menu. The following console window should appear, although your window will be larger than the one shown here: The "Press any key to continue..." message is automatically generated by Visual Studio. Congratulations, you have just run your first Assembly Language program! Press any key to close the Console window. Running a program from the Command Prompt: When you assembled and linked the project, a file named Project32_VS2015.exe was created inside the project's \Debug folder. This file executes when you run the project. You can execute any EXE by double-clicking its name inside Windows Explorer, but it will often just flash on the screen and disappear. That is because Windows Explorer does not pause the display before closing the command window. On the other hand, you can open a Command prompt window, move to the Debug directory, and run Project32_VS2015.exe by typing "Project32_VS2015" (without the quotes). You will need to do some reading on Windows shell commands if you plan to use the command line.
Any time you want 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. Step 5: Running the Sample Program in Debug ModeIn this step, you set a breakpoint inside the sample program. Then you use the Visual Studio debugger to step through the program's execution one statement at a time.
RegistersIf you want to display the CPU registers, do the following: Start debugging the program, then select Windows from the Debug menu. Select Registers from the drop-down list. The Registers window may appear at the bottom of the workspace, as a tab highlighted in yellow. Use the mouse to drag the window to the right side of the work area. Right click inside the Registers window and check the item Flags to enable the display of CPU status flags. You can interrupt a debugging session at any time by selecting Stop Debugging from the Debug menu. You can do the same by clicking the maroon-colored square button on the toolbar. To remove a breakpoint from the program, click on its red dot to make it disappear. Setting a BreakPointIf 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.
You can remove a breakpoint by clicking its red dot with the mouse. Take a few minutes to experiment with the Debug menu commands. Set more breakpoints and run the program again. For the time being, you can use the F11 key to step through the program in the same way the F10 key did. Building and Running Other ProgramsSuppose 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.
Adding a File to a ProjectAn 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:
Copying a Source FileOne 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. Tutorial: Building and Running a 64-Bit ProgramIn this tutorial, we will show you how to assemble, link, and run a sample 64-bit program. We assume you have already completed our tutorial entitled Building a 32-Bit Assembly Language Program. Do the following steps, in order:
You should see the following program in the editor window: ; AddTwoSum_64.asm - Chapter 3 example. ExitProcess proto (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.) Build the ProgramSelect Build Project from the Build menu. You should see text written to Visual Studio's output window like the following: 1>------ Build started: Project: Project64_VS2015, Configuration: Debug Win64 ------ 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. Building 16-bit Applications (Chapters 14-17)Only Chapters 14 through 17 require you to build 16-bit applications. Except for a few exceptions, which are noted in the book, your 16-bit applications will run under the 32-bit versions of Windows (XP, Vista, 7). But 16-bit applications will not run directly 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. The filename in the sample image (make16.bat) has been replaced by two files:
Step 1: Create the Build 16-bit ASM CommandClick the Add button and fill in the Title, Command, Arguments, and Initial directory fields as shown in the screen snapshot. If you click the buttons with arrows on the right side of the Arguments and Initial directory fields, a convenient list appears. You can select an item without having to worry about spelling: Click the Apply button to save the command. Step 2: Create the Run 16-bit ASM CommandClick 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. Testing Your new 16-Bit CommandsTo 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: Press a key to close the window. You have completed the setup for building and running 16-bit assembly language programs. Creating a 32-Bit Project From ScratchVisual Studio makes it possible (in 14 steps!) to create an Assembly Language project from scratch. You must create a Win32 Console application designed for C++ and modify various settings. Step 1: Select New from the File menu, then select Project. In the New Project window, select Win32 under Visual C++ in the left panel, and select Win32 Console Application in the middle panel. Give your project a suitable name (near the bottom of the window): (Depending on your Visual Studio configuration, you might have to find Visual C++ under the "Other Languages" category in the left panel.) Step 2: Click the OK button to continue. The Win32 Application Wizard window will appear. Select Application Settings, and then select the Empty project check box. Step 3: Click the Finish button to save the new project. Step 4: Open the Solution Explorer window and add an assembly language source file to your project. Here's how: Right-click the project name in the Visual Studio window, select Add, select Existing Item. 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. Note: You may delete the Header Files, Resource Files, and Source Files folders if you wish. They are not necessary. Step 5: Next, right click your project name, select Build Dependencies, then select Build Customizationsfrom the popup menu. When you see this window, select the check box next to masm and click the OK button to close the dialog. Step 6: You're almost ready to set the assembler properties. But first, you must help Visual Studio associate source code files having the .asm extension with the Microsoft Macro Assembler. To do this, right-click the .asm file in the Solution Explorer window, select Properties from the popup window, click on Item Type in the right-hand pane, scroll down the list, and select Microsoft Macro Assembler. Click OK to close the dialog window. Step 7: Right-click the project name in the Solution Explorer window and select Properties from the popup menu. Expand the entry under Configuration Properties. Then expand the entry named Microsoft Macro Assembler. This is what you should see: Step 8: Modify the Include Paths option so it equals "C:\Irvine". This tells the assembler where to find files having a filename extension of ".inc". Here is a sample: Step 9: Next, select the Listing File entry, also in the Microsoft Macro Assembler group. Modify the Assembled Code Listing File entry (shown below) so it contains $(ProjectName).lst. This uses a built-in variable to identify the name of the source input file, with a file extension of .lst. So, if your project were named MyProject, the listing file would be named MyProject.lst: Step 10: Find the Linker entry under Configuration Properties. Select the Input entry in the left panel and insert irvine32.lib; at the beginning of the Additional Dependencies entry. The irvine32.lib file is the link library file supplied with this book. The filenames must be separated by semicolons. Step 11: Select Linker under Configuration Properties, and then select General. The Additional Library Directories entry must contain c:\Irvine so the linker can find the Irvine32.lib library file: Step 12: Select Linker under the Configuration Properties and select Debugging. Verify that the Generate Debug Info option is equal to Yes(/DEBUG) Step 13: Select Advanced under the Linker entry. Set the Image Has Safe Exception Handlers option to No. Step 14: Click the OK button to close the Property Pages window. Verify that your project has been created correctly by building and debugging the program as you did in an earlier tutorial. Generating a Source Listing FileOpen the project. From the menu, select Project, select Properties. In the list box, select Microsoft Macro Assembler, then select Listing File. Set the Assembled Code Listing file option to $(InputName).lst. MASM syntax highlightingWhen 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. There is an interesting third party Visual Studio 2015 extension named Asm-Dude, featuring a rich set of syntax highlighting and code completion features. You can download it from GitHub.
If you decide to use Visual Studio's built-in MASM syntax highlighter, here are the required steps to set it up: 1) Download the Usertype.dat file (enclosed in a ZIP file) given here to a folder in which you have read/write permissions. If you are using Windows 7, download to My Documents, or C:\temp, or any folder that doesn't have security restrictions. 2) Copy Usertype.dat to the C:\Program Files\Microsoft Visual Studio 12.x\Common7\IDE folder. In 64-bit windows, Program Files will be named Program Files(x86). Windows will display a verification dialog before copying the file. 3) 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. Close Visual Studio and restart it. 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: ;// AddTwo.asm - adds two 32-bit integers. |
(英文版)使用Visual Studio 2015 编写 MASM 汇编程序!的更多相关文章
- 用Visual Studio 2015 编写 MASM 汇编程序(二)从头开发一个Win32汇编程序
一,建立一个VC的控制台类型的空工程: 1,从VS菜单中选择“文件”->“新建”->“项目”. 2,在新建项目中选择:“Visual c++”->"Win32"- ...
- 用Visual Studio 2015 编写 MASM 汇编程序(一)环境配置
原文地址:http://kipirvine.com/asm/gettingStartedVS2015/index.htm#CreatingProject 下面内容根据上面文章翻译整理而来! 开发32位 ...
- 用Visual Studio 2015 编写驱动之前一定要注意的问题!!!
如果你确定要使用Visual Studio 2015 编写驱动,那么在你安装Visual Studio 2015 和WDK之前,一定一定要注意一件事情,那就是确保SDK和WDK版本保持一致,切记切记! ...
- 用Visual Studio 2015 编写第一个UMDF驱动遇到的问题!!
前提:Visual Studio 2015已经成功安装了驱动环境,WDK都已经完全正常安装了,在Visual Studio 2015的菜单可以看到"Driver"菜单项了.这说明已 ...
- 使用Visual Studio 2013 调试 MASM 汇编程序!
原文地址:http://kipirvine.com/asm/debug/vstudio2013/index.htm Using the Microsoft Visual Studio 2013 Deb ...
- Visual Studio 2015里面汇编工具Asm Dude的配置!
最近开始学习汇编,也开始使用Visual Studio 2015写汇编程序,比较了半天,最后觉得Asm Dude应该是目前Visual Studio 2015非常好的汇编开发插件了,但是如果默认安装上 ...
- Visual Studio 2015 开发 ASP.NET 5 有何变化?
本篇博文目录: ASP.NET 5 模版 ASP.NET 5 目录结构 前端管理工具 无编译开发 Microsoft Git Provider 智能感知和错误信息 Smart Unit Testing ...
- 使用Visual Studio 2015 Community 开发windows服务
昨天研究在.NET下开发Windows服务程序,期间遇到一些小问题,这里仅将自己的开发过程和需要注意的地方写下和广大网友分享…… 1.基础 Windows服务是指系统启动时能够自己运行的程序.W ...
- Visual Studio 2015 和 Apache Cordova 跨平台开发入门(一)
基于 Windows 10 的 Visual Studio 2015 跨平台的应用开发主要分为基于Visual Studio 安装 Xamarin 扩展的跨Android.iOS 和 Windows的 ...
随机推荐
- python中文输出和写入文本
中文输出 #-*-coding:utf8-*- import requests import re timeout = 8 headers = {'User-Agent':'Mozilla/5.0 ( ...
- linq实现左连接
1.左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals ...
- linux服务之openfiler
架构:b/s 服务器端:封装好的linux系统 客户端:浏览器 相关包:封装好的linux系统 Openfiler imports user and group information from ce ...
- CSS之column语法
columns column-width:[<length>|auto] 定义每栏的宽度 column-span:1|all 1:只在本栏中显示:all:横跨所有栏目并定位在栏目的Z轴之上 ...
- Flowplayer-一款免费的WEB视频播放器
Flowplayer 是一个开源(GPL 3的)WEB视频播放器.您可以将该播放器嵌入您的网页中,如果您是开发人员,您还可以自由定制和配置播放器相关参数以达到您要的播放效果. Flowplayer支持 ...
- kindle相关工具
里是与 Kindle 电子书相关的工具软件.它们可以帮助我们解决在日常使用电子书时所可能遇到的问题,比如 kindle 管理工具.kindle 转换工具.kindle电子书制作工具.kindle 推送 ...
- ScrumMaster需要了解的7件事
当一个组织开始使用Scrum时,被选为担任Scrumaster角色的人通常来自于那些有管理背景的人.组织期望那些管理人员,所谓的“大师”,能够交付Scrum项目因为她有管理的专门知识——并且可以同时管 ...
- Kettle使用jndi mssqlserver
kettle可以使用jdbc的方式设置job或者tansform的数据库连接,但是,同时它也支持JNDI方式连接数据库,后者更加方便,只需要配置一份配置文件就可以了,不用每个DBConnection都 ...
- sqlserver服务器常用的性能计数器
sqlserver服务器常用的性能计数器,在此标记. 性能对象 计数器 说明 Processor %Processor Time %Privileged Time 建议值:持续低于80 建议值:持续低 ...
- MYSQL 获取表的列信息
SELECT COLUMN_NAME as '列名' ,DATA_TYPE as '字段类型' ,COLUMN_TYPE as '长度加类型' FROM information_schema.`COL ...