参考链接:
Integrating a compiler/assembler in VS ; Using NASM with Visual Studio 2010 - CodeProject
Creation and Implementation
As the fundamental of the target file is understood, its time to create a requirement specific target file, its schema and property definition files (already shown in previous section for illustration of elements). These files are attached with the article. To make it globally available its better to copy these files (nasm.prop,nasm.targets & nasm.xml) into the MSBuild directory (for me its "C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations\" )where the masm.targets exists ($(VCTargetsPath)\BuildCustomizations\).
Note: There is no need to set anything to build and run the provided sample. They are already configured to reference their own directory only.
So below is the quick summary for the configurations adopted to use build with utmost ease:-
- Add nasm installation path to the PATH variable or copy the executable to the %SYSTEMROOT%\System32 or whatever to make it available on command prompt with just its name.
- Copy nasm.prop,nasm.targets & nasm.xml to "C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations\" (Explained earlier).
Now follow these steps to create a sample functional project which uses nasm.
- Create a C++ project (Preferably Empty Project); avoid managed projects. Name it "NASM".
- In the solution explorer Right Click Project name ( topmost parent node i.e. NASM ) -> Build Customizations. A pop-up will appear with nasm as an option along with masm and lc (this wont appear if you haven’t copied the target and property files in Build Customization directory, explained earlier and you have to manually locate it using "Find Existing…" option in the pop-up).Check the nasm checkbox and OK.
- Again in the solution explorer Right Click Source Files -> Add -> New Item -> Select C++ or header file, enter name of the file (Nasm) followed by extension .asm (i.e. Nasm.asm) and click Add. Do it once more to add Called.asm.
- Open the property page (through solution explorer-> right click NASM-> Properties or Select NASM and Alt+Enter or Project->Properties in menu .
- Select Linker in the Configuration Properties category. Expand it and click Input. In the Additional Dependencies add one more library "libcmt.lib;". You can add other as well. This is just a C Run-Time (CRT) Library.
- Now click Advanced option in the left pane. Yes !!!! of course , we are planning for a chaos. In this era I don’t think anyone learns assembly for printing black and white "Hello World". So set Data Execution Prevention to "No" so that we can bounce the instruction pointer to the data section.
Great, settings done, add the following code to Nasm.asm and Called.asm.
创建与实施
作为目标文件的基本知识,它是创建特定于需求的目标文件,其架构和属性定义文件的时间(已经在上一节中显示了元素说明)。 这些文件随文章一起提供。 为了使其在全球范围内可用,最好将这些文
件(nasm.prop,nasm.targets和nasm.xml)复制到MSBuild目录(对我来说,其“ C:\ Program Files \ MSBuild \ Microsoft.Cpp \ v4.0 \ BuildCustomizations \ “)masm.targets所在的位
置($(VCTargetsPath)\ BuildCustomizations \)。
注意:无需设置任何内容即可构建和运行提供的示例。 它们已经被配置为仅引用自己的目录。
因此,以下是为最轻松地使用build而采用的配置的快速摘要:-
1. 将nasm安装路径添加到PATH变量中,或将可执行文件复制到%SYSTEMROOT%\ System32或任何其他文件中,以使其在命令提示符下仅具有名称即可使用。
参考百度文档 “WIN10 设置环境变量 PATH的方法”
https://jingyan.baidu.com/article/4f7d5712ba9f735b201927ac.html
Nasm下载地址
Index of /pub/nasm/releasebuilds/2.15.05/win64
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\BuildCustomizations
工作目录
G:\csdn nasm visual studio\410776\Sample_Project\NASM
启动项目测试
遇到问题了!!!
问题是printf再Visual Studio 2015 中使用有问题 ,具体咋弄还没想好,后来把printf换成了puts()就暂时算能运行了。
2. 将nasm.prop,nasm.targets和nasm.xml复制到“ C:\ Program Files \ MSBuild \ Microsoft.Cpp \ v4.0 \ BuildCustomizations \ ”(前面已介绍)。
现在,按照以下步骤创建使用nasm的示例功能项目。
1. 创建一个C ++项目(最好是空项目); 避免管理项目。 将其命名为“ NASM”。
2. 在解决方案资源管理器中,右键单击项目名称(最上面的父节点,即NASM)->构建自定义项。 弹出窗口将带有nasm以及masm和lc(如果您没有在Build Customization目录中复制目标文件和
属性文件,则不会出现,如前所述,并且您必须使用“查找现有文件...”来手动找到该弹出窗口。 ”)。选中nasm复选框,然后单击确定。
3. 再次在解决方案资源管理器中,右键单击源文件->添加->新建项目->选择C ++或头文件,输入文件名(Nasm),然后输入扩展名.asm (即Nasm.asm) 然后单击添加。 再做一次以添加
Called.asm。
4. 打开属性页(通过解决方案资源管理器->右键单击NASM->属性,或在菜单中选择NASM并选择Alt + Enter或Project-> Properties。
5. 在“配置属性”类别中选择“链接器”。 展开它,然后单击输入。 在“其他依赖关系”中,再添加一个库“ libcmt.lib;”。 您也可以添加其他。 这只是一个C运行时(CRT)库。
6. 现在,单击左窗格中的“高级”选项。 是的 !!!! 当然,我们正在计划一个混乱。 在这个时代,我认为没有人会学习印刷黑白“ Hello World”的程序集。 因此,将“数据执行保护”设置为“否”,
以便我们可以使指向数据部分的指令指针退回。
很好,设置完成后,将以下代码添加到Nasm.asm和Called.asm 。
纳斯
Nasm.asm
extern _printf ; the C function, to be called
extern _getchar
extern _extfun
SECTION .data ; Data section, initialized variables .: dd 5 ; int a=5;
fmt: db "a=%d, eax=%d", 10, 0 ; The printf format, "\n",'0'
msg: db "We are now executing in data area :) With DEP disabled",10,0
Codev:
push ebp ; set up stack frame
mov ebp,esp
push dword msg ; address of ctrl string
call _printf ; Call C function
add esp, 4 ; pop stack 1 push times 4 bytes
call _extfun
mov esp, ebp ; takedown stack frame
pop ebp ; same as "leave" op
mov eax,0 ; normal, no error, return value
ret
SECTION .text ; Code section. .global _main ; the standard gcc entry point
_main: ; the program label for the entry point
push ebp ; set up stack frame
mov ebp,esp
mov eax, [a] ; put a from memory and store into register
add eax, 2 ; a+2
push eax ; value of a+2
push dword [a] ; value of variable a
push dword fmt ; address of ctrl string
call _printf ; Call C function
add esp, 12 ; pop stack 3 push times 4 bytes
mov esp, ebp ; takedown stack frame
pop ebp ; same as "leave" op
mov eax,0 ; normal, no error, return value
call Codev ;Time to break into data section
call _getchar ;let us C ;)
ret ; return
Called.asm
extern _printf
SECTION .data ; Data section, initialized variables .ewmsg: db "We are now from data section into an external function ;) ",10,0
global _extfun ; the standard gcc entry point
_extfun: ; the program label for the entry point
push ebp ; set up stack frame
mov ebp,esp
push dword newmsg ; address of ctrl string
call _printf ; Call C function
add esp, 4 ; pop stack 1 push times 4 bytes
mov esp, ebp ; takedown stack frame
pop ebp ; same as "leave" op
xor eax,eax ; normal, no error, return value
ret