如何在C/C++使用內聯彙編[英文版]

來源:互聯網
上載者:User

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.