Difference Between AT&T and Intel Assembly Syntax(AT&T和Intel汇编语法的不同)

译者注:学习汇编语言的时候用的是王爽老师的那本,里面讲的是Intel系统的语法。但是在实际工作中,都是在Linux下研究C,就发现GCC编译后的汇编代码都看不懂。

The difference(不同之处)
--------------

This document is more related to coding than hacking, although assembly is a very useful programming language, as its machine level and provides direct access to the CPU, hardware, etc. Now in all Unix-derived systems, the compilers like gcc use att syntax assembly and not intel. For example: movl %esp, %ebp

汇编语言是直接面向硬件的,他提供了对CPU,硬件等的直接访问。因此,汇编语言是一门非常有用的编程语言。但是本篇文档将更多滴描述程序编写的一些东西,而不是怎么将汇编的功能发挥到极致。现在,在所有的类Unix系统中,类此GCC这样的编译器使用的是AT&T汇编语法系统,而不是Intel系统。比如:movl %esp, %ebp

Now this is unfortunate for DOS assembly programmers who recently switched to Unix-derived systems. They are used to Intel syntax, whereas Linux (and others) uses AT&T syntax. Where in the example above you would use: mov ebp, esp.

这对那些以前在DOS下编写汇编语言的程序员来说确实很悲催。因为DOS下面使用的是Intel语法系列,而不是AT&T系列。上面那句汇编在DOS洗啊是这样的:mov ebp, esp

I wrote this because I have only seen one document that explained the differences between AT&T and Intel syntax. That document was the GAS (GNU assembler) reference manual.

我之所以要写这篇文章,是因为我发现只有一篇文档讲到了这两个汇编语法体系的区别:GAS (GNU assembler) reference manual

You can get the GAS reference manual at:http://www.cs.utah.edu/csinfo/texinfo under "gas".

可以从这个网址获取这篇手册。

First let me give a few examples.
Intel: push 4
AT&T: pushl $4

首先,让我们看些例子:

Intel: push 4

AT&T: push $4

All the immediate operands have a $ in front of them, in intel syntax, you don't have prefix.

在AT&T语法中,所有立即数的前面都加了一个$符号。Intel语法中,立即数是没有这个前缀的。

The register operands, have a % in front of them, intel has none.
Intel: mov eax, 4
att: movl $4, %eax

寄存器操作数前面都加了一个百分号%,Intel语法中是没有。

Intel: mov eax, 4

ATT: movl $4, %eax

You notice there is a diff in intel/att's src/dst... 也许你已经注意到了,这两个语法系统在 数据源/目标位置 的书写顺序上是不同的。
Intel: you do dst, src like mov ax, 2 Intel: 顺序是这样的:数据源,目标位置 比如:mov ax, 2
att: it's the opposite, src, dst like movl $2, %ax att: 顺序刚好反了:目标位置,数据源 比如:movl $2, %ax

You can use 'b' for byte, 'w' for word, 'l' for long, etc...as the memory suffix: 在att语法中,我们可以使用以下后缀来标识所操作数据的长度:b 字节 w 字 l 长字
movl, movb, movw, etc.
in intel you wold do this like mov ax, byte ptr foo... 在Intel中,是这样表示操作数长度的:mov ax, byte ptr foo

The far instruction for att is lret $stack-adjust, in intel it's ret far stack-adjust.

AT&T语法返回指令lret $stack-adjustIntel使用retfar stack-adjust

The l in front of mov, is the byte/memory operand..... this is actually more convient if you ask me.

我认为在mov指令前加l,来标识字节操作数,真是太方便了!

In Intel you have: Intel的指令系统里有:
section:[base + index*scale + disp] 段地址:[基地址 + index*scale + 偏移量]

disp = displacement 偏移量
scale = 1 if not given

In AT&T, however, you would have:
section:disp(base, index, scale)

So "es:[ebp-5]" in Intel would be "%es:-4(%ebp)" in AT&T syntax.

Intel: [foo] AT&T: foo(,1) the ,1 means an index of one...
Intel: [foor + eax*4] AT&T: foor(, %eax, 4)

I hope this helps :)

How to Get some assembly examples in unix: 怎样在Unix系统下搞一段AT&T语法的汇编看看
-----------------------------------------

Now how to get a few examples on how to get some assembly code for Unix.
Use this (assuming you called it test.c):

现在看下怎么样在Unix下获取一段汇编代码。使用下面这一段C代码,保存到tesst.c。然后运行 gcc -S test.c,会产生test.s。

void main()
{
printf("hi\n");
}

now to compile it, do gcc -S test.c, this will make a file test.s in
assembly......look at it it contains great info....and some examples of
the macros and what not defined/shown in gas' (GNU assembler) manual.
(Which can be found at http://www.cs.utah.edu/csinfo/texinfo, under gas.

here is what test.s will look like:

.file "test.c"
.version "01.01"
gcc2_compiled.:
.section .rodata
.LC0:
.string "test\n"
.text
.align 4
.globl main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
pushl $.LC0
call printf
addl $4,%esp
.L1:
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 2.7.2.1"


As you know, the l's in front of push, mov, add, etc....that means it's
type long. and the % goes in front of all register operands, whereas in
intel syntax, it is undelimited. Likewise, the immediate operands, have a
'$' in front of them, whereas once again, intel is undelimited.

movl $3, %eax
is equal to:
mov eax, 3
in intel

The other way to get asm code is with gdb......you compile your program
with gcc -g .......and for even more......gcc -g -a...
here is our test.c ......in gdb,

获得汇编(asm)代码的另一种方法是使用GDB。使用GCC编译程序的时候加上-g选项,也可是-g -a,更详细。

进入GDB以后,执行 'disassemble main'
we do 'disassemble main':

(gdb) disassemble main
Dump of assembler code for function main:
0x8048474 <main>: pushl %ebp
0x8048475 <main+1>: movl %esp,%ebp
0x8048477 <main+3>: pushl $0x80484c8
0x804847c <main+8>: call 0x8048378 <printf>
0x8048481 <main+13>: addl $0x4,%esp
0x8048484 <main+16>: leave
0x8048485 <main+17>: ret
End of assembler dump.

That is with just -g.......with -a as well you can see the difference
(more instructions show up that usually wouldn't):

(gdb) disassemble main
Dump of assembler code for function main:
0x80485d8 <main>: pushl %ebp
0x80485d9 <main+1>: movl %esp,%ebp
0x80485db <main+3>: cmpl $0x0,0x8049a6c
0x80485e2 <main+10>: jne 0x80485f1 <main+25>
0x80485e4 <main+12>: pushl $0x8049a6c
0x80485e9 <main+17>: call 0x80488fc <__bb_init_func>
0x80485ee <main+22>: addl $0x4,%esp
0x80485f1 <main+25>: incl 0x8049b78
0x80485f7 <main+31>: pushl $0x8048978
0x80485fc <main+36>: call 0x8048468 <printf>
0x8048601 <main+41>: addl $0x4,%esp
0x8048604 <main+44>: incl 0x8049b7c
0x804860a <main+50>: leave
0x804860b <main+51>: ret
End of assembler dump.

I of course need to give credit of this to the gas manual, as parts were taken from there.

详细的揭示了linux下的AT&T汇编指令使用方法,给出了各种汇编指令,指令用法,以及相应的例程。其中包括一些如何使用汇编链接C语言库,汇编调用系统调用,汇编执行浮点运算,C语言内嵌汇编等。 Chapter 1: What Is Assembly Language? 1 Processor Instructions 1 Instruction code handling 2 Instruction code format 3 High-Level Languages 6 Types of high-level languages 7 High-level language features 9 Assembly Language 10 Opcode mnemonics 11 Defining data 12 Directives 14 Summary 15 Chapter 2: The IA-32 Platform 17 Core Parts of an IA-32 Processor 17 Control unit 19 Execution unit 24 Registers 25 Flags 29 Advanced IA-32 Features 32 The x87 floating-point unit 32 Multimedia extensions (MMX) 33 Streaming SIMD extensions (SSE) 33 Hyperthreading 34 The IA-32 Processor Family 34 Intel processors 35 Non-Intel processors 36 Summary 37 Chapter 3: The Tools of the Trade 39 The Development Tools 39 The Assembler 40 The Linker 42 The Debugger 43 The Compiler 44 The object code disassembler 44 The Profiler 44 The GNU Assembler 45 Installing the assembler 45 Using the assembler 47 A word about opcode syntax 49 The GNU Linker 50 The GNU Compiler 53 Downloading and installing gcc 53 Using gcc 54 The GNU Debugger Program 56 Downloading and installing gdb 56 Using gdb 57 The KDE Debugger 60 Downloading and installing kdbg 60 Using kdbg 60 The GNU Objdump Program 62 Using objdump 63 An objdump example 64 The GNU Profiler Program 65 Using the profiler 65 A profile example 68 A Complete Assembly Development System 69 The basics of Linux 69 Downloading and running MEPIS 70 Your new development system 71 Summary 72 Chapter 4: A Sample Assembly Language Program 73 The Parts of a Program 73 Defining sections 74 Defining the starting point 74 Creating a Simple Program 75 The CPUID instruction 76 The sample program 77 Building the executable 80 Running the executable 80 Assembling using a compiler 80 Debugging the Program 81 Using gdb 81 Using C Library Functions in Assembly 86 Using printf 87 Linking with C library functions 88 Summary 90 Chapter 5: Moving Data 91 Defining Data Elements 91 The data section 91 Defining static symbols 94 The bss section 95 Moving Data Elements 97 The MOV instruction formats 97 Moving immediate data to registers and memory 98 Moving data between registers 99 Moving data between memory and registers 99 Conditional Move Instructions 106 The CMOV instructions 107 Using CMOV instructions 109 Exchanging Data 110 The data exchange instructions 111 Using the data exchange instruction 116 The Stack 119 How the stack works 119 PUSHing and POPing data 120 PUSHing and POPing all the registers 123 Manually using the ESP and EBP registers 123 Optimizing Memory Access 123 Summary 124 Chapter 6: Controlling Execution Flow 127 The Instruction Pointer 127 Unconditional Branches 129 Jumps 129 Calls 132 Interrupts 135 Conditional Branches 136 Conditional jump instructions 136 The compare instruction 138 Examples of using the flag bits 140 Loops 144 The loop instructions 144 A loop example 145 Preventing LOOP catastrophes 145 Duplicating High-Level Conditional Branches 146 if statements 147 for loops 150 Optimizing Branch Instructions 153 Branch prediction 153 Optimizing tips 155 Summary 158 Chapter 7: Using Numbers 161 Numeric Data Types 161 Integers 162 Standard integer sizes 162 Unsigned integers 164 Signed integers 166 Using signed integers 168 Extending integers 169 Defining integers in GAS 172 SIMD Integers 173 MMX integers 173 Moving MMX integers 174 SSE integers 176 Moving SSE integers 177 Binary Coded Decimal 178 What is BCD? 178 FPU BCD values 179 Moving BCD values 180 Floating-Point Numbers 182 What are floating-point numbers? 182 Standard floating-point data types 184 IA-32 floating-point values 186 Defining floating-point values in GAS 187 Moving floating-point values 187 Using preset floating-point values 189 SSE floating-point data types 190 Moving SSE floating-point values 191 Conversions 196 Conversion instructions 196 A conversion example 198 Summary 199 Chapter 8: Basic Math Functions 201 Integer Arithmetic 201 Addition 201 Subtraction 210 Incrementing and decrementing 215 Multiplication 216 Division 221 Shift Instructions 223 Multiply by shifting 224 Dividing by shifting 225 Rotating bits 226 Decimal Arithmetic 227 Unpacked BCD arithmetic 227 Packed BCD arithmetic 229 Logical Operations 231 Boolean logic 231 Bit testing 232 Summary 233 Chapter 9: Advanced Math Functions 235 The FPU Environment 235 The FPU register stack 236 The FPU status, control, and tag registers 237 Using the FPU stack 242 Basic Floating-Point Math 245 Advanced Floating-Point Math 249 Floating-point functions 249 Partial remainders 252 Trigonometric functions 254 Logarithmic functions 257 Floating-Point Conditional Branches 259 The FCOM instruction family 260 The FCOMI instruction family 262 The FCMOV instruction family 263 Saving and Restoring the FPU State 265 Saving and restoring the FPU environment 265 Saving and restoring the FPU state 266 Waiting versus Nonwaiting Instructions 269 Optimizing Floating-Point Calculations 270 Summary 270 Chapter 10: Working with Strings 273 Moving Strings 273 The MOVS instruction 274 The REP prefix 278 Other REP instructions 283 Storing and Loading Strings 283 The LODS instruction 283 The STOS instruction 284 Building your own string functions 285 Comparing Strings 286 The CMPS instruction 286 Using REP with CMPS 288 String inequality 289 Scanning Strings 291 The SCAS instruction 292 Scanning for multiple characters 293 Finding a string length 295 Summary 296 Chapter 11: Using Functions 297 Defining Functions 297 Assembly Functions 299 Writing functions 299 Accessing functions 302 Function placement 304 Using registers 304 Using global data 304 Passing Data Values in C Style 306 Revisiting the stack 306 Passing function parameters on the stack 306 Function prologue and epilogue 308 Defining local function data 309 Cleaning out the stack 312 An example 312 Watching the stack in action 314 Using Separate Function Files 317 Creating a separate function file 317 Creating the executable file 318 Debugging separate function files 319 Using Command-Line Parameters 320 The anatomy of a program 320 Analyzing the stack 321 Viewing command-line parameters 323 Viewing environment variables 325 An example using command-line parameters 326 Summary 328 Chapter 12: Using Linux System Calls 329 The Linux Kernel 329 Parts of the kernel 330 Linux kernel version 336 System Calls 337 Finding system calls 337 Finding system call definitions 338 Common system calls 339 Using System Calls 341 The system call format 341 Advanced System Call Return Values 346 The sysinfo system call 346 Using the return structure 347 Viewing the results 348 Tracing System Calls 349 The strace program 349 Advanced strace parameters 350 Watching program system calls 351 Attaching to a running program 353 System Calls versus C Libraries 355 The C libraries 356 Tracing C functions 357 Comparing system calls and C libraries 358 Summary 359 Chapter 13: Using Inline Assembly 361 What Is Inline Assembly? 361 Basic Inline Assembly Code 365 The asm format 365 Using global C variables 367 Using the volatile modifier 369 Using an alternate keyword 369 Extended ASM 370 Extended ASM format 370 Specifying input and output values 370 Using registers 372 Using placeholders 373 Referencing placeholders 376 Alternative placeholders 377 Changed registers list 377 Using memory locations 379 Using floating-point values 380 Handling jumps 382 Using Inline Assembly Code 384 What are macros? 384 C macro functions 384 Creating inline assembly macro functions 386 Summary 387 Chapter 14: Calling Assembly Libraries 389 Creating Assembly Functions 389 Compiling the C and Assembly Programs 391 Compiling assembly source code files 392 Using assembly object code files 392 The executable file 393 Using Assembly Functions in C Programs 395 Using integer return values 396 Using string return values 397 Using floating-point return values 400 Using multiple input values 401 Using mixed data type input values 403 Using Assembly Functions in C++ Programs 407 Creating Static Libraries 408 What is a static library? 408 The ar command 409 Creating a static library file 410 Compiling with static libraries 412 Using Shared Libraries 412 What are shared libraries? 412 Creating a shared library 414 Compiling with a shared library 414 Running programs that use shared libraries 415 Debugging Assembly Functions 417 Debugging C programs 417 Debugging assembly functions 418 Summary 420 Chapter 15: Optimizing Routines 421 Optimized Compiler Code 421 Compiler optimization level 1 422 Compiler optimization level 2 423 Compiler optimization level 3 425 Creating Optimized Code 425 Generating the assembly language code 425 Viewing optimized code 429 Recompiling the optimized code 429 Optimization Tricks 430 Optimizing calculations 430 Optimizing variables 433 Optimizing loops 437 Optimizing conditional branches 442 Common subexpression elimination 447 Summary 450 Chapter 16: Using Files 453 The File-Handling Sequence 453 Opening and Closing Files 454 Access types 455 UNIX permissions 456 Open file code 458 Open error return codes 459 Closing files 460 Writing to Files 460 A simple write example 460 Changing file access modes 462 Handling file errors 462 Reading Files 463 A simple read example 464 A more complicated read example 465 Reading, Processing, and Writing Data 467 Memory-Mapped Files 470 What are memory-mapped files? 470 The mmap system call 471 mmap assembly language format 473 An mmap example 475 Summary 479 Chapter 17: Using Advanced IA-32 Features 481 A Brief Review of SIMD 481 MMX 482 SSE 483 SSE2 483 Detecting Supported SIMD Operations 483 Detecting support 484 SIMD feature program 485 Using MMX Instructions 487 Loading and retrieving packed integer values 487 Performing MMX operations 488 Using SSE Instructions 497 Moving data 498 Processing data 499 Using SSE2 Instructions 504 Moving data 505 Processing data 505 SSE3 Instructions 508 Summary 508 Index 511
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值