x86 platform to x64 platform about inline assembly no longer supported resolution

Source: Internet
Author: User

x86 platform to x64 platform about inline assembly no longer supported resolution

2011/08/25 keep A record of the problems and solutions you have encountered, and keep them for backup!

Tools:VS2005 compiler : cl.exe (X86 c/C + +) Ml64.exe (X64 ASM64)

Premise: TheX86 assembler is embedded within the function

Under the X86 platform, it is easy to embed the assembly code in C + + code, call it "inline assembly", use "__asm{}" syntax, it is relatively simple, here is not introduced. When you are under the X86 platform, the requirements for performance and speed need to be assembled inline in the C + + code. And when you finally achieved these in the X86 platform, found to go to the X64 platform below, how to do (not to say in X64 run X86 program, because it is certainly possible, but to say from X86 Project transferred to X64 project)? Can be compatible, right? Let's try it again.

Give it a try:



See the Debug and Win32 words, starting from here to change.

The lower triangle at the right end of the drop-down Win32 , showing as. As I have set it up, there will be x64 words. If not previously set, then the default is no x64 typeface. To appear x64 platform, please set: Click "Configuration Manager", a series of operations are shown in the diagram:



Note : In general, it is programmed under the X86 platform, at least I am. So when the VS2005 is installed by default, the EXE that compiles the connection generation X64 platform under the X86 platform is not supported. If you really want to compile the connection under X86 to generate X64 platform EXE, like me. Then locate setup when you install VS2005 . EXE, double-click on it, select the "Add or remove" option (name is not OK, but the first one), then tick the X64 Word (I do not remember it), and then wait for SETUP. EXE to help you to install this item just fine. Next look at Figure ~




Note : When you first "new", the "X64" option appears under "New Platform (P)", which does not appear because I have already set it up.




They said they had a try, and it was the result. Environment settings. The following compilation connection (F7), ah Oh! Error, and a lot of error hints! Oh, Mom!!!

One of the classic errors is:

errorC4235: nonstandard extension used : "__asm" keyword is not supported on this structure

It's finished! Find a way.

Online solution, said vs2005x64 platform no longer support inline assembly! But considering the time, and not allowed to change the compiler, how to do? Continue online Search Solution ... There it is. Write compilation ASM files Separately, compile builds . OBJ file. That is, it is not possible to embed (inline) the assembly directly in the C + + code, but to put the original inline assembly in a separate assembly file. In other words, part of the assembler code, part of C + + code. The build is then compiled from the standalone assembly . The OBJ file is attached to the C + + main project ... It seems so hard oh, nothing, everything is difficult at the beginning ~

All right, if you don't understand, look down first!

There are some head-big operations down there.

Remember, I was under the X86 platform to compile the program under the connection X64 Platform Oh! The main project is a program that runs under the X64 platform. The two parts mentioned above: the assembler and the C + + are of course under the X64 platform ( sure ).

- bit assembly, dumbfounded, all the head big, suddenly jump to the level , there is no mistake?? No, the four-digit assembly code should not make too much of a change. In fact, that's true.

First, the original inline assembly code is not converted to a separate assembly file. The question is, how to compile a single assembly file? With VS2005 integration Environment to compile a separate assembly file, yes, that is, do not go back to the DOS interface to knock command line, this will be dead, and inconvenient!! Next follow me to do:

Use VS2005 to create a regular empty project. And then the self-improvement of the number of the compilation (plainly, most of the instructions) to join the project,F7 compiled connection seems to have a reaction, but it is false! By default, the IDE does not recognize the assembly. What to do?

  

  

Click on "Custom Build rule":



The default is only the first two items, the last one I added myself. The first item, I don't know anything. The second item is the compiled compiler (ml.exe). But we're going to compile the A-bit compilation! No hurry, do as below:

Find this path "... \program Files\Microsoft Visual Studio8\vc\vcprojectdefaults",OK, in this directory, find Masm.rules. Next, copy and paste masm.rules, copy the file name "masm64.rules".

Back to the IDE, there are three items on it. Make a change to the last item, obviously, or the latter two are the same. Tick the third item and click on "Modify Rule File":



Click on "Modify Build rule":


Changed to blue selected text on the line, is simply added "" "inthe original ml behind, you know ~ all the way to determine, everything is done! If there is no syntax error in the file assembly, it will be generated . OBJ file now, haha! It's not so simple ... The main requirement is to write code that conforms to the requirements, rather than the previously simple inline in C + + code. About writing a separate assembly code file, depending on your original compilation foundation, this I have no words, on your own!!

OK , "Done",F7 The following tips:

1>------ Build started : project : ASM64, configuration : Debug x64------

1> being linked ...

1>link:fatal errorLNK1561: entry point must be defined

1> The build log is saved in thefile://f:\vc projects\visual Studio 2005projects\happyway\test\asm64\asm64\x64\debug\ BuildLog.htm"

1>asm64-1 a mistake, a warning.

========== Build : 0 succeeded , 1 failed , 0 latest , 0 skipped ==========

I did not go to the entry point, because I do not need this, the entry point in the C + + main project that, assembly done, grammar! The compiler generated asm64.objis obtained.

Next, is another part of the main engineering code,C + + , stop pen, Hotan big head to eat ~

......

I X, Tian Big head put me pigeon, eat a person!

Note that theX86 assembler is embedded within the function, so the task of the above- mentioned compilation is to write the Function procedure (PROC).

Next, go back to the main project. In the main project to use the functions in the Assembly, then the assembly generated . The OBJ file comes in handy. The main project can be called . OBJ goes to use the functions in the assembly. The process is as follows:

"Project", "Properties", pops up:

Then, "Configuration Properties"," linker ", "input", "Additional dependencies" on the right, are populated with assembly files . OBJ, such as.



It then exposes the interfaces of the functions written in the assembly.

I was so exposed to my assembler function:

extern "C" int __stdcall   rgb2yuv_all (int bmpwidth , int bmpheight , unsigned char *videoref , unsigned char *yuvdata );

extern "C" int __stdcall   rgb2yuv_block (int bmpwidth , int bmpheight , unsigned char *videoref , unsigned char *yuvdata , int largebmpwidth );

If all goes well, the main project can use the above function, compile the connection is not a problem, but the evil of the operation of the wrong, it depends on your debugging! At this point, almost, from X86 to X64, from the inline assembly to a separate assembly file, to solve the X64 does not support inline assembly problems, hehe.

However, the more laborious and time-consuming is the compilation of code modification and writing,C + + calls assembly function process encountered a variety of problems, but as long as the efforts to add a certain basis of the situation is to make a lot of problems!

If you are interested, or just as I have encountered such a problem, the following example code.

1. ASM Files

. Data

; Data Segment


. Code
FUNC proc
MOV eax,1234
Ret
FUNC ENDP
End

2. test the VS2005 console Project

#include <stdio.h>

extern "C" int __stdcall FUNC ();

int main (int argc, wchar* argv[])

{

int nret = FUNC ();

printf ("%d\n", nret);

System ("pause");   

return 0;

}

This process, there are a lot of problems to solve and think about the place, which requires your basic skills, it is not here to say, do it all know! Believe in yourself!

JPG change rar

x86 platform to x64 platform about inline assembly no longer supported resolution

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.