Visual Studio中使用NASM编译汇编文件
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"?> <VisualStudioToolFile Name="Netwide Macro Assembler" Version="8.00" > <Rules> <CustomBuildRule Name="NASM" DisplayName="Netwide Macro Assembler" CommandLine="nasm.exe [AllOptions] [AdditionalOptions] [Inputs]" Outputs="[$ObjectFileName]" FileExtensions="*.asm" ExecutionDescription="正在汇编..." > <Properties> <StringProperty Name="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" /> <StringProperty Name="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" /> <StringProperty Name="IncludePaths" DisplayName="包含路径" PropertyPageName="常规" Description="设置包含文件查找路径. (-I[path])" HelpURL="http://www.nasm.us/doc/" Switch="-I[value]" Delimited="true" Inheritable="true" /> <BooleanProperty Name="TreatWarningsAsErrors" DisplayName="将警告视为错误" PropertyPageName="常规" Description="Returns an error code if warnings are generated. (-Werror)" HelpURL="http://www.nasm.us/doc/" Switch="-Werror" /> <BooleanProperty Name="GenerateDebugInformation" DisplayName="生成调试信息" PropertyPageName="常规" Description="Generates Debug Information. (-g)" HelpURL="http://www.nasm.us/doc/" Switch="-g" DefaultValue="true" /> <EnumProperty Name="Format" DisplayName="目标格式" PropertyPageName="常规" Description="输出文件的格式(win32,win64,bin,obj,coff),(-f [format])" > <Values> <EnumValue Value="0" Switch="-f win32" DisplayName="WIN32" /> <EnumValue Value="1" Switch="-f win64" DisplayName="WIN64" /> <EnumValue Value="2" Switch="-f bin" DisplayName="BIN" /> <EnumValue Value="3" Switch="-f obj" DisplayName="OBJ" /> <EnumValue Value="4" Switch="-f coff" DisplayName="COFF" /> </Values> </EnumProperty> <StringProperty Name="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" /> <StringProperty Name="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汇编代码了。
祝,玩得开心!
E_Mail:witton@163.com
E_Mail:witton@163.com

浙公网安备 33010602011771号