The similarities and differences between the Linux at-T assembly syntax format and the Intel Assembler syntax format

Source: Internet
Author: User

Since the vast majority of domestic programmers have previously only been exposed to the Intel format of assembly language, there is little or no contact with the-T assembly language, although these assembly codes are Intel-style. But in Unix and Linux systems, more use of the or T format, both in the syntax format is very different, in fact, can use the original assembly of ideas to solve the problem, as long as the following two differences:

First, in the/T assembly format, the register name is prefixed with '% ', whereas in the Intel assembler format, the register name does not need to be prefixed. For example:

/t format

Intel format

PUSHL%eax

Push EAX

In the/T assembly format, a ' $ ' prefix is used to denote an immediate operand, whereas in the Intel assembler format, the immediate number representation is not prefixed with any prefix. For example:

/t format

Intel format

PUSHL $

Push 1

The position of the source operand and the target operand in the three, T, and Intel formats is exactly the opposite. In the Intel assembler format, the target operand is to the left of the source operand, while in the/T assembly format, the target operand is to the right of the source operand. For example:

/t format

Intel format

Addl $,%eax

add eax, 1

Iv. in the/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 operands as bytes (byte,8 bits), words (word,16 bits), and long words (long,32 bits), while in the Intel assembly format , the length of the operand is expressed as a prefix such as "byte ptr" and "word ptr". For example:

/t format

Intel format

Movb Val,%al

mov al, byte ptr val

V. In the/T assembly format, the absolute transfer and invoke instruction (Jump/call) is preceded by a ' * ' as the prefix, but not in the Intel format.

The operation codes for remote transfer instructions and remote sub-invoke instructions are "ljump" and "Lcall" in the/T assembly format, while in the Intel Assembler format are "jmp far" and "call far", i.e.:

/t format

Intel format

Ljump $section, $offset

JMP far Section:offset

Lcall $section, $offset

Call Far Section:offset

The corresponding remote return instruction is:

/t format

Intel format

Lret $stack _adjust

RET far Stack_adjust

Vi. in the AT/T assembly format, the memory operand is addressed in the form

Section:disp (base, index, scale)

In the Intel assembler format, the memory operand is addressed in the following way:

Section:[base + Index*scale + disp]

Regardless of the form, the following address calculations are implemented: (where base and index must be registers, disp and scale can be constants)

DISP + base + Index * scale

Here is an example of a memory operand:

tr> /tr>

at &t format

intel format

movl-4 (%EBP),%eax

mov eax, [ebp-4]

MOVL Array (,%EAX, 4),%ea x

mov eax, [eax*4 + array]

MOVW Array (%EBX,%eax, 4),%cx

mov cx, [ebx + 4*eax + array]

Movb $4,%fs: (%eax)

mov fs:eax, 4

Hello world! program in different syntax format

The first example of all programming languages is to print a string "Hello world!" on the screen, which is how we begin to introduce the assembly language programming under Linux. In the Linux operating system, you have many ways to display a string on the screen, but the simplest way is to use the system calls provided by the Linux kernel. The biggest benefit of using this approach is that you can communicate directly with the operating system's kernel, do not need to link libraries such as libc, and do not need to use the ELF interpreter, so the code size is small and execution speed is fast. Linux is a 32-bit operating system running in protected mode, using flat memory mode, the most commonly used is the ELF format of binary code. An elf-formatted executable program is typically divided into the following sections:. Text,. Data, and. BSS, where. Text is a read-only code area,. Data is a readable, writable, and. BSS is a data area that can be read-write and not initialized. The code area and data area are called sections in the Elf, and depending on the actual need you can use the other standard section, or you can add a custom portion, but an elf executable should have at least one. Text part. Here is our first assembler, with the-T assembly format:

Example 1. /t format

#hello. S

. data# Data Segment Declaration

msg:. String "Hello, world!//n" #要输出的字符串

Len =. -msg# String length

. text# Code Snippet Declaration

. Global _start# Specify the entry function

_start: #在屏幕上显示一个字符串

MOVL $len,%edx# parameter three: string length

MOVL $msg,%ecx# parameter two: the string to display

MOVL $,%ebx# parameter one: File descriptor (STDOUT)

MOVL $4,%eax# system call number (Sys_write)

int$0x80# Calling kernel functions

#退出程序

MOVL $0,%ebx# parameter one: Exit code

MOVL $1,%eax# system call number (Sys_exit)

int$0x80# Calling kernel functions

When the first contact with the T-format assembly code, many programmers think it is too obscure, no relationship, on the Linux platform you can also use the Intel format to write assembler, suggest or stick to it, after all, under the Linux T is the mainstream language format:

Example 2. Intel format

; Hello.asm

section. data; Segment Declaration

MSG db "Hello, world!", 0xA; string to output

Len Equ $-msg; string length

section. Text; code Snippet Declaration

Global _start; Specify the entry function

_start:; display a string on the screen

mov edx, len; parameter three: string length

mov ecx, msg; parameter two: the string to display

mov ebx, 1; parameter one: File descriptor (STDOUT)

mov eax, 4; system call number (Sys_write)

int 0x80; invoke kernel function

; exit the program

mov ebx, 0; parameter one: Exit code

mov eax, 1; system call number (Sys_exit)

int 0x80; invoke kernel function

The syntax used for the above two assembler is completely different.

The same is: 1, the function is to call the Linux kernel provided by the Sys_write to display a string;

2, call Sys_exit exit program. In the Linux kernel source file/usr/include/asm/unistd.h, you can find definitions for all system calls.

The difference is: 1, program note at/t use # to start commenting, Intel use; start commenting;

2. The statement of the AT/t paragraph is used directly by the. Data and. Text, Inter uses section. Data and sections. Text;

The similarities and differences between the Linux at-T assembly syntax format and the Intel Assembler syntax format

Related Article

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.