汇编语言常用指令

一个x86-64的中央处理单元(CPU)包含一组16个存储64位值的通用目的寄存器。这些寄存器用来存储整数数据和指针。

除了整数寄存器, CPU还维护着一组单个位的条件码(condition code)寄存器,它们描述了最近的算术或逻辑操作的属性。可以检测这些寄存器来执行条件分支指令。最常用的条件码有:

CF:进位标志。最近的操作使最高位产生了进位。可用来检查无符号操作的溢出.

ZF:零标志。最近的操作得出的结果为0

SF:符号标志。最近的操作得到的结果为负数

OF:溢出标志。最近的操作导致一个补码溢出一正溢出或负溢出

比如说,假设我们用一条ADD指令完成等价于C表达式t=a+ b的功能,这里变量a、 b和t都是整型的。然后,根据下面的C表达式来设置条件码:

条件码 表达式 描述
CF (unsigned) t < (unsigned) a 无符号溢出
ZF (t ==0)
SF (t<0) 负数
OF (a<0==b<0) && (t<0 !=a<0) 有符号溢出

CMP subtracts the operands and sets the flags. Namely, it sets the zero flag(ZF) if the difference is zero (operands are equal).

TEST sets the zero flag, ZF, when the result of the AND operation is zero. If two operands are equal, their bitwise AND is zero when both are zero. TEST also sets the sign flag, SF, when the most significant bit is set in the result, and the parity flag, PF, when the number of set bits is even.

JE [Jump if Equals] tests the zero flag and jumps if the flag is set. JE is an alias of JZ

So,

1
2
TEST %eax, %eax
JE 400e77 <phase_1+0x23>

jumps if the %eax is zero.