Here, I try to parse some actual instruction codes:
(1) converting the instruction encode into an Assembly Statement
The following instruction code is taken from an assembly code. I will encode this instruction and parse it into an Assembly statement:
Example 1:
00000016: 89 54 24 04
The analysis is as follows:
(1) first, let's look at Intel & AMD's instruction encoding format: (basic knowledge)
+ -------- + ----------------- + ------------- + ----------- + ------- + ------------------ + ---------------- +
| Prefix | Rex prefix | opcode | MOD/RM | SIB | displacement | immediate |
---------- + ----------------- + ------------- + ---------- + ------- + ------------------- + --------------- +
66 40 1 byte 1 byte 1 byte 1 byte 1 byte 1, 4 bytes 1, 2, 4 bytes
67
2e |
3E |
26
64 althought
65
36 |
F0 |
F3
F2 4f
Rex prefix byte: only supported by: amd64 processor, in order to extend the 64-bit operands.
Intel's processor does not have this domain.
MOD/RM bytes consists of three domains: xx xxx (Binary)
------
MoD Reg R/m
SIB bytes is also composed of three domains:
Xx xxx (Binary)
------
Scale Index base
(2) 1st Bytes: 89 (opcode), one byte encode.
The instructions for this encode (encoding) are: mov eV, GV.
Explanation: This is an mov Command, which has two operand (operands ).
The target operand is: register or memory operand. The size is 2 bytes or 4 bytes. The specific value depends on the default operand size of the command. In this example, this is the number of 4 bytes.
Source operand is: Register, which is 2 bytes or 4 bytes. In this example, the number is 4 bytes.
(3) 2nd Bytes: 54 (mod/RM), one byte encode
54 (mod/RM): 01, 010 100
------
|
+ -------- | ------ -- | ----------------> Mod (mode): 01
|
+ ---------- | ----------------> Register: 010 is the edX register.
|
+ ---------------> R/M (memory reference): 100
Source operand is the edX register.
Target operand: in combination with Mod 01 and R/M 100, this is an addressing mode that requires [SIB] + displacement. That is to say, it must be in combination with the sib bytes before correct addressing. Therefore, the next 1 bytes must be SIB byte.
(4) 3rd Bytes: 24 (SIB), one byte encode
24 (SIB): 00 100 100
------
|
+ ----------- | ------------ | -------------------> Scale: 00
+ ----------- | --------------------> Index: 100
+ -------------------> Base: 100
Scale: 00 indicates that scale is 1
Index: 100 indicates that memory operand is in the form of [base ].
Base: 100 indicates that base register is an ESP register.
Now, what is the objective operand?
MOD/RM indicates that operand has a displacement number.
SIB indicates that the operand format is [base].
Combined, MOD/RM and SIB get the final operand addressing mode: [base + displacement] addressing mode.
The result is as follows: [esp + disp]
(5) Last Bytes: 04 (displacement)
The next byte in the SIB is displacement (offset), which is based on the Code segmemt.
Here is the 04 value.
Conclusion: The result is now available in the form of assembly:
00000016: 89 54 24 04 => movl % edX, 4 (% ESP)
Example 2:
The following is an excerpt from the encoding of a statement. What is its Assembly form?
0f B6 4D F8 (HEX)
(1) first, let's look at the 1st byte, which is: 0fh
Byte 0f: indicates that the opcode of this command has two bytes.
In addition, this is a 32-bit operands command. Because it does not have a prefix modifier.
(2) The 2nd bytes are b6h, so we can find out the following in the table two-byte opcode:
This command is: movzx GV, EB
Note: this refers to the transfer instruction with 0 extension. The opcode of this command has two bytes.
Source operand: EB indicates the number of 1 byte register or memory
Target operand: GV is any general purpose register (General Register). Its size is: Word (2 bytes), doubleword (4 bytes) or quadword (8 bytes)
(3) 3rd Bytes: 4dh. The byte is MoD/RM byte. That is: 01 001 101 (Binary)
The target operands is the ECX register.
The source operands addressing method is in the form of [EBP] + displacement.
This displacement only requires one bytes. Therefore, the next byte must be displacement.
(4) 4th Bytes: f8h.
Based on, as described in point 3rd, the source operand addressing method is: [EBP] + displacement, which does not require SIB byte.
Therefore, this byte is a displacement.
Therefore, the source operands is: [EBP] + F8.
(5) Conclusion: The result is:
0f B6 4D F8 (HEX) => movzx ECx, byte PTR [EBP + F8] (Intel format)
Convert me to the favorite at&t format: movzbl 0xf8 (% EBP), % ECx
That is: movzbl-8 (% EBP), % ECx (final result)
(2) parse the Assembly statement into the instruction encode)
How is the following statement converted to encode?
I. Command Example 1: Add % ECx, % eax (AT & A format)
(1) query table. The opcode encode of add eV and GV (Intel format) is: 01
(2) operands encode is determined by MoD/RM.
The analysis is as follows: eV is determined by MoD filed and R/M filed in mod/RM bytes.
GV is determined by Reg filed in mod/RM bytes.
Therefore, MOD/RM byte produces the following results: 11 001 000 (Binary): C8 (HEX)
Conclusion: Add % ECx, % eax => 01 C8 (Instruction encode)
Ii. Command Example 2: add (% ESP), % eax (AT & A format)
(1) the opcode encode of add GV, eV (Intel format) is: 03.
(2) Similarly, operands encode is determined by MoD/RM. However, in this example, the addressing address of the source operand is in the form of SIB, that is, DISP (base, index, scale). Therefore, MOD/RM byte alone is not enough, but must be combined with SIB byte.
Therefore, it is preliminarily determined that this command requires three bytes. Including opcode encode, MOD/RM encode, and SIB encode.
(3) GV is determined by the reg field in mod/RM. In addition, the R/M field in mod/RM determines the sib addressing.
Therefore, the MOD/RM result is: 00 000 100 (Binary), that is, 04 (HEX)
(4) eV is ultimately determined by SIB. The base field in SIB determines the base register.
The index field in SIB determines the addressing mode.
Therefore, the sib result is 00 100 100 (Binary), that is, 24 (HEX)
(5) The final result code is:
Add (% ESP), % eax => 03 04 24 (Instruction encode)
Iii. Command Example 3:
Here, I will convert the amd64 command to encodes
Command: mov rax, [RBx] (Intel format, 64-bit operand)
Rex prefix encoding:
0100 x
------
4 w r x B
(1) AMD's Rex prefix is set to 64-bit operands, so this command must require Rex prefix
The W-bit (3rd bit) function of Rex is to extend the 64-bit operand. When the position is 1, AMD's 64-bit operand size can be used. Here is 1.
The B-bit (0th-bit) function of Rex is to extend 64 registers. Here is 1.
The r bit (2nd bit) of Rex is used to extend 64 registers. Here is 1.
X-bit (1st-bit) of Rex acts on SIB and is used to extend addressing registers. Here is 0.
Therefore, the entire Rex prefix is 4D.
(3) The following table lists mov GV and EV codes: 8B.
(2) mod/RM byte: 03. SIB addressing is not required here
If the source operand is [RBx + rcX], SIB bytes are required! Because there is a sib addressing method.
(3) The final result code is:
MoV rax, [RBx] => 4D 8B 03 (HEX)
Note: This command must be an amd64 processor and can only be executed in the initialized 64-Bit mode.
If: 4D 8B 03 is executed under ia32, it will be: unexpected results!
I hope my analysis can help you. Of course, it is impossible to explain in detail here. For the encode, please refer to the intel and AMD manuals. Some attention knowledge can also be obtained from instructions.
For example:
1. AMD introduced Rex prefixs in the command structure to support 64 technology. 8 General registers (R8-R15) are added ). 8 debugging registers and 8 control registers. More effective address addressing
2. The default operad size of the command is 32-bit. In 64-Bit mode, w (BIT) in the amd Rex prefix is extended to 64-bit. The prefix 66 h can change the operand size. The default address operands size is 64-bit. Prefix 67 h can change the size of address operand size.
3. The default size of the command is determined by the current CPU mode. If the CPU is initialized to the 64-Bit mode, the amd64 technology can be used.
4. By learning instruction encoding, you can develop your own compilation language compiler to ultimately implement your own back-end assembler. Implement 64 compilers. This is my purpose!