VC inline assembly

Source: Internet
Author: User
Tags emit intel pentium
I. Advantages
Inline assembly can be used to embed assembly language instructions in C/C ++ code without additional assembly and connection steps. In Visual C ++, inline assembly is a built-in compiler, so you do not need to configure an Independent Assembly tool such as MASM. Here, we will take Visual Studio. NET 2003 as the background to introduce the knowledge of using inner links in Visual C ++ (if it is an earlier version, there may be some discrepancies ).

Inline assembly code can use C/C ++ variables and functions, so it can be easily integrated into C/C ++ code. It can do some tasks that are very cumbersome or impossible to use C/C ++ alone.

The purposes of inline assembly include:

* Compile specific functions in assembly language;
* Write code with high speed requirements;
* Directly access the hardware in the device driver;
* Write the initialization and end code of the naked function.

Ii. Keywords

The _ ASM keyword is used to use inline assembly. It can appear in any place where C/C ++ statements are allowed to appear. Let's look at some examples:

* Simple _ ASM block:

_ ASM
{
MoV Al, 2
MoV dx, 0xd007
Out Al, DX
}

* Add the _ ASM keyword before each Assembly command:

_ ASM mov Al, 2
_ ASM mov dx, 0xd007
_ ASM out Al, DX

* Because the _ ASM keyword is a statement separator, multiple Assembly commands can be placed on the same line:

_ ASM mov Al, 2 _ ASM mov dx, 0xd007 _ ASM out Al, DX

Obviously, the first method is very consistent with the C/C ++ style, and the Assembly Code and C/C ++ code are clearly separated, the _ ASM keyword is also avoided, so the first method is recommended.

Unlike "{}" in C/C ++, "{}" in __asm blocks does not affect the scope of C/C ++ variables. At the same time, the __asm block can be nested, and nesting does not affect the scope of the variable.

To be compatible with earlier versions of Visual C ++, _ ASM and _ ASM have the same meaning. In addition, Visual C ++ supports the Standard C ++ ASM keyword, but it does not generate any instructions. Its function is limited to so that the compiler will not produce compilation errors. To use inline assembly, you must use the _ ASM keyword instead of the ASM keyword.

Iii. Assembly Language

1. Instruction Set

Inline assembly supports all commands of Intel Pentium 4 and AMD athlon. More commands for other processors can be created through the _ emit pseudocommand (see the description of _ emit pseudocommands below ).

2. MASM expression

In inline assembly code, all MASM expressions can be used. (a masm expression is a combination of operators and operands used to calculate a value or an address ).

3. Data Indicators and operators

Although the data types and objects of C/C ++ can be used in the _ ASM block, it cannot use MASM indicators and operators to define data objects. Specifically, the definition indicators (dB, DW, DD, DQ, DT, and DF) in the masm are not allowed in the ASM block, and the DUP and this operators are not allowed. The structures and records in the MASM are no longer valid. The inline assembly does not accept struc, record, width, or mask.

4. Even and align indicators

Although inline assembly does not support most MASM indicators, it supports even and align. When necessary, add the NOP command (null operation) to the Assembly Code to align the labels to specific boundaries. In this way, some processors can get commands more efficiently.

5. MASM macro indicator

Inline assembly is not a macro assembly and cannot use MASM macro indicators (macro, rept, IRC, IRP, and endm) and macro operators (<> ,! , &, %, And. type ).

6. Section

You must use registers instead of names to specify segments (the segment name "_ text" is invalid ). In addition, the CIDR block must be explicitly stated, for example, ES: [EBX].

7. Type and variable size

In inline assembly, you can use length, size, and type to obtain the large numbers of C/C ++ variables and types? ?

* The length operator is used to obtain the number of elements in the array in C/C ++ (if it is not an array, the result is 1 ).
* The size operator can obtain the size of the C/C ++ variable (the size of a variable is the product of length and type ).
* The type operator can return the C/C ++ type and variable size (if the variable is an array, it returns the size of a single element in the array ).

For example, an 8-dimensional integer variable is defined in the program:

Int iarray [8];

Below are the values of iarray and its elements obtained in C and Assembly expressions:

_ Asm c size

Length iarray sizeof (iarray)/sizeof (iarray [0]) 8
Size iarray sizeof (iarray) 32
Type iarray sizeof (iarray [0]) 4

8. Notes

The comments in the assembly language can be used in inline assembly, that is, ";". For example:

_ ASM mov eax, offset pbbuff; load address of pbbuff

The C/C ++ Macro will be moved to a logic line. To avoid confusion caused by the use of assembly language annotations in macros, inline assembly can also use C/C ++-style annotations.

9. _ emit pseudocommand

_ Emit pseudo commands are equivalent to DB in MASM, but _ emit can only define one byte in the current Code segment (. Text Segment) at a time. For example:

_ ASM
{
JMP _ codelabel

_ Emit 0x00; defines the data mixed in the code segment
_ Emit 0x01

_ Codelabel:; here is the code
_ Emit 0x90; NOP command
}

10. Register usage

Generally, it cannot be assumed that a register has a known value at the beginning of the _ ASM block. The register value cannot be guaranteed to be retained from the _ ASM block to another _ ASM block.

If a function is declared as _ fastcall, its parameters are passed through registers rather than stacks. This will cause a problem with the _ ASM block, because the function cannot be informed of which parameter is in which register. If the function receives the parameters in eax and immediately stores a value in eax, the original parameters will be lost. In addition, the ECX register must be retained for all functions declared as _ fastcall. To avoid the preceding conflicts, do not declare the _ fastcall call Method for functions containing the _ ASM block.

* Tip: if you use the eax, EBX, ECx, EDX, ESI, and EDI registers, you do not need to save them. However, if you use ds, SS, SP, BP, and flag registers, you should use push to save these registers.

* Tip: if the direction flag for STD and CLD is changed in the program, it must be restored to the original value.

4. Use the C/C ++ Element

1. Available C/C ++ Elements

C/C ++ and assembly languages can be used together. In inline assembly, C/C ++ variables and many other C/C ++ elements can be used, including:

* Symbol, including the label, variable, and function name;
* Constants, including symbolic constants and enumeration members;
* Macro definition and preprocessing indicator;
* Annotations, including "/**/" and "//";
* Type name, including all valid MASM types;
* Typedef name, usually using the PTR and type operators, or using the specified structure or enumeration members.

In inline assembly, C/C ++ or assembly language base notation can be used. For example, 0x100 and 100 h are equal.

2. Use Operators

Inline assembly cannot use C/C ++ operators such as <. However, operators common to C/C ++ and MASM (such as the "*" and "[]" Operators) are considered to be operators in assembly languages and can be used. For example:

Int iarray [10];

_ ASM mov iarray [6], BX; store BX at iarray + 6 (not scaled)
Iarray [6] = 0; // store 0 at iarray + 12 (scaled)

* Tip: in inline assembly, you can use the type operator to make it consistent with C/C ++. For example, the following two statements are the same:

_ ASM mov iarray [6 * type int], 0; store 0 at iarray + 12
Iarray [6] = 0; // store 0 at iarray + 12

3. Use the C/C ++ symbol

In the _ ASM block, you can reference all the C/C ++ symbols in the scope, including the variable name, function name, and label. However, you cannot access member functions of the C ++ class.

The following are some restrictions on using the C/C ++ symbol in inline assembly:

* Each assembly statement can contain only one C/C ++ symbol. In an assembly instruction, multiple symbols can only appear in length, type, or size expressions.
* The referenced function in the _ ASM block must be declared first. Otherwise, the compiler cannot distinguish the function name and label in the _ ASM block.
* The _ ASM block cannot contain the C/C ++ characters reserved for MASM (Case Insensitive ). MASM reserved words include command names (such as push) and register names (such as Esi.
* The _ ASM block cannot identify the structure and Union tags.

4. Access Data in C/C ++

One of the great conveniences of inline assembly is that it can reference C/C ++ variables by name. For example, if the C/C ++ variable Ivar is within the scope of its function:

_ ASM mov eax, Ivar; stores the value of Ivar in eax

If the class, structure, or enumeration member in C/C ++ has a unique name, the _ ASM block can only be accessed by the member name (omitted ". "variable name or typedef name before the operator ). However, if the member is not unique, you must add the variable name or typedef name before the "." operator. For example, the following two structures have the member variable samename:

Struct first_type
{
Char * pszweasel;
Int samename;
};

Struct second_type
{
Int iwonton;
Long samename;
};

If the variables are declared as follows:

Struct first_type fttest;
Struct second_type sttemp;

The variable name must be used for all references to the samename Member, because samename is not unique. In addition, because the preceding pszweasel variable has a unique name, you can reference it only by using its member name:

_ ASM
{
MoV EBX, offset fttest
MoV ECx, [EBX] fttest. samename; "fttest" must be used"
MoV ESI, [EBX]. pszweasel; "fttest" can be omitted"
}

* Tip: omitting the variable name is only for the convenience of writing code, and the generated Assembly commands are the same.

5. Compile functions with inner Confluence

If you use inner confluence to write a function, it is very easy to pass the parameter and return a value. Let's take a look at the example below and compare the functions written using independent assembly and inner Confluence:

; Powerasm. ASM
; Compute the power of an integer

Public getpowerasm
_ Text Segment word public 'code'
Getpowerasm proc
Push EBP; save EBP
MoV EBP, esp; move ESP into EBP so we can refer
; To arguments on the stack
MoV eax, [EBP + 4]; get first argument
MoV ECx, [EBP + 6]; get second argument
SHL eax, Cl; eax = eax * (2 ^ cl)
Pop EBP; restore EBP
RET; return with sum in eax
Getpowerasm endp
_ Text ends
End

C/C ++ functions generally use stacks to pass parameters. Therefore, the above functions need to access its parameters through stack positions (in MASM or some other compilation tools, you can also access stack parameters and local stack variables by name ).

The following program is compiled using inner links:

// Powerc. c

# Include <stdio. h>

Int getpowerc (INT inum, int ipower );

Int main ()
{
Printf ("3 times 2 to the power of 5 is % d/N", getpowerc (3, 5 ));
}

Int getpowerc (INT inum, int ipower)
{
_ ASM
{
MoV eax, inum; get first argument
MoV ECx, ipower; get second argument
SHL eax, Cl; eax = eax * (2 to the power of Cl)
}
// Return with result in eax
}

The getpowerc function compiled by inner consortium can reference its parameters by parameter names. Since the getpowerc function does not execute the Return Statement of C, the compiler will give a warning message. We can use # pragma warning to disable this warning.

One of the purposes of inline assembly is to compile the initialization and end code of the naked function. For general functions, the compiler will automatically help us generate function initialization (build parameter pointers and assign local variables, etc.) and end code (balance the stack and return a value ). With inline assembly, we can write clean functions by ourselves. Of course, at this time, we must do some work on function initialization and scanning. For example:

Void _ declspec (naked) mynakedfunction ()
{
// Naked functions must provide their own Prolog.
_ ASM
{
Push EBP
MoV ESP, EBP
Sub ESP, _ local_size
}

.
.
.

// And we must provide epilog.
_ ASM
{
Pop EBP
RET
}
}

6. Call the C/C ++ Function

The C/C ++ function declared as _ cdecl (default) in inline assembly must be cleared by the caller. The following is an example of calling a C/C ++ function:

# Include <stdio. h>

Char szformat [] = "% S % s/n ";
Char szhello [] = "hello ";
Char szworld [] = "world ";

Void main ()
{
_ ASM
{
MoV eax, offset szworld
Push eax
MoV eax, offset szhello
Push eax
MoV eax, offset szformat
Push eax
Call printf

// Three parameters are pushed into the stack. You need to adjust the stack after calling the function.
Add ESP, 12
}
}

* Tip: the parameters are pushed to the stack from right to left.

If you call the _ stdcall function, you do not need to clear the stack yourself. Because the returned command of this function is ret n, the stack is automatically cleared. Most Windows API functions use the _ stdcall call method (except for the number of wsprintf functions). The following is an example of calling the MessageBox function:

# Include <windows. h>

Tchar g_tszappname [] = text ("API test ");

Void main ()
{
Tchar tszhello [] = text ("Hello, world! ");

_ ASM
{
Push mb_ OK or mb_iconinformation
Push offset g_tszappname; offset is used for global variables.
Lea eax, tszhello; Lea
Push eax
Push 0
Call dword ptr [MessageBox]; note that this is not the call MessageBox, but the address of the relocated function.
}
}

* Tip: You can access C ++ member variables without restriction, but cannot access C ++ member functions.

7. Define the _ ASM block as a C/C ++ macro.

Using the C/C ++ macro, you can easily Insert the assembly code into the source code. However, you need to note thatMacroWill be extended to a logical line.
To avoid any problems, follow these rules to write macros:

* Enclose the _ ASM block with parentheses;
* Place the _ ASM keyword before each Assembly command;
* Use a classic C-style annotation ("/* Comment */"). Do not use an assembly-style annotation ("; comment ") or a single line of C/C ++ comments ("// comment ");

For example, a simple macro is defined below:

# Define portio _ ASM/
/* Port output *//
{/
_ ASM mov Al, 2/
_ ASM mov dx, 0xd007/
_ ASM out dx, Al/
}

At first glance, the following three _ ASM keywords seem redundant. They are actually needed, becauseMacroWill be extended into a single row:

_ ASM/* port output */{__ ASM mov Al, 2 _ ASM mov dx, 0xd007 _ ASM out dx, Al}

From the expanded code, we can see that the third and fourth _ ASM keywords are required (as statement delimiters ). In the _ ASM block, only the _ ASM keywords and line breaks are considered as statement delimiters.MacroA statement block is considered as a logical line, so the _ ASM keyword must be used before each command.

Parentheses are also required. If you omit it, the compiler will not know where the Assembly Code ends, and the C/C ++ statement after the ASM block looks to be considered an assembly instruction.

Likewise, due to macro expansion, compilation-style comments ("; Comment") and single-line C/C ++ comments ("// commen") may also cause errors. To avoid these errors, define the _ ASM BlockMacroUse the Classic C-style comments ("/* Comment */").

A macro written in the _ ASM block similar to a C/C ++ macro can also have parameters. Unlike the C/C ++ macro, The __asm macro cannot return a value. Therefore, this macro cannot be used as a C/C ++ expression.

Do not call macros of this type without any choice. For example, calling an assembly language macro in a function declared as _ fastcall may lead to unexpected results (see the preceding description ).

8. Jump

You can use goto in C/C ++ to jump to the labels in the _ ASM block, or switch to the labels inside or outside the _ ASM block. The labels in the _ ASM block are case-insensitive (commands, indicators, and so on are case-insensitive ). For example:

Void myfunction ()
{
Goto c_dest;/* correct */
Goto c_dest;/* Error */

Goto a_dest;/* correct */
Goto a_dest;/* correct */

_ ASM
{
JMP c_dest; correct
JMP c_dest; Error

JMP a_dest; correct
JMP a_dest; correct

A_dest:; _ ASM label
}

C_dest:/* C/C ++ Number */
Return;
}

Do not use the function name as the label. Otherwise, it will jump to the function for execution, rather than the label. For example, because exit is a C/C ++ function, the following redirection will not go to the exit label:

; Error: Use the function name as the label
JNE exit
.
.
.
Exit:
.
.
.

The dollar sign "$" is used to specify the current instruction position, which is often used in conditional jump. For example:

JNE $ + 5; the length of the following command is 5 bytes.
JMP _ label
NOP; $ + 5, jump to here
.
.
.
_ Label:
.
.
.

5. Use Independent Assembly in Visual C ++ Projects

Inline assembly code is not easy to transplant. If your program is intended to run on different types of machines (such as x86 and alpha), you may need to use specific machine code in different modules. At this time, you can use MASM (Microsoft Macro Assembler) Because MASM supports more convenient macro commands and data indicators.

Here is a brief introduction to the procedure of calling MASM to compile an Independent Assembly file in Visual Studio. NET 2003.

In the Visual C ++ project, add the. ASM file as required by MASM. In Solution Explorer, right-click the file and select the "properties" menu item. In the "properties" dialog box, click "Custom generation step" to set the following items:

Command Line: ml.exe/nologo/C/coff "-fo $ (intdir)/$ (inputname). OBJ" "$ (inputpath )"
Output: $ (intdir)/$ (inputname). OBJ

To generate debugging information, you can add the "/Zi" parameter to the command line, and generate. lst and. SBR files as needed.

If you want to call Windows API in an assembly file, you can download the masm32 package from the Internet (including the MASM Assembly Tool, a very complete Windows API header file/library file, a practical macro, and a large number of Win32 Assembly examples ). Correspondingly, the "/I X:/masm32/include" parameter should be added to the command line to specify the path of the Windows API Assembly header file (. Inc. Masm32 home page is: http://www.masm32.com, which can download the latest version of the masm32 package.

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.