Introduction to At&t syntax under Linux (i.e. GNU as Assembly syntax) __linux

Source: Internet
Author: User
As an efficient and tightly integrated hardware platform, assembly language plays an important role in the operating system, embedded development and other fields. Because the assembler relies on the hardware structure (CPU script), the assembly language of the different architectures is also very divergent. This article briefly introduces the AT&T syntax under Linux (the GNU as Assembly Syntax) and the basic methods of compiling it under Linux.

The at&t syntax, which originated in the At&t Bell Lab, was formed on top of the processor opcode syntax used to implement UNIX systems, and the main differences between AT&T and Intel syntax are as follows:
At&t uses $ to represent immediate numbers, Intel does not, so it means decimal 2 o'clock, At&t is $, and Intel is 2
At&t in front of the register, such as the EAX register is represented as%eax
The order of at&t processing operands is the opposite of Intel, for example, Movl%eax,%EBX is to pass the value in EAX to EBX, and Intel is the Mov ebx, eax
At&t a single character at the back of the mnemonic to indicate the length of the data in the operation, such as Movl $foo,%eax equivalent to Intel's mov eax, word ptr foo
Long jumps and calls are different in format, at&t for ljmp $section, $offset, while Intel is JMP Section:offset
The main difference is these, the other details are many, here is a concrete example to illustrate

#cpuid. S Sample Program

. Section. Data

Output
. ASCII "The processor vendor ID is ' xxxxxxxxxxxx '/n '

. section. Text
. globl _start

_start:

MOVL $,%eax

Cpuid

MOVL $output,%edi

MOVL%EBX (%edi)

MOVL%edx (%edi)

MOVL%ecx (%edi)

MOVL $,%eax

MOVL $,%EBX

MOVL $output,%ECX

MOVL $42,%edx

int $0x80

MOVL $,%eax

MOVL $,%EBX

int $0x80


The purpose of this program is to query the manufacturer ID of the CPU, where:

, the ASCII definition string (which is completely different from the Intel format). Section is the statement for the declaration segment,. Data and. Text is the segment name, which is the section and code snippet, _start is the default entry label for gas (GNU assembler), which indicates that the program starts from here. Globl _start is declared as a label for external program Access. CPUID for instructions to request the CPU's specified information, the instruction with EAX as input, ebx,edx,ecx as output, here will be 0 as a cpuid input instruction, request return CPU Vendor ID string. The returned result, a 12-byte string, is stored in three registers, where EBX holds a low 4-bit, edx Middle 4-bit, and ecx high 4-bit (note order.) )。 Next, define a pointer edi,edi to the start address of output, and then the 3 statements will replace the X in output with the vendor information. 28 in%edi represents an offset, that is, the address in%edi is added to 28 bytes, which is exactly the address of the first X in output. And then the results are printed, here is a system call to Linux (int 0x80), the system calls the parameters are: EAX system call number, EBX to write the file descriptor, ECX string first address, edx string length, the program in the values of these parameters are 4, respectively, 1 (standard output), output address and 42. The last call to the 1th system call-Exit function, return to the shell, the value in this ebx is returned to the shell exit code, 0 means no exception

Then assemble the connection to run the program:
[Root@zieckey-laptop src]# as-o cpuid.o Cpuid.s
[Root@zieckey-laptop src]# ld Cpuid.o-o CPUID
[Root@zieckey-laptop src]#./cpuid
The processor vendor ID is ' Genuineintel '
[Root@zieckey-laptop src]#

My computer is Pentium M CPU so the result of return is Genuineintel.

A few notes:

1 Linux Standard assembly environment for As,ld,gdb,gprof,objdump and other GNU development debugging tools, in addition to GDB, all other with Binutils package released. Where as uses the at&t syntax. You can also use NASM to write an Intel format assembler under Linux

2 Linux under the assembly system call for int 0x80, and DOS under the INT 21h the same, but the transfer parameters are different

3 paragraph declaration statement. Section does not need to be preceded by the end of the paragraph, as in the case of Intel format (Segment/ends), and the beginning of the next paragraph automatically marks the end of the last paragraph.

4 The entry tag of a simple program is not necessarily defined, the LD will judge the entrance itself, but it will give a warning

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.