nasm vs汇编混编

Using Assembly from Microsoft Visual C++ 2010

Dr. Orion Lawlor, 2011-10-12

Step 1: Write C++ Code

First, hit File - New Project to make a standard C++ console application:

Where to click in Visual C++ GUI

Next, remove the Microsoft junk, like the procompiled header.

Where to click in Visual C++ GUI

Next, write some test C++ code. It helps to run at this point, to make sure everything works.

Where to click in Visual C++ GUI

Step 2: Write assembly code

Now write an assembly language source file with these critical pieces:

  • Give the file the .S extension, the standard extension for assembly code.
  • Start the file with "section .text", so the code is executable.
  • Make the function "global", so the linker can see it.
  • Make the function name begin with an underscore--for some reason Windows compilers stick an underscore on the front of every function name.
Finally, right click on your new file, and hit properties.

Where to click in Visual C++ GUIVisual doesn't know how to compile the .S file by default, so it ignores it: "Does not participate in build." Change this to "Custom Build Tool": Where to click in Visual C++ GUI Now you need to download and install NASM(or YASM, etc.) You then give Visual Studio the Command Line needed to run your assembler, just like at the DOS prompt:

	"\Program Files\nasm\nasm" -f win32  asmstuff.S
The assembler will output a .obj file, which you list under "Outputs".

Where to click in Visual C++ GUI

Build the project, and you should be able to call assembly functions from your C++, and vice versa!  Don't forget extern "C" from C++!

Where to click in Visual C++ GUI

Simple C++ code:

#include <iostream>
extern "C" int foo(void); // written in assembly!

int main() {
	std::cout<<"Foo returns "<<foo()<<"\n";
	system("pause");
	return 0;
}
Assembly code:
section .text ; makes this executable
global _foo ; makes this visible to linker
_foo:
	mov eax,7 ; just return a constant
	ret
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值