GCC inline assembly

Source: Internet
Author: User
Tags processing instruction
Sometimes in order to be efficient, sometimes in order to directly control the hardware, we have to write some modules directly in assembly language, and provide externally called interfaces to hide details. This is actually inline assembly. How to Use inline assembly? Let's take GCC as an example to get a glimpse of the mysteries!


I. Keywords
How can we let GCC know the embedded assembly in the code? With keywords! Let's look at the following example:

_ ASM _ volatile _ ("hlt ");

_ ASM _ indicates that the subsequent code is embedded assembly, and ASM is the alias of _ ASM. _ Volatile _ indicates that the compiler does not optimize the code. The subsequent commands are kept as they are, and volatile is its alias. The brackets contain Assembly commands.

Ii. Example Analysis
To use embedded assembly, you must first compile the assembly instruction template, then associate the C language expression with the instruction operand, And tell GCC What restrictions are imposed on these operations. Example:
 
_ ASM _ violate _ ("movl % 1, % 0": "= r" (result): "M" (input ));

Movl % 1, % 0 is the instruction template; % 0 and % 1 represent the operand of the instruction, which is called a placeholder. Embedded Assembly relies on them to correspond the C language expression to the instruction operand.

The instruction template is enclosed in parentheses in the C language expression. In this example, there are only two results and input, which correspond to the instruction operands % 0 and % 1 in the order of appearance; note the corresponding order: the first c expression corresponds to % 0; the second expression corresponds to % 1, and so on. The operands have up to 10, respectively % 0, % 1 .... % 9.

There is a string enclosed in quotation marks before each operand. The content of the string is a limitation or requirement on the operand. The limit string before result is = r, where = indicates that result is the output operand, and r indicates that result needs to be associated with a general register. First, the value of the operand is read into the register, then use the corresponding register in the instruction, instead of the result itself. Of course, after the instruction is executed, you need to store the value in the Register into the result variable. On the surface, the result is directly operated by the command, in fact, GCC implements implicit processing, so that we can write less commands. The R in front of input indicates that the expression needs to be put into a register first, and then used in the instruction for calculation.

The relationship between a c expression or variable and a register is automatically handled by GCC. We only need to use a restricted string to guide GCC in how to handle it. The limit character must match the instruction's requirements on the operand. Otherwise, the resulting assembly code may be wrong. You can change the two r values in the above example to M (M indicates that the operand is placed in the memory, rather than register), the result after compilation is:

Movl input, result

Obviously this is an invalid instruction, so the character string must match the instruction's requirements on the operand. For example, the instruction movl allows the Register to the register and counts immediately to the register, but does not allow the memory to the memory. Therefore, the two operands cannot use m as the delimiter at the same time.
The embedded assembly syntax is as follows:

_ ASM _ (Assembly statement template: Output part: input part: Destroy description part)

There are four parts in total: The Assembly statement template, the output part, the input part, and the destruction description part. Each part uses the ":" format. The Assembly statement template is required. The other three parts are optional, if the latter part is used, and the former part is empty, use the ":" format. The content of the corresponding part is empty. For example:

_ ASM _ volatile _ ("CLI": "Memory ")

What are the restrictions on these parts? This should be done in detail!

Iii. Syntax details
1. Assembly statement Template
An assembly statement template consists of a sequence of Assembly statements, separated by ";", "/N", or "/n/t. The operands in the command can reference the C language variables using placeholders. A maximum of 10 operands can be placeholders, with the following names: % 0, % 1 ,..., % 9. Operations expressed by placeholders in a command are always considered as long (4 bytes). However, operations on the command can be a word or byte, when the operand is used as a word or byte, the default value is low or low. The Byte operation can explicitly specify whether it is a low byte or a secondary byte. Insert a letter between % and serial number. B indicates low bytes, and h indicates high bytes, for example, % H1.

2. Output part
The output part describes the output operands. Different operands are separated by commas. each operand descriptor consists of a limited string and a C-language variable. The limited string of each output operand must contain "=", indicating that it is an output operand. For example:
 
_ ASM _ volatile _ ("pushfl; popl % 0; CLI": "= G" (x ))

The descriptor string represents the constraints on the variable, so that GCC can determine how to allocate registers based on these conditions, and how to generate the connection between the necessary code Processing Instruction operands and C expressions or C variables.

3. Input
The input part describes the input operands. Different operands are separated by commas (,). Each operand descriptor consists of a qualified string, a C-language expression, or a C-language variable. Example:

Example 1:
_ ASM _ volatile _ ("LIDT % 0": "M" (real_mode_idt ));

Example 2:
Static _ inline _ void _ set_bit (int nr, volatile void * ADDR)
{

_ ASM __(
"Btsl % 1, % 0"
: "= M" (ADDR)
: "Ir" (NR ));
}

The following example sets the NR bit of (* ADDR) to 1. The first placeholder % 0 corresponds to the C language variable ADDR, and the second placeholder % 1 corresponds to the C language variable Nr. Therefore, the Assembly statement code above is equivalent to the pseudo code below: btsl NR, ADDR. The two operands of this command cannot be all memory variables. Therefore, the qualified string of NR is specified as "ir ", associate NR with the immediate number or register, so that only ADDR is the memory variable in the two operands.

4. Restricted characters
There are many types of restricted characters, some of which are related to the specific architecture. Here, only the commonly used qualified characters and some common qualifiers that may be used in i386 are listed. They are used to indicate how the compiler processes the relationship between the subsequent C-language variables and the instruction operands.

 

Category

Qualifier

Description

General registers

""

Put input variables into eax

"B"

Put input variables in EBX

"C"

Put input variables in ECx

"D"

Put input variables in edX

"S"

Put input variables into ESI

"D"

Put input variables into EDI

"Q"

Put the input variables in one of eax, EBX, ECx, and EDX.

"R"

Put the input variables into the General registers, that is, one of eax, EBX, ECx, EDX, ESI, and EDI.

""

Combine eax and EDX into a 64-bit register (use long longs)

Memory

"M"

Memory variable

"O"

The operand is a memory variable, but its addressing method is offset type, that is, base address addressing

"V"

The operand is a memory variable, but the addressing mode is not an offset type.

""

The operand is a memory variable, but the addressing mode is auto increment.

"P"

The operand is a valid memory address (pointer)

Register or memory

"G"

Put the input variable into one of eax, EBX, ECx, and EDX, or as the memory variable

"X"

Operands can be of any type

Instant count

"I"

Immediate number between 0 and 31 (for 32-bit displacement instructions)

"J"

Immediate number between 0 and 63 (for 64-bit displacement instructions)

"N"

Immediate number between 0 and 25 5 (used for out commands)

"I"

Instant count

"N"

Instant count. If some systems do not support instant count other than words, use "N" instead of "I"

Match

"0"

It indicates that the operands restricted by it match a specified operand.

"1 "...

That is, the specified operand, for example, "0"

"9"

Describe the "% 1" operations, then "% 1" references the "% 0" operations, note the difference between 0-9 as the qualifier and "% 0"-"% 9" in the command. The former describes the operands, and the latter indicates the operands.

&

The output operations cannot use registers that are the same as the input operations.

Operand type

"="

The operands are only written in the instruction (output operands)

"+"

Operands are read/write type in the instruction (input/output operands)

Floating Point Number

"F"

Floating point register

"T"

First floating-point register

"U"

Second floating point register

"G"

Standard 80387 floating point constant

%

This operand can exchange positions with the next operand. For example, the two operands of addl can exchange order (of course neither of them can be an immediate number)

#

Partial comments. All letters from this character to the comma after it are ignored.

*

Indicates that if a register is selected, the subsequent letters are ignored.

5. Damage description
The destroy descriptor is used to notify the Compiler which registers or memory are used. It is composed of comma-separated strings. Each string describes a situation, generally the register name; in addition to registers, there is also "Memory ". For example, "% eax", "% EBX", and "Memory.

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.