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汇编代码了。


转载于:https://www.cnblogs.com/witton/archive/2012/07/12/6868976.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值