本文主要记录汇编学习笔记。

RAX,EAX,AX,AH,AL

  1. RAX是64位的寄存器

  2. EAX是32位的寄存器,而AX是EAX的低16位。

  3. AH是AX的高8位,而AL是AX的低8位

操作数前缀

1
2
3
movl   $8,%eax 
movl $0xffff,%ebx
int $0x80

看到在AT%T汇编中诸如”%eax”、”%ebx”之类的寄存器名字前都要加上”%”;”$8”、”$0xffff”这样的立即数之前都要加上”$”。

源/目的操作数顺序

在Intel语法中,第一个操作数是目的操作数,第二个操作数源操作数。而在AT&T中,第一个数是源操作数,第二个数是目的操作数。

1
2
3
4
// INTEL语法
MOV EAX,8 //EAX是目的操作数, 8是源操作数
// AT&T语法
movl $8,%eax //8是源操作数 EAX是目的操作数

标识长度的操作码后缀

在AT&T的操作码后面有时还会有一个后缀,其含义就是指出操作码的大小。“l”表示长整数(32位),“w”表示字(16位),“b”表示字节(8位)。

1
2
3
4
movb    %bl,%al
movw %bx,%ax
movl %ebx,%eax
movl (%ebx),%eax

Labels

A label can be placed at the beginning of a statement. During assembly, the label is assigned the current value of the active location counter and serves as an instruction operand. There are two types of lables: symbolic and numeric.

Symbolic Labels

A symbolic label consists of an identifier (or symbol) followed by a colon (:) (ASCII 0x3A). Symbolic labels must be defined only once. Symbolic labels have global scope and appear in the object file’s symbol table.

Symbolic labels with identifiers beginning with a period (.) (ASCII 0x2E) are considered to have local scope and are not included in the object file’s symbol table.

Numeric Labels

A numeric label consists of a unsigned decimal int32 value followed by a colon (:). Numeric labels are used only for local reference and are not included in the object file’s symbol table. Numeric labels have limited scope and can be redefined repeatedly.

When a numeric label is used as a reference (as an instruction operand, for example), the suffixes b (“backward”) or f (“forward”) should be added to the numeric label. For numeric label N, the reference Nb refers to the nearest label N defined before the reference, and the reference Nf refers to the nearest label N defined after the reference. The following example illustrates the use of numeric labels:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1:          / define numeric label "1"
one: / define symbolic label "one"

/ ... assembler code ...

jmp 1f / jump to first numeric label "1" defined
/ after this instruction
/ (this reference is equivalent to label "two")

jmp 1b / jump to last numeric label "1" defined
/ before this instruction
/ (this reference is equivalent to label "one")

1: / redefine label "1"
two: / define symbolic label "two"

jmp 1b / jump to last numeric label "1" defined
/ before this instruction
/ (this reference is equivalent to label "two")

参考资料:

  1. 内嵌汇编学习
  2. x86 Assembly Language Reference Manual