Assembly language is always indispensable in the source code of any operating system. Therefore, assembly language is an essential basic knowledge for learning the operating system. However, at present, most of the teaching in China is conducted on the Windows platform, therefore, most readers are familiar with Intel's Assembly syntax, but at&t's assembly is used in Linux kernel code. This section compares the two types of assembly, allowing readers who are familiar with Intel assembly syntax to quickly master at&t's assembly.
1. prefix
In Intel assembly syntax, there is no prefix for both registers and immediate numbers, but in at&t's Assembly syntax, the prefix for registers is "%", and the prefix for immediate numbers is "$ ". The differences between the two formats are as follows :----------------------------------------------------------------------------
Code snippet 2.1 comparison of Intel and at&t Assembly formats # eax <= 8
MoV eax, 8 (Intel)
Movl $8, % eax (at&t) # eax <= EBX
MoV eax, EBX (Intel)
Movl % EBX, % eax (at&t )------------------------------------------------------------------------
2. operand direction
The direction of the operands of Intel assembly and at&t is the opposite. The first operand in Intel assembly is the destination operand, while the second operand is the source operand. In at&t Assembly syntax, the first operand is the source operand, the second number is the destination operand. The preceding example shows the differences between them.
3. operand Bit Width
In Intel assembly, the bit width of an operand is specified by a specific character, such as "Byte PTR", "word PTR", and "dwordptr. In at&t assembly, the last character of the operation code is used to specify the Bit Width of the operand. B, W, and l represent 8-bit, 16-bit, and 32-bit, respectively. The following example illustrates their differences: ---------------------------------------------------------------------------- code snippet 2.2 comparison between Intel and at&t Assembly formats mov Al, byte PTR bar (Intel)
Movb % Al, bar (at&t )------------------------------------------------------------------------
4. Indirect addressing
The indirect addressing formats of Intel and at&t are as follows :------------------------------------------------------------------------
Code snippet 2.3 comparison of Intel and at&t Assembly formats
1 segreg: [base + Index * scale + disp] (Intel)
2 segreg: disp (base, index, scale) (at&t) the above example is a general form of indirect addressing. To facilitate understanding, we will give an example of this general form.
Assume that there is an array of such struct: struct test {
Int;
Int B;}; struct test bar [10]; ------------------------------------------------------------------------
Assume that you want to access member B in item 6th of the array and use the default segment register, then it corresponds to the Assembly generation
The Code is as follows: ------------------------------------------------------------------ code snippet 2.4 comparison between Intel and at&t Assembly formats
# Base indicates the base address of the array, which corresponds to the bar in this example.
# Index is an array index, which corresponds to the first item in this example, that is, 51.
# Scale is the size of the struct. In this example, the size is 8 bytes.
# Disp is the offset of the structure body. In this example, the offset of B is 4.
MoV eax, dword ptr [BAR + 5*8 + 4] (Intel)
Movl 4 (Bar, 5, 8), % eax (at&t) syntax from here we can see that Intel syntax is relatively intuitive, general form of scale, index, etc. can be empty, for example, the following is a common form. ------------------------------------------------------------------------ Code snippet 2.5 comparison between Intel and at&t Assembly formats
MoV eax, dword ptr [EBP + 20 h] (Intel)
Movl 0x20 (% EBP), % eax, (at&t)
------------------------------------------------------------------------
Note: The array subscript starts from 0..