Visual Studio中使用NASM编译汇编文件

来源:互联网 发布:锦纶 氨纶 涤纶 知乎 编辑:程序博客网 时间:2024/06/02 21:04

    NASM相对MASM和GAS而言,是一款比较中庸的汇编器,它语法简洁、功能强大,而且跨平台、免费,是外联汇编的不错选择。

    使用Visual Studio开发项目时,如果需要外联NASM汇编,可以使用VS中集成的功能进行设定,让它自动编译相应的汇编文件。在VS2005以前的版本中,可以使用“生成事件”来设置汇编文件的编译工作;在VS2005及以上版本中,可以使用“自定义生成规则”来设定。这里主要说明一下后者。

    在VS2005及以上版本中都有“自定义生成规则”功能,它使用一个扩展名为.rules的XML格式的文档来定义生成规则,VS自带一个MASM的生成规则文件masm.rules,在VS安装目录下的VC\VCProjectDefaults中可以找到。要在VS中使用NASM汇编器,也需要一个这样的文件,可以在http://sourceforge.net/projects/nasm/files/Contributions/rules%20file%20for%20VS/下载,将之放在前面提到的目录中。这个文件只能生成Win32格式的文件。为了生成多种文件格式,需要自己添加相应的规则。下面是我修改后的,可以生成多种格式的规则文件。

<?xml version="1.0" encoding="utf-8"?><VisualStudioToolFileName="Netwide Macro Assembler"Version="8.00"><Rules><CustomBuildRuleName="NASM"DisplayName="Netwide Macro Assembler"CommandLine="nasm.exe [AllOptions] [AdditionalOptions] [Inputs]"Outputs="[$ObjectFileName]"FileExtensions="*.asm"ExecutionDescription="正在汇编..."><Properties><StringPropertyName="PreprocessorDefinitions"DisplayName="宏定义"PropertyPageName="常规"Description="Defines a text macro with the given name.     (-D[symbol])"HelpURL="http://www.nasm.us/doc/"Switch="-D[value]"Delimited="true"Inheritable="true"/><StringPropertyName="UndefinePreprocessorDefinitions"DisplayName="取消宏定义"PropertyPageName="常规"Description="Undefines a text macro with the given name.     (-U[symbol])"HelpURL="http://www.nasm.us/doc/"Switch="-U[value]"Delimited="true"Inheritable="true"/><StringPropertyName="IncludePaths"DisplayName="包含路径"PropertyPageName="常规"Description="设置包含文件查找路径.     (-I[path])"HelpURL="http://www.nasm.us/doc/"Switch="-I[value]"Delimited="true"Inheritable="true"/><BooleanPropertyName="TreatWarningsAsErrors"DisplayName="将警告视为错误"PropertyPageName="常规"Description="Returns an error code if warnings are generated.     (-Werror)"HelpURL="http://www.nasm.us/doc/"Switch="-Werror"/><BooleanPropertyName="GenerateDebugInformation"DisplayName="生成调试信息"PropertyPageName="常规"Description="Generates Debug Information.     (-g)"HelpURL="http://www.nasm.us/doc/"Switch="-g"DefaultValue="true"/><EnumPropertyName="Format"DisplayName="目标格式"PropertyPageName="常规"Description="输出文件的格式(win32,win64,bin,obj,coff),(-f [format])"><Values><EnumValueValue="0"Switch="-f win32"DisplayName="WIN32"/><EnumValueValue="1"Switch="-f win64"DisplayName="WIN64"/><EnumValueValue="2"Switch="-f bin"DisplayName="BIN"/><EnumValueValue="3"Switch="-f obj"DisplayName="OBJ"/><EnumValueValue="4"Switch="-f coff"DisplayName="COFF"/></Values></EnumProperty><StringPropertyName="ObjectFileName"DisplayName="输出文件"PropertyPageName="常规"Description="Specifies the name of the output object file.     (-o [file])"HelpURL="http://www.nasm.us/doc/"Switch="-o "[value]""DefaultValue="$(IntDir)\$(InputName).obj"/><StringPropertyName="AssembledCodeListingFile"DisplayName="汇编代码列表文件"PropertyPageName="常规"Description="Generates an assembled code listing file.     (-l [file])"HelpURL="http://www.nasm.us/doc/"Switch="-l "[value]""/></Properties></CustomBuildRule></Rules></VisualStudioToolFile>

    将此文件放在指定目录后,打开VS的“自定义生成规则”对话框,在“可用规则文件”中我们可以看到刚才添加的规则文件,把它前面的选择框勾上就可以在项目中编译NASM汇编代码了。


原创粉丝点击