请参阅80386程序员参考手册的17.2章节。反汇编器实际上只是一种被美化的有限状态机。反汇编的步骤如下:
F3
,F2
或F0
);如果是,则有一个REP
/REPE
/REPNE
/LOCK
前缀。继续到下一个字节。67
)。如果是,则在16位模式下解码指令的其余部分,如果当前处于32位模式,则在32位模式下解码地址。66
)。如果是,则在16位模式下解码立即数操作数,如果当前处于32位模式,则在32位模式下解码立即数操作数。2E
,36
,3E
,26
,64
或65
)。如果是,则使用相应的段寄存器来解码地址,而不是默认的段寄存器。0F
,则它是扩展操作码,并读取下一个字节作为扩展操作码。操作码告诉您正在执行的操作。操作码的参数可以从Mod R/M、SIB、位移和立即值的值中解码。由于x86的复杂性,存在许多可能性和特殊情况。有关更详细的说明,请参见上面的链接。
数据结构和解码阶段在https://code.google.com/p/distorm/wiki/diStorm_Internals中有详细说明。80x86 Instruction:
A 80x86 instruction is divided to a number of elements:
- Instruction prefixes, affects the behaviour of the instruction's operation.
- Mandatory prefix used as an opcode byte for SSE instructions.
- Opcode bytes, could be one or more bytes (up to 3 whole bytes).
- ModR/M byte is optional and sometimes could contain a part of the opcode itself.
- SIB byte is optional and represents complex memory indirection forms.
- Displacement is optional and it is a value of a varying size of bytes(byte, word, long) and used as an offset.
- Immediate is optional and it is used as a general number value built from a varying size of bytes(byte, word, long).
The format looks as follows:
/-------------------------------------------------------------------------------------------------------------------------------------------\ |*Prefixes | *Mandatory Prefix | *REX Prefix | Opcode Bytes | *ModR/M | *SIB | *Displacement (1,2 or 4 bytes) | *Immediate (1,2 or 4 bytes) | \-------------------------------------------------------------------------------------------------------------------------------------------/ * means the element is optional.
出于历史原因,原始链接被保留:
http://code.google.com/p/distorm/wiki/x86_x64_Machine_Code 和 http://code.google.com/p/distorm/wiki/diStorm_Internals
查看objdump源代码——这是一个很棒的工具,它包含了许多操作码表,而且它的源代码可以为制作自己的反汇编器提供良好的基础。