If you want to write a very high-performance game code, use assembly language will undoubtedly you will be your best choice. However, it has developed the programming technology has evolved today, and you can use the assembly language to make it too uncomfortable. As a writer of a high-performance gaming program, you should use inline assembly. What is inline assembly inline assembly is what we usually say online, that is, add assembly language code directly in your C / C code. The benefits of online compilation Those who are accustomed to online assembly feel that it is very convenient. Compared with traditional assembly methods, we can completely avoid those cumbersome assembly and link steps, which can use C variable names and function names directly in assembly code. In this way, our assembly code can be easily combined with our C language procedures. In addition, since the assembled statement and C or C statement are mixed together, we will also achieve many of the original lighting c or c statements that cannot be worked. The use of online assembly is usually used, and the online assembly is used to write certain special functions, such as the portion of the optimization code has a very high-demanding requirement, directly accessing the hardware and device drivers and writing and exiting for a call code. Protective code, etc. On the one hand, online assembly is a special tool that does not support macro and other functions in macro. On the other hand, when you want to transplant the program code, these machines are more difficult to handle. How to use online assembly in the Visual C environment, using online assembly is a very easy thing: you just use a "_ASM {}" to enclose your assembly code. Let's take a simple example, this example is to implement a C function called mycode (). This function is very simple, it adds the value of the two input parameters to another global variable. Long Result; Void Mycode (long a, long * b) {_ASM {MOV EAX, A // EAX = A MOV EBX, B // EBX = B Add Eax, [EBX] // EAX = EAX [EBX] = EAX (* b) Mov Result, EAX // Result = EAX} This example is simple, but it demonstrates different processing methods for general variables and pointer variables while explaining the "_asm {}" usage. Visual C Inline assembly details I have summarized the following processing details according to Microsoft's incomplete information and their long-term practices, which are also expected to replenish and correct them.
In a 32-bit application, because all addressing offset addresses are 32-bit, the modification and use segment registers should be avoided at this time. The global variable is translated to establish an immediate address. The local variable in the function is translated into a stack offset address, such as [EBP ????], where EBP is set to ESP when the function is entered. Therefore, in the function of local variables, be careful not to destroy the contents of the EBP. The Visual C compiler does not make any optimization and changes on the code located in "_asm {}". The Visual C compiler will automatically discard the register of the "_ASM {}" code segment when optimized, so you don't need to add register protection and recovery code in the "_ASM {}" code segment. In addition, in a hurry, I thought of such a few, if you have any opinions, questions and suggestions, please be sure to come out. If you can make a further discussion, make this article to be perfect and upgraded, so more Seeing the people herein benefit. In addition, if you have a little assembly language foundation, but I haven't learned the 32-bit assembly language of the Intel X86 system. Please read another text: "32-bit assembly language of the Intel X86 system". If you define a structure, such as: typedef struct {int x;
Int Y;
} Mystruct; then there are more constants in Inline ASM: mystruct.x is 0
MySTURCT.Y is 4 They represent the offset of x, y in the structure. So you can use: mysturct * a;
MOV EBX, A;
MOV EAX, [EBX] mystruct.x;
(Eax is A-> X)
The above line can also be written into MOV Eax, [EBX MyStruct.x]; in addition, you can also create a function in the VC does not help you do your stack processing, you need yourself to get the input parameters, protect the register, even write RET, etc. Wait. This can be seen as a "pure" ASM function. The method is to join before the function definition: __declspec (naked) is OK. Align N can be used to align code in VC Inline ASM. If you need to fill in data, the VC will fill in some instructions that do not change any registers and flags (not necessarily NOP), but careful VC has bugs in this regard, my home page (Cloudwu.yeah.net) . In addition, in the Inline ASM of the VC, it is not as simple as we use DB in the BC's Inline ASM, which is inserted with _emit n into one byte. This is sometimes useful, such as the Pentium's CPUID Directive VC does not support, we can use: #define cpuid __asm _emit 0x0f __asm _emit 0xa2 instead of instead.