VS2012 under X64 platform embedded assembler program

Source: Internet
Author: User


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?
  1. #include <iostream>   
  2. using   namespace std;
  3. int Add (int NUM1, int num2)
  4. {
  5. __asm
  6. {
  7. MOV eax,num1
  8. MOV ecx,num2
  9. Add EAX,ECX
  10. }
  11. }
  12. void Main ()
  13. {
  14. int  a = ADD (2,3);
  15. cout<< a <<endl;
  16. System ("pause");
  17. }
#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?
    1. . code  
    2.   
    3. Int_3 proc   
    4.         mov eax, 1234   ; return 1234  
    5.          ret  
    6. int_3 endp  
    7.    
    8.   
    9. my_test proc  
    10.         MOV EAX, 23 ; return 23  
    11.         ret  
    12. my_test endp  
    13.   
    14. 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?
  1. #ifndef __asmcode_h   
  2. #define __ASMCODE_H   
  3. extern "C"
  4. {
  5. int  _stdcall int_3 ();
  6. int  _stdcall my_test ();
  7. }
  8. #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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.