Ah, it's still the x86 Platform for translation last semester. The compilation is embedded in Linux, and the translation is very bad.

Source: Internet
Author: User

X86 Platform, Embedded Assembly in Linux

Bharata B. Rao (rbharata@in.ibm.com)
IBM Linux technology center, IBM software labs, India
March 2001

If you are a Linux kernel developer, you will find that you often write some functions related to the system structure or frequently optimize a piece of code, you will often use an assembly language embedded in the C language environment. let's take a look at the usage of Embedded Assembly in Linux.

Briefly describe the syntax of the GNU Compiler
The gnu c compiler in GCC and Linux uses the at&t syntax, which is listed below, but not all. It is only related to embedded assembly.

Register name
Add the prefix % before the register name. For example, if you want to use eax, use % eax.

Sequence of source and destination operands
The source operand is in front, and the destination operand is in the back. This is exactly the opposite of the Intel syntax.
For example, mov % eax and % EBX indicate uploading the content in eax to EBX.

Operand size
The instruction has the suffix B, W, and l, depending on whether the operand is byte, word or long. this is not mandatory. GCC will use the read operations to determine the suffix, but it is not difficult to add the suffix itself, which can increase the readability of the program, and can reduce the possibility of compiler errors.
Movb % Al, % BL transfer byte
Movw % ax, % BX transfers word
Movl % eax, % EBX transfers longword

Instant count
Add $
Movl $0 xFFFF, % eax will send the value 0xffff to eax

Indirect register addressing
When using registers for indirect addressing memory ()
Movb (% Esi), % Al will send a byte of the memory unit pointed by ESI to Al

Embedded Assembly
GCC provides structure (construct) "ASM" (note that the first letter is lowercase :) used to indicate embedded assembly, the following format
ASM (assembler template)
: The output operand is optional.
: The input operand is optional.
: Registers used are optional.
);
In this way, the assembler template contains Assembly commands. The input operands are variables and are used as input operands of Assembly commands. The output operands are also variables, output operations used as assembly commands (save output results)

An important aspect of Embedded Assembly is that it can operate and explicitly output to variables. Due to this feature, "ASM" is treated as an interface in the same language as C.

// A basic but important difference is that a simple Embedded Assembly only contains commands, and an extended Embedded Assembly contains operations. For example:

Basic Embedded Assembly
{
Int A = 10, B;
ASM ("movl % 1, % eax;
Movl % eax, % 0 ;"
: "= R" (B)/* output */
: "R" (a)/* input */
: "% Eax"/* occupied REGISTERS */
}
In this example, we use an assembly command to make the value of B equal to A. Pay attention to the following points:
"B" is the output operand, which is specified by % 0. "A" is the input operand, which is specified by % 1.
"R" is a constraint that specifies that the variables "a" and "B" are stored in the constraint ,, the limit parameter of the output operand is preceded by "=" to indicate that it is the output operand.
To use the Register "% eax" in the embedded assembly, you must add another %, which is % eax. The Embedded Assembly uses % 0, % 1 to specify the variable, any operations starting with % will be used as input and output operations, not registers.
"% Eax" after 3rd colons tells GCC that the % eax register has been specified in ASM, and GCC will not store other values using this register.
Movl % 1, % eax move the value of "a" to % eax, movl % eax, % 0 move the content of % eax to "B"
After this ASM completes, "B" will reflect the value change, because it is used as an output operand. In other words, the value of "B" Changes in ASM and is displayed outside ASM.

Now let's take a closer look.

Assembler Template
A assembler template is an assembly instruction (one or more) inserted into a C program. Each instruction must be enclosed in double quotation marks, or a group of instructions must be enclosed in double quotation marks, each Command must end with a delimiter. The available delimiter can be "/N" or ";". The operands are numbered % 0, % 1 ....

If you don't want the compiler to optimize your assembly code, you can use the "volatile" keyword to put it behind "ASM". If your program wants to be ANSI compatible, replace ASM and volatile with _ ASM _ and _ volatile _.

C-language variables can be directly embedded in assembly operations, which is very convenient

Each operand is defined by a restriction parameter enclosed in double quotation marks, followed by a variable expanded in parentheses. This is the form of "restriction parameter" (C variable) the main function of a restricted parameter is to determine the addressing mode of the operand (addressing mode)

You can use multiple operands in the input and output, separated by commas (,).

Occupied registers
If registers are used in embedded commands, we can tell GCC so that GCC will leave the specified registers for other purposes, but when the restriction parameter of the operand is specified, you don't need to talk about it again, such as "= B" (variable)
However, when commands use other registers, they are explicit or implicit (and these registers are not included in the input or output list ), at this time, it is necessary to clearly write the list in the register list, the list is listed after the 3rd colons, with the register name specified

As far as keywords are concerned, if the instructions modify the memory in some unpredictable fashion, and not
Explicitly, then the "Memory" keyword may be added to the list of clobbered registers. This tells GCC not to keep
Memory values cached in the register guest ss the instructions)

Operand restriction Parameters
The restriction parameters can have the following functions:
Whether the operand can be put in a register or in which register
Whether the operand is a memory address
Whether the operand is an immediate number

Below are several common restrictions on Parameter usage:
ASM ("movl % 32a, % 0/N": "= r" (cr3val ));
It means to put the variable cr3val into the register, copy the value of % 33rd to the register first, and then update the value in the register to the place where the variable cr3val is saved in the memory. because it is "= r", GCC can put the variable cr3val in any common register. If you want to specify a register, use the following method to specify
A % eax
B % EBX
C % ECx
D % edX
S % ESI
D % EDI
The usage is like this. ASM ("movl % 32a, % 0/N": "= B" (cr3val); specify % EBX

Memory operand constraint (m)
When the operands are in the memory, any operation is directly applied to the memory, instead of using registers. The register is generally used as a required command or used to make the program run faster,
("Sidt % 0/N": "M" (LOC); indicates that the input variable loc does not pass the Register

Mathing (DIGIT) Constraints
When a variable is used in input and output, "0", "1", "2"... (I think so)
ASM ("incl % 0": "= A" (VAR): "0" (VAR ));
Store VaR in the register % eax first, add 1 to the register, and then send it to the VaR

Common Embedded Assembly usage
Int main (void)
{
Int x = 10, Y;
ASM ("movl % 1, % eax;
Movl % eax, % 0 ;"
: "= R" (y) output variable
: "R" (x) input variable
: "% Eax ");
}
Here, the value of X is copied to Y, X, and Y, which are first put into the register. The Assembly Code will look like this:
Main:
Pushl % EBP
Movl % ESP, % EBP
Subl $8, % ESP
Movl $10,-4 (% EBP)
Movl-4 (% EBP), % edX/* x = 10 exists in % edX */
# APP/* ASM starts here */
Movl % edX, % eax/* x moved to % eax */
Movl % eax, % edX/* y are allocated to edX */
# No_app/* ASM ends here */
Movl % edX,-8 (% EBP)/* Y in stack is updated by EDX median */

GCC randomly allocates registers here because it is "r". In our example, it selects % edX to store X, and reads the value of X in % EDX, GCC assigned % edX to Y again

There seems to be another point behind ,,,,

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.