WEB开发网
开发学院软件开发汇编语言 如何在C/C++使用内联汇编[英文版] 阅读

如何在C/C++使用内联汇编[英文版]

 2010-01-10 09:37:23 来源:WEB开发网   
核心提示:Inline AssemblerThe compiler includes a powerful inline assembler. With it, assembly language instructions can be used directly in C and C++ source programs wit

Inline AssemblerThe compiler includes a powerful inline assembler. With it, assembly language instructions can be used directly in C and C++ source programs without requiring a separate assembler program. Assembly language enables optimizing critical functions, interfacing to the BIOS, operating system and special hardware, and access capabilities of the processor that are not available from C++.

It supports both 16-bit and 32-bit code generation in all memory models.

What's in This ChapterBasic features of the inline assembler. The ASM statement and the ASM block. Using ASM registers. Calling C and C++ from assembly language. Registers and opcodes. Advantages of writing inline assembly language functionsUse assembly language functions to: Create a subroutine that executes as quickly as possible Provide an interface to functions compiled with another compiler Acess capabilities of the CPU and the native instruction set that are not available from C++ Interface to specialized hardware Write code where specific instruction selection and ordering is critical The asm StatementThe asm statement invokes the assembler. Use this statement wherever a C or C++ statement is legal. You can use asm in any of three ways.

The first example shows asm followed simply by an assembly instruction:

asm mov AH,2
asm mov DL,7
asm int 21H
The second example shows asm followed by a set of assembly instructions enclosed by braces. An empty set of braces may follow the directive.asm {
  mov AH,2
  mov DL,7
  int 21H
}
Because the asm statement is a statement separator, assembly instructions can appear on the same line:asm mov AH,2 asm mov DL,7 asm int 21HThe three previous examples generate identical code. But enclosing assembly language in braces, as in the second example, has the advantage of setting the assembly language off from the surrounding C++ code and avoids repeating the asm statement.

No assembler instruction can continue onto a second line. Use the form of the last example primarily for writing macros, which must be one line long after expansion.

NoteThe Digital Mars C++ asm statement emulates the Borland asm statement. The _asm and __asm statements emulate the Microsoft _asm and __asm statements. The ASM BlockA series of assembler instructions enclosed by braces following the asm keyword are called an "ASM block." Unlike C++ blocks, ASM blocks do not affect the scope of variables. Restrictions on using C and C++ in an ASM blockAn ASM block can use the following C and C++ language elements: Symbols, including labels, variables, and function names. Constants, including symbolic constants and enum members. Macros and preprocessor directives. Comments delimited by /**/ symbols or defined by // symbols. In Microsoft-compatible mode, semicolons (;) can also be used to delimit comments. Type names (where a MASM type would be legal). Type names, including declarations of structs and pointers. C-style type casts. NoteMicrosoft and Borland inline assemblers do not support type casts.

Inline assembly instructions within C or C++ statements can refer to C or C++ variables by name.

MASM-Style hexadecimal constantsSupport for MASM-style hexadecimal constants provides easy conversion of MASM-style source code. The constants take the form:digit {hex_digit} ('H'| 'h')You cannot use hexadecimal constants if the -A (for ANSI compatibility) option is used. C and C++ operators in an ASM blockAn ASM block cannot use operators specific to C and C++, such as the left-shift (<<) operator.

You can use operators common to C, C++, and MASM within an ASM block but interpret them as assembly language operators. C and C++ interpret brackets ([]) as enclosing array subscripts and scale them to the size of an array element. But within an asm statement, C and C++ interpret brackets as the MASM index operator, which adds an unscaled byte offset to any adjacent operand.

In Microsoft-compatible mode, the semicolon delimits comments, as in MASM.

Assembly language in an asm statementIn common with other assemblers, the inline assembler accepts any instruction that is legal in MASM. The following are some of the assembly language features of the asm statement: Expressions. Inline assembly code can use any MASM expression. A MASM expression is any combination of operands and operators that evaluates to a single value or address. Data directives and operators. An asm statement can define data objects in the code segment with the MASM directives DB, DW, and DQ. The MASM directives DT, DF, STRUC, and RECORD, and the operators DUP, THIS, WIDTH, and MASK are not accepted. Macros. An asm statement can use C preprocessor macros even though the inline assembler is not a macro assembler and does not support MASM macro directives. Other restrictions on C and C++ symbolsAn asm statement can reference any C or C++ variable name, function, or label in scope, provided those names are not symbolic constants. However, you cannot call a C++ member function from within an asm statement.

Prototype the functions referenced in an asm statement before using them in programs. This lets the compiler distinguish them from names and labels.

Each assembly language instruction can contain a single C or C++ symbol.

C or C++ symbols within an ASM block must not have the same spelling as an asm reserved word.

The inline assembler allows structure or union tags in asm statements, but only as qualifiers of references to members of the structure or union.

1 2 3 4 5  下一页

Tags:如何 使用 内联

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接