AT&T Assembly Foundation

Source: Internet
Author: User

The At&t assembly is an assembly language that is completely different from that of Intel, in order to avoid confusing Intel syntax, this article only introduces the At&t assembly, the following is all registers, the first feature of At&t is that each register name must be added '% ', Immediately before the number must be added ' $ ': (below omitted%)

8 32-bit registers: eax, ECX, edx, EBX, ESP, EBP, ESI, EDI

8 16-bit registers: AX, CX, DX, BX, SP, BP, Si, di

8 8-bit registers: AL, cl, DL, bl, ah, ch, dh, BH

(Note: 16-bit registers are 32-bit registers of the lower 16-bit, 8-bit registers are AX,CX,DX,BX 8-bit "L: Low, H: High")

6 Segment Registers: CS, DS, SS, ES, FS, GS

3 Control Registers: CR0, Cr2, CR3

6 Debug Registers: Db0, DB1, DB2, DB3, DB6, DB7

2 Test registers: TR6, TR7

8 Floating-point register stacks: St (0), St (1), St (2), St (3), St (4), St (5), St (6), St (7)

An obvious feature of the at&t syntax is that the operands are arranged from left to right, in simple terms, for example:

Intel syntax = "MOV ax,123; 123 to register AX

At&t grammar = "mov $123,%ax #将123存入寄存器ax

On most instructions, both assembly instructions are the same.

In At&t, to describe the length of the operand, there are three symbols: l (Long), w (word), B (Byte), which are added directly after the instruction, such as "Movl $0x80,%eax", indicating that the 0x80 of the immediate number is 4 bytes long.

for annotation symbols, at&t allows 3 annotation symbols, two C/s (Single note "//" and multiline Comment "/**/"), as well as "#" Single-line comments, it is noteworthy that "//" although it can be a single-line comment, but can only be placed in blank line comments, The other two comments can be followed by lines of code, and "/**/" even allows you to write a line of comments at the top of a line of code. In contrast, an assembler with Intel syntax like NASM has only one single-line annotation symbol ";". The at&t assembler does not support this annotation symbol, but it is ignored when encountered.

(The assembly code for Intel syntax is in uppercase letters and at&t in lowercase letters)

Assembler example (under Linux):

Output string to screen

. section. Data #声明数据段,. Sections are used to indicate that this is a segment, which is not added here

Msg

The. ASCII "goodmorning!\n" #.ascii is declared as ASCII, and can be used as a. String, the same effect

Len=.-msg #len储存了字符串长度

. section. Text #声明代码段

. GLOBL _START#.GLOBL used to declare a program entry

_start:

MOVL$MSG,%ECX #标号实质上就是一个地址, this address points to the address of the first data in the segment

MOVL $len,%edx #将字符串的长度放进寄存器edx

MOVL $1,%EBX #

MOVL $4,%eax #将中断号存入eax

Int 0x80 #进入中断

Movl$0,%ebx

MOVL $1,%eax# when eax deposit 1 o'clock Enter interrupt, the description has run out, the program will automatically end

Int 0x80

/*EBX The return value of the stored program, you can use "echo $?" When the program is executed in the terminal. Instruction Viewer return value * *

The general Linux system will have the At&t compiler, the following is a brief summary of the steps to execute the code above

1. Save the above code, named "M.S" (file name casually, Linux for file suffix name is not strict requirements)

2. In the saved directory, the right mouse button, open the terminal.

3. In the terminal input "as M.s-o m.o" (O can name the generated file as you want, here M.S after the as program compiled, the generated target file is M.O, when the filename is not specified with-O, the default name is "A.out")

4. Again in the terminal input "LD m.o-o m", LD is the link program, any program to run at the operating system level must be linked to the executable file to function properly

5. The generated file M is our program, type ". M" and then enter to run, you can enter echo $? To see the return value after the program's end, that is, the value in EBX.

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.