Small tutorial on Embedded Assembly syntax format used by Gcc

Source: Internet
Author: User
A small tutorial on Embedded Assembly syntax format used by Gcc-Linux general technology-Linux programming and kernel information. The following is a detailed description. Small tutorial on Embedded Assembly syntax format used by Gcc
This article describes the Embedded Assembly syntax in detail from the basic syntax, the format of embedded assembly, and the extended Embedded Assembly format. It should be noted that gcc uses at&t's Assembly format.

Basic syntax
There are several differences in syntax.
★Register naming principles
At&t: % eax intel: eax
★Source/destination operand order
At&t: movl % eax, % ebx intel: mov ebx, eax
★Constant/immediate Number Format
At&t: movl $ _ value, % ebx intel: mov eax, _ value
Put the _ value address in the eax register
At&t: movl $ 0xd00d, % ebx intel: mov ebx, 0xd00d
★Operand length identifier
At&t: movw % ax, % bx intel: mov bx, ax
★Addressing Mode
At&t: immed32 (basepointer, indexpointer, indexscale)
Intel: [basepointer indexpointer * indexscale imm32)
Linux uses a 32-bit linear address in protection mode. Therefore, you do not need to consider the segment: offset issue when calculating the address. The address in the above formula should be:
Imm32 basepointer indexpointer * indexscale
The following are some examples:
★Direct addressing
At&t: _ booga;
_ Booga is a global c variable. Note that adding $ indicates address reference, and not adding $ indicates value reference.
Note: For local variables, You can reference them through the stack pointer.
Intel: [_ booga]
★Register indirect addressing
At&t: (% eax)
Intel: [eax]
★Address Change Addressing
At&t: _ variable (% eax)
Intel: [eax _ variable]
At&t: _ array (, % eax, 4)
Intel: [eax * 4 _ array]
At&t: _ array (% ebx, % eax, 8)
Intel: [ebx eax * 8 _ array]

2. Basic Embedded Assembly
The basic embedded assembly is very simple, generally in the following format
Asm (statements );
Example: asm (nop); asm (cli );
Asm and _ asm _ are exactly the same.
If there are multiple lines of assembly, nt must be added for each line, for example:
Asm (pushl % eaxnt
Movl $0, % eaxnt
Popl % eax );
In fact, gcc needs to print the content of asm (...) to the Assembly file when processing the assembly, so it is necessary to control the format. For example:
Asm (movl % eax, % ebx );
Asm (xorl % ebx, % edx );
Asm (movl $0, _ booga );
In the above example, we changed the values of edx and ebx in the Assembly, but due to the special processing method of gcc, the Assembly file is first formed and then handed over to the gas for assembly, therefore, gas does not know that we have changed the values of edx and ebx. If the context of the program needs to be saved by edx or ebx, this will cause serious consequences. the same problem exists for Variable _ booga. to solve this problem, we need to use the extended in-row Assembly syntax.

3-extended in-line assembly
The extended in-row assembly is similar to watcom.
The basic format is:
Asm (statements: output_regs: input_regs: clobbered_regs );
Clobbered_regs indicates the changed register.
The following is an example (for convenience, I use global variables ):
Int count = 1;
Int value = 1;
Int buf [10];
Void main ()
{
Asm (
Cld nt
Rep nt
Stosl
:
: C (count), a (value), d (buf [0])
: % Ecx, % edi );
}
The compilation code is as follows:
Movl count, % ecx
Movl value, % eax
Movl buf, % edi
# App
Cld
Rep
Stosl
# No_app
Cld, rep, and stos do not need to be explained.
The functions of these statements are to write count value to buf.
The statement after the colon indicates the input, output, and changed registers.
With a colon, the compiler will know what registers your commands need and change,
This allows you to optimize register allocation.
The symbol c (count) indicates to put the value of count into the ecx register.
Similar:
A eax
B ebx
C ecx
D edx
S esi
D edi
I constant value, (0-31)
Q, r registers dynamically allocated
G eax, ebx, ecx, edx, or memory variable
A combines eax and edx into a 64-bit register (use long longs)
We can also let gcc select the appropriate register.
Example:
Asm (leal (% 1, % 1), % 0
: = R (x)
: 0 (x ));
This code implements fast multiplication of 5 x.
The compilation code is as follows:
Movl x, % eax
# App
Leal (% eax, % eax, 4), % eax
# No_app
Movl % eax, x
Notes:
1. Use q to indicate that the compiler allocates registers from eax, ebx, ecx, and edx. Use r to indicate that the compiler allocates registers from eax, ebx, ecx, edx, esi, and edi.
2. We don't have to put the registers allocated by the compiler into the changed register list, because the registers have remembered them.
3. = indicates the output register, which must be used in this way.
4. Number % n usage: registers represented by numbers are mapped to r or q requests in the order of appearance and left-to-right
If we want to reuse the r or q request registers, we can use them.
5. If fixed registers are forcibly used, for example, ebx instead of % 1
Asm (leal (% ebx, % ebx, 4), % 0
: = R (x)
: 0 (x ));
Note that two % s should be used, because the syntax of one % has been used by % n.

The following describes the letter 4854-4855 problem:
1. What are the special meanings of variable underscores and double underscores? An underscore indicates a global variable, but it doesn't matter if it is not added in gcc.
2. What does the above definition mean when a meeting is called as follows?
# Define _ syscall1 (type, name, type1, arg1)
Type name (type1 arg1)
{
Long _ res;
/* _ Res should be a global variable */
_ Asm _ volatile (int $0x80
/* Volatile means that optimization is not allowed, so that the compiler compiles Code strictly according to your Assembly Code */
: = A (_ res)
/* Generate code movl % eax, _ res */
: 0 (_ nr _ # name), B (long) (arg1 )));
/* If I remember correctly, here # indicates two macro exhibitions.
Replace name with the actual system call name, and then expand _ nr.
Then put the expanded constant into eax, And put arg1 into ebx */
If (_ res> = 0)
Return (type) _ res;
Errno =-_ res;
Return-1;
}


Posted on angel tears read (2311) Comment (0) EDIT alarm

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.