天天看点

VS2015中运行32位汇编程序

【前言】

  这学期开汇编语言课。作为培养方案中的一门选修课,与课组中教微机原理、嵌入式的同事商量,决定这门课就以8086为载体,带大家入门即可。不过,在课程结束之前,也向同学们做了拓展。

  本文的版权属于我的2015级学生小鲁同学。他在课程设计阶段,费了不少周折,配置环境,完成了32位汇编程序的开发。他将整个过程做了记录,并且完成本文。

  我说:“你将这些文字发成博客做个分享。”他说:“不发了。老师,您的博客点击量大,给您吧。”

  这是本文的由来。感谢这位自主、慷慨的弟子!

【正文】

  Visual Studio 2015只需要14步就可以运行32位汇编语言。你必须创建一个32位的控制台应用(原本32位控制台应用用于C++编写)并且修改几个地方的设置。

Step1:选择左上角的创建项目,选择创建Win32控制台应用程序。(Depending on your Visual Studio configuration, you might have to find Visual C++ under the “Other Languages” category in the left panel.)当然你的Visual Studio 2015的win32控制选项可能需要你自己找找了,但是Visual Studio 2015必须可以编写并运行C++程序。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

Step2:都搞好之后点确定,进行下一步的选择,点击下一步,之后选择空项目。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

Step3:点完成保存项目的设置。

Step4:此时方案已经生成,在右边显示了当前的项目名称,接下来需要点击鼠标右键选择生成依赖项,选择生成自定义,此时会出现一个生成自定义文件的选择对话框,选择masm,然后点击确定。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

Step5:此时就可以进行基本的80386汇编程序了,在右上方的项目的源文件中添加新建项,创建C++文件,将名称改为*.asm作为汇编程序,之后点击添加。添加一个测试文件试试是否可行。

将此段汇编程序写入创建好的mytest.asm文件中,点击上方的生成,生成解决方案,之后观察当前控制台的提示,成功之后就可以点击本地Windows调试器运行了。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序
VS2015中运行32位汇编程序
VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

下载好之后安装即可,我安装在C盘的默认目录下,即:C:\Irvine。之后进行配置。

Step7:右击项目,选择属性,在弹出的对话框中选择Microsoft Macro Assembly,就会看到如下配置。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

Step8:选择General,然后点击Include Paths,点击编辑,将刚刚安装的Irvine的目录写入编辑框中,This tells the assembler where to find files having a filename extension of “.inc”.告诉编辑器去哪里寻找后缀名位inc的文件,输入好点击确定。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

Step9:接下来我们想让VS2015生成链接文件,方便我们查看汇编代码对应的机器代码,点击Listing File,点击Assembled Code Listing File点击编辑,输入$(ProjectName).lst,确定。

VS2015中运行32位汇编程序

Step10: 找到连接器,选择输入,在附加依赖库中添加irvine32.lib,注意每个依赖库以分号结尾!

VS2015中运行32位汇编程序

Step11:选择连接器的常规,选择附加库目录,将安装的地址写入,使得连接器可以找到Irvine32.lib这个库文件,之后点击确定。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

Step12:选择连接器的调试,将生成调试信息选择为“优化以便于调试(/DEBUG)”

VS2015中运行32位汇编程序

Step13:选择连接器的高级,选择映像具有安全异常处理程序,选择否。

VS2015中运行32位汇编程序

Step 14:Ok,配置完成,试试调用Irvine32头文件是否可行。下面给出一个在Github上托管的代码的实例:

以同样的方式生成解决方案,这个时候输出正确!可以进行愉快的32位汇编喽!

VS2015中运行32位汇编程序

注意调试断点的使用!红点就是断点。

VS2015中运行32位汇编程序

调试时调出内存管理,利用反汇编进行内存内容的查看,当然也可以把寄存器也调出来:

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

这个是个人调整之后的调试界面,左下角的寄存器部分也已进行多种选项,显示标志寄存器,左上角有个反汇编,

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

将里边的内存地址赋值,在内存查看部分可以看到内存的信息。

VS2015中运行32位汇编程序
VS2015中运行32位汇编程序

点击逐步运行,可以看到寄存器的变化,以及标志寄存器,红色代表改变。

VS2015中运行32位汇编程序

程序编译成功之后生成的列表文件和编写的文件在同一目录下。

VS2015中运行32位汇编程序

对列表文件的部分截屏:

VS2015中运行32位汇编程序

最后还差一个变色包,让汇编环境关键字变色。

MASM syntax highlighting

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:

VS2015中运行32位汇编程序

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.

VS2015中运行32位汇编程序

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。

或者给出VS2015变色安装包,不过安装完之后需要在VS2015中更新一下,重启才有效果。