How to use the inline assembler in C + + + [English version]

Source: Internet
Author: User
Tags comments expression variables

Inline Assemblerthe compiler includes a powerful Inline. With it, assembly language instructions can is used directly in C and C + + source programs without requiring a separate ass Embler program. Assembly language enables optimizing critical functions, interfacing to the BIOS, operating system and special hardware, a ND 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 Execu TES as quickly as possible provide a interface to functions compiled with another compiler acess of the 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 into any of the three ways.

The example shows ASM followed simply by a 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: the asm mov AH,2 asm mov DL,7 asm int 21H three Previ ous examples generate identical code. But enclosing assembly language in braces, as in the second example, has the advantage of setting the assembly language of F 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 is one line long after expansion.

The

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 a "ASM block." Unlike C + + blocks, ASM blocks does not affect the scope of variables. Restrictions on using C and C + + in a ASM Blockan ASM block can use the following C and C + + language Elements:symbols, in cluding 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 is 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: you digit {hex_digit} ('H'| 'h') cannot use hexadecimal constants if the-a (for ANSI compatibility) option is used. C and C + + operators in a ASM Blockan ASM block cannot with 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 as them assembly language. 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 t o any adjacent operand.

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

Assembly language in a ASM Statementin common with other assemblers, the inline assembler accepts any instruction this is Legal in MASM. The following are some of the assembly language features of the ASM Statement:expressions. Inline assembly code can use the any MASM expression. A MASM expression is any combination the 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, 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 ' not ' a macro assembler and does not supp ORT MASM macro directives. Restrictions on C and C + + Symbolsan ASM statement can reference any C or C + + variable name, function, or label in SC Ope, provided those names are not symbolic constants. However, your cannot call a C + + MembeR function from within an ASM statement.

Prototype the functions referenced in a 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 blocks must not have the same spelling as a 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.

Related Article

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.