VS2012 in the Win32 platform compile time can be very good support of assembly language embedding. Create a console application and select an empty project. After the project is established, add a. cpp file. Write the following code in the CPP file:
[CPP]View PlainCopyPrint?
- #include <iostream>
- using namespace std;
- int Add (int NUM1, int num2)
- {
- __asm
- {
- MOV eax,num1
- MOV ecx,num2
- Add EAX,ECX
- }
- }
- void Main ()
- {
- int a = ADD (2,3);
- cout<< a <<endl;
- System ("pause");
- }
#include <iostream>using namespace Std;int Add (int num1, int num2) {__asm{mov Eax,num1mov ecx,num2add eax,ecx} }void Main () {int a = ADD (2,3);cout<< a <<endl;system ("pause");}
It can be found that the above code can be used normally, and then switch the project to x64 compilation mode, you will find the following error prompt:
Error C4235: nonstandard extension used: "__asm" keyword is not supported on this structure
This means that __asm assembly embedding is not supported in x64 compilation mode . From the information on the Internet , although it is not possible to embed the assembler segment directly, but you can put all the program segments into an ASM file to compile , and finally asm file compilation generated obj file and. cpp file compilation generated obj file linked together to generate an EXE file.
Create a new ASM file, where a file named Test.asm is created.
Write the following test code:
[CPP]View PlainCopyPrint?
- . code
-
- Int_3 proc
- mov eax, 1234 ; return 1234
- ret
- int_3 endp
-
-
- my_test proc
- MOV EAX, 23 ; return 23
- ret
- my_test endp
-
- end
. Codeint_3 Procmov EAX, 1234 ; return 1234retint_3 endpmy_test Procmov EAX, 23; return 23retmy_test Endpend
One of the two assembler functions, Int_3 and My_test, is in the preceding code snippet.
Then create a new. h file to declare the code in the assembler. A header file named Test.h is created here. Write the following declaration information:
[CPP]View PlainCopyPrint?
- #ifndef __asmcode_h
- #define __ASMCODE_H
- extern "C"
- {
- int _stdcall int_3 ();
- int _stdcall my_test ();
- }
- #endif
#ifndef __asmcode_h#define __asmcode_hextern "C" {int _stdcall int_3 (); int _stdcall my_test ();} #endif
include the above header file in the main.cpp, call the function in the Assembly, compile in x64 mode, and you will see that there is still an error. The following steps are key!!
right mouse button Test.asm file.
Go to Properties, configuration Properties, General , set exclude from Build to No, select item type as Custom build tool , Click Apply, OK .
At this point, you will find a more custom build tool.
Under Custom build Tools, select three items in the General Settings red box:
After setting up the application, OK, and then recompile the program, found that this time can be compiled successfully.
Click Debug. Errors may occur:
Unable to find debug information for "XXX.exe" or debug information mismatch. Binary is not generated using debug information
This is because the PDB debug file is not generated during the link generation, so you can modify the project properties to complete the following three items.
First open the menu item, Project Properties page
1, select Configuration Properties, Linker---Debug-build debugging information instead of
2, select Configuration Properties->c/c++, general, debug Information Format change to the program database for Edit and Continue (/zi)
3. Select configuration attribute->c/c++, optimization to disable (/od)
This is the normal use of the assembler in the VS2012 x64 mode, according to the online information,X64 under the assembly instructions and 32-bit assembly instructions will be slightly different , in-depth use of the time also need to query X64 compilation manual.
Resources:
http://bbs.pediy.com/showthread.PHP?p=1318183 a complete scenario for driving the use of the ASM keyword under VS2012 x64
Http://www.cnblogs.com/cryinstall/archive/2011/04/24/2280843.html in VS2008, but "no debug information is used to generate binary files" when debugging
Http://blog.sina.com.cn/s/blog_6b849dd00100xcgj.html x86 platform to x64 platform about inline assembly no longer supported resolution
JPG change rar
VS2012 under X64 platform embedded assembler program