GCC Embedded Assembly language

Source: Internet
Author: User
Tags processing instruction
GCC Embedded assembly language Author: Xiaowenpeng Linjiang: Yang Xiaohua The vast majority of Linux programmers have only previously contacted the assembly language of Dos/windows, and these assembly codes are Intel-style. But in Unix and Linux systems, more or at&t format, the two in the syntax format is very different. Introduction to compiling basic grammarIn the AT&T assembly format, the register name is prefixed with '% ', whereas in the Intel assembly format, the register name does not need to be prefixed. For example:
At&t format Intel format
PUSHL%eax Push EAX
In the AT&T assembly format, the ' $ ' prefix is used to denote an immediate operand, whereas in the Intel assembly format, the immediate number representation is not prefixed with any prefix. For example:
At&t format Intel format
PUSHL $ Push 1
The source operand and the target operand in the at&t and Intel formats are the exact opposite. In the Intel assembly format, the target operand is to the left of the source operand, whereas in the At&t assembly format, the target operand is to the right of the source operand. For example:
At&t format Intel format
Addl $,%eax add eax, 1
In the AT&T assembly format, the word length of the operand is determined by the last letter of the operator, with the suffix ' b ', ' W ', ' l ' representing the operand as byte (byte,8 bit), the character (word,16 bit) and the long Word (long,32 bit), and in the Intel assembly format, The length of the operands is represented by prefixes such as "byte ptr" and "word ptr". For example:
At&t format Intel format
Movb Val,%al mov al, byte ptr val
In the AT&T assembly format, the absolute transfer and invocation instruction (Jump/call) operands are preceded by a ' * ' prefix, which is not required in the Intel format. The operation code for the remote transfer instruction and the remote child invocation instruction is "Ljump" and "Lcall" in the At&t assembly format, while in the Intel assembly format "jmp far" and "call far", namely:
At&t format Intel format
Ljump $section, $offset JMP far Section:offset
Lcall $section, $offset Call Far Section:offset
The corresponding remote return instruction is:
At&t format Intel format
Lret $stack _adjust RET far Stack_adjust
In the AT&T assembly format, the number of memory operands is addressed by
At&t format Intel format
Section:disp (base, index, scale) Section:[base + Index*scale + disp]
Since Linux works in protected mode with a 32-bit linear address, the address is computed without regard to the segment base and offset, using the following address calculation method: Disp + base + Index * Scale below are some examples of memory operands:
At&t format Intel format
Movl-4 (%EBP),%eax mov eax, [ebp-4]
MOVL Array (,%EAX, 4),%eax mov eax, [eax*4 + array]
MOVW Array (%EBX,%eax, 4),%CX mov cx, [ebx + 4*eax + array]
Movb $,%fs: (%EAX) MOV fs:eax, 4
Introduction to inline assembly formatThe inline assembler syntax is as follows:
__ASM__ (Assembly statement Template: Output part: Input part: Damage Description section)
Of these, ASM and __asm__ are exactly the same. A total of four parts: Assembly statement Template, the output part, the input part, the destruction description part, each part uses ":" The lattice opens, the assembly statement template is essential, the other three parts are optional, if uses the later part, but the front part is empty, also needs to use ":" The lattice opens, the corresponding partial content is empty. For example:
__asm__ __volatile__ ("CLI"::: "Memory")
1, Assembly statement Template assembly statement template consists of assembly statement sequence, the use of ";", "//n" or "//n//t" separated. The operands in the directive can use placeholders to refer to the C language variable, with a maximum of 10 operands, with the following name:%0,%1,...,%9. The operand represented by a placeholder in a directive is always treated as a long (4 bytes), but the operation imposed on it can be either word or byte, and the default is either low or low byte when the operand is used as a word or byte. A byte operation can explicitly indicate whether it is a low byte or a secondary byte.   The method is to insert a letter between the% and the ordinal number, "B" for the low Byte, and "H" to represent the high byte, for example:%H1. 2, output part output part describes output operand, different operand descriptors are separated by commas, each operand descriptor is composed of a qualified string and a C language variable. The qualifying string for each output operand must contain "=" to indicate that he is an output operand. Cases:
__asm__ __volatile__ ("PUSHFL; POPL%0; CLI ":" =g "(x))
A descriptor string represents a restriction on the variable, so that GCC can determine how to allocate registers based on these conditions, and how to generate the necessary code-processing instruction operands to be associated with C-expressions or C-variables. 3, input part of the input part of the description of the input operand, different operand descriptor between the use of commas, each operand descriptor by the qualified string and C language expressions or C language variables. Example 1:
__asm__ __volatile__ ("Lidt%0":: "M" (Real_mode_idt));
Example II (BITOPS.H):
Static __inline__ void __set_bit (int nr, volatile void * addr) {__asm__ ("BTSL%1,%0" : "=m" (ADDR): "Ir" (NR); }
The following example function is to set the NR bit (*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.   So the assembly statement code above is equivalent to the following pseudocode: BTSL nr, ADDR, the two operands of the directive cannot be all memory variables, so the qualifying string for NR is specified as "Ir", and the Nr is associated with an immediate number or register so that only ADDR is the memory variable in the two operands. 4, limit character 4.1, limit character list limit character There are many kinds, some are related to a particular architecture, here only list the commonly used qualifiers and some common qualifiers that may be used in i386. Their role is to instruct the compiler how to handle the relationship between the subsequent C language variables and the instruction operands.
category qualifier description
    &N Bsp         General Register a put input variables into eax here's a question: suppose eax is already in use. It's simple: because GCC knows that EAX is already in use, it inserts a statement at the beginning of the assembly code PUSHL%eax, saves the eax content to the stack, and then adds a statement at the end of the code POPL%eax to restore the EAX content
b put input variable into ebx
c put input variable into ecx
d put input variables into edx
s put input variables into ESI
d Place input variables in EDI
Q put the input variable into a eax,ebx,ecx,edx
R put the input variable into a universal register, that is, E A
a in Ax,ebx,ecx,edx,esi,edi synthesizes eax and edx into a 64-bit register (use long longs)

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.