Skip to main content

x86_64_assembler/instruction/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use serde::{Deserialize, Serialize};
4
5/// 寄存器枚举,表示x86_64架构中的所有寄存器
6#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
7pub enum Register {
8    /// 低 8 位累加器寄存器 (AL)
9    AL,
10    /// 低 8 位计数器寄存器 (CL)
11    CL,
12    /// 低 8 位数据寄存器 (DL)
13    DL,
14    /// 低 8 位基址寄存器 (BL)
15    BL,
16    /// 高 8 位累加器寄存器 (AH)
17    AH,
18    /// 高 8 位计数器寄存器 (CH)
19    CH,
20    /// 高 8 位数据寄存器 (DH)
21    DH,
22    /// 高 8 位基址寄存器 (BH)
23    BH,
24    /// 16 位累加器寄存器 (AX)
25    AX,
26    /// 16 位计数器寄存器 (CX)
27    CX,
28    /// 16 位数据寄存器 (DX)
29    DX,
30    /// 16 位基址寄存器 (BX)
31    BX,
32    /// 16 位栈指针寄存器 (SP)
33    SP,
34    /// 16 位基址指针寄存器 (BP)
35    BP,
36    /// 16 位源变址寄存器 (SI)
37    SI,
38    /// 16 位目的变址寄存器 (DI)
39    DI,
40    /// 32 位扩展累加器寄存器 (EAX)
41    EAX,
42    /// 32 位扩展计数器寄存器 (ECX)
43    ECX,
44    /// 32 位扩展数据寄存器 (EDX)
45    EDX,
46    /// 32 位扩展基址寄存器 (EBX)
47    EBX,
48    /// 32 位扩展栈指针寄存器 (ESP)
49    ESP,
50    /// 32 位扩展基址指针寄存器 (EBP)
51    EBP,
52    /// 32 位扩展源变址寄存器 (ESI)
53    ESI,
54    /// 32 位扩展目的变址寄存器 (EDI)
55    EDI,
56    /// 64 位扩展累加器寄存器 (RAX)
57    RAX,
58    /// 64 位扩展计数器寄存器 (RCX)
59    RCX,
60    /// 64 位扩展数据寄存器 (RDX)
61    RDX,
62    /// 64 位扩展基址寄存器 (RBX)
63    RBX,
64    /// 64 位扩展栈指针寄存器 (RSP)
65    RSP,
66    /// 64 位扩展基址指针寄存器 (RBP)
67    RBP,
68    /// 64 位扩展源变址寄存器 (RSI)
69    RSI,
70    /// 64 位扩展目的变址寄存器 (RDI)
71    RDI,
72    /// 64 位扩展寄存器 R8
73    R8,
74    /// 64 位扩展寄存器 R9
75    R9,
76    /// 64 位扩展寄存器 R10
77    R10,
78    /// 64 位扩展寄存器 R11
79    R11,
80    /// 64 位扩展寄存器 R12
81    R12,
82    /// 64 位扩展寄存器 R13
83    R13,
84    /// 64 位扩展寄存器 R14
85    R14,
86    /// 64 位扩展寄存器 R15
87    R15,
88    /// 8 位扩展寄存器 R8 低字节
89    R8B,
90    /// 8 位扩展寄存器 R9 低字节
91    R9B,
92    /// 8 位扩展寄存器 R10 低字节
93    R10B,
94    /// 8 位扩展寄存器 R11 低字节
95    R11B,
96    /// 8 位扩展寄存器 R12 低字节
97    R12B,
98    /// 8 位扩展寄存器 R13 低字节
99    R13B,
100    /// 8 位扩展寄存器 R14 低字节
101    R14B,
102    /// 8 位扩展寄存器 R15 低字节
103    R15B,
104    /// 16 位扩展寄存器 R8
105    R8W,
106    /// 16 位扩展寄存器 R9
107    R9W,
108    /// 16 位扩展寄存器 R10
109    R10W,
110    /// 16 位扩展寄存器 R11
111    R11W,
112    /// 16 位扩展寄存器 R12
113    R12W,
114    /// 16 位扩展寄存器 R13
115    R13W,
116    /// 16 位扩展寄存器 R14
117    R14W,
118    /// 16 位扩展寄存器 R15
119    R15W,
120    /// 32 位扩展寄存器 R8
121    R8D,
122    /// 32 位扩展寄存器 R9
123    R9D,
124    /// 32 位扩展寄存器 R10
125    R10D,
126    /// 32 位扩展寄存器 R11
127    R11D,
128    /// 32 位扩展寄存器 R12
129    R12D,
130    /// 32 位扩展寄存器 R13
131    R13D,
132    /// 32 位扩展寄存器 R14
133    R14D,
134    /// 32 位扩展寄存器 R15
135    R15D,
136    /// SSE 寄存器 XMM0
137    XMM0,
138    /// SSE 寄存器 XMM1
139    XMM1,
140    /// SSE 寄存器 XMM2
141    XMM2,
142    /// SSE 寄存器 XMM3
143    XMM3,
144    /// SSE 寄存器 XMM4
145    XMM4,
146    /// SSE 寄存器 XMM5
147    XMM5,
148    /// SSE 寄存器 XMM6
149    XMM6,
150    /// SSE 寄存器 XMM7
151    XMM7,
152}
153
154/// 操作数枚举,表示汇编指令中的各种操作数类型
155#[derive(Debug, Clone, PartialEq)]
156pub enum Operand {
157    /// 寄存器操作数
158    Reg(Register),
159    /// 立即数操作数,包含值和大小
160    Imm {
161        /// 立即数的值
162        value: i64,
163        /// 立即数的大小(位数)
164        size: u8,
165    },
166    /// 内存操作数,包含基址、索引、比例和位移
167    Mem {
168        /// 基址寄存器
169        base: Option<Register>,
170        /// 索引寄存器
171        index: Option<Register>,
172        /// 比例因子
173        scale: u8,
174        /// 位移量
175        displacement: i32,
176    },
177    /// 标签操作数
178    Label(String),
179}
180
181/// 指令枚举,表示x86_64汇编指令
182#[derive(Debug, Clone, PartialEq)]
183pub enum Instruction {
184    /// 数据传送指令:将源操作数传送到目标操作数
185    Mov {
186        /// 目标操作数
187        dst: Operand,
188        /// 源操作数
189        src: Operand,
190    },
191    /// 压栈指令:将操作数压入栈顶
192    Push {
193        /// 要压栈的操作数
194        op: Operand,
195    },
196    /// 出栈指令:将栈顶数据弹出到目标操作数
197    Pop {
198        /// 接收出栈数据的目标操作数
199        dst: Operand,
200    },
201    /// 加法指令:将源操作数加到目标操作数
202    Add {
203        /// 目标操作数(被加数,结果存储位置)
204        dst: Operand,
205        /// 源操作数(加数)
206        src: Operand,
207    },
208    /// 减法指令:从目标操作数中减去源操作数
209    Sub {
210        /// 目标操作数(被减数,结果存储位置)
211        dst: Operand,
212        /// 源操作数(减数)
213        src: Operand,
214    },
215    /// 返回指令:从函数返回
216    Ret,
217    /// 调用指令:调用目标函数
218    Call {
219        /// 要调用的目标函数或地址
220        target: Operand,
221    },
222    /// 加载有效地址指令:计算地址并加载到目标寄存器
223    Lea {
224        /// 目标寄存器(存储计算出的地址)
225        dst: Register,
226        /// 位移量
227        displacement: i32,
228        /// 是否使用 RIP 相对寻址
229        rip_relative: bool,
230    },
231    /// 空操作指令:不执行任何操作
232    /// NOP 指令
233    Nop,
234    /// 标签 (伪指令)
235    Label(String),
236    /// Compare instruction: compares two operands.
237    Cmp {
238        /// The destination operand.
239        dst: Operand,
240        /// The source operand.
241        src: Operand,
242    },
243    /// Unconditional jump instruction.
244    Jmp {
245        /// The jump target operand.
246        target: Operand,
247    },
248    /// Conditional jump instruction.
249    Jcc {
250        /// The jump condition.
251        cond: Condition,
252        /// The jump target operand.
253        target: Operand,
254    },
255    /// Jump if not zero instruction.
256    Jnz {
257        /// The jump target operand.
258        target: Operand,
259    },
260    /// Jump if zero instruction.
261    Jz {
262        /// The jump target operand.
263        target: Operand,
264    },
265    /// Decrement instruction.
266    Dec {
267        /// The operand to decrement.
268        op: Operand,
269    },
270    /// Increment instruction.
271    Inc {
272        /// The operand to increment.
273        op: Operand,
274    },
275    /// Move scalar single-precision floating-point instruction.
276    Movss {
277        /// The destination operand.
278        dst: Operand,
279        /// The source operand.
280        src: Operand,
281    },
282    /// Add scalar single-precision floating-point instruction.
283    Addss {
284        /// The destination operand.
285        dst: Operand,
286        /// The source operand.
287        src: Operand,
288    },
289    /// Subtract scalar single-precision floating-point instruction.
290    Subss {
291        /// The destination operand.
292        dst: Operand,
293        /// The source operand.
294        src: Operand,
295    },
296    /// Multiply scalar single-precision floating-point instruction.
297    Mulss {
298        /// The destination operand.
299        dst: Operand,
300        /// The source operand.
301        src: Operand,
302    },
303    /// Divide scalar single-precision floating-point instruction.
304    Divss {
305        /// The destination operand.
306        dst: Operand,
307        /// The source operand.
308        src: Operand,
309    },
310    /// Maximum scalar single-precision floating-point instruction.
311    Maxss {
312        /// The destination operand.
313        dst: Operand,
314        /// The source operand.
315        src: Operand,
316    },
317    /// Bitwise XOR instruction.
318    Xor {
319        /// The destination operand.
320        dst: Operand,
321        /// The source operand.
322        src: Operand,
323    },
324    /// Bitwise XOR for XMM registers (used for zeroing).
325    Xorps {
326        /// The destination operand.
327        dst: Operand,
328        /// The source operand.
329        src: Operand,
330    },
331    /// Signed integer multiplication instruction.
332    Imul {
333        /// The destination register.
334        dst: Register,
335        /// The source operand.
336        src: Operand,
337    },
338    /// Unsigned integer multiplication instruction.
339    Mul {
340        /// The source operand.
341        src: Operand,
342    },
343    /// Unsigned integer division instruction.
344    Div {
345        /// The source operand.
346        src: Operand,
347    },
348    /// Signed integer division instruction.
349    Idiv {
350        /// The source operand.
351        src: Operand,
352    },
353    /// Sign-extend RAX to RDX:RAX (Convert Quadword to Octoword).
354    Cqo,
355    /// Bitwise AND instruction.
356    And {
357        /// The destination operand.
358        dst: Operand,
359        /// The source operand.
360        src: Operand,
361    },
362    /// Bitwise OR instruction.
363    Or {
364        /// The destination operand.
365        dst: Operand,
366        /// The source operand.
367        src: Operand,
368    },
369    /// Bitwise NOT instruction.
370    Not {
371        /// The operand to invert.
372        op: Operand,
373    },
374    /// Two's complement negation instruction.
375    Neg {
376        /// The operand to negate.
377        op: Operand,
378    },
379    /// Shift left logical instruction.
380    Shl {
381        /// The destination operand.
382        dst: Operand,
383        /// The source operand (shift count).
384        src: Operand,
385    },
386    /// Shift right logical instruction.
387    Shr {
388        /// The destination operand.
389        dst: Operand,
390        /// The source operand (shift count).
391        src: Operand,
392    },
393    /// Test instruction (non-destructive AND).
394    Test {
395        /// The destination operand.
396        dst: Operand,
397        /// The source operand.
398        src: Operand,
399    },
400    /// Conditional set byte instruction.
401    Setcc {
402        /// The condition code.
403        cond: Condition,
404        /// The destination operand.
405        dst: Operand,
406    },
407    /// Set byte if equal instruction.
408    Sete {
409        /// The destination operand.
410        dst: Operand,
411    },
412    /// Set byte if not equal instruction.
413    Setne {
414        /// The destination operand.
415        dst: Operand,
416    },
417    /// Set byte if less instruction.
418    Setl {
419        /// The destination operand.
420        dst: Operand,
421    },
422    /// Set byte if less or equal instruction.
423    Setle {
424        /// The destination operand.
425        dst: Operand,
426    },
427    /// Set byte if greater instruction.
428    Setg {
429        /// The destination operand.
430        dst: Operand,
431    },
432    /// Set byte if greater or equal instruction.
433    Setge {
434        /// The destination operand.
435        dst: Operand,
436    },
437    /// Move with sign extension instruction.
438    Movsx {
439        /// The destination register.
440        dst: Register,
441        /// The source operand.
442        src: Operand,
443        /// The size of the source operand in bits.
444        size: u8,
445    },
446    /// Move with zero extension instruction.
447    Movzx {
448        /// The destination register.
449        dst: Register,
450        /// The source operand.
451        src: Operand,
452        /// The size of the source operand in bits.
453        size: u8,
454    },
455}
456
457/// 条件码枚举,用于Jcc和Setcc指令
458#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
459pub enum Condition {
460    /// 相等 (Equal) / 零 (Zero)
461    E,
462    /// 不相等 (Not Equal) / 非零 (Not Zero)
463    NE,
464    /// 小于 (Less)
465    L,
466    /// 小于等于 (Less or Equal)
467    LE,
468    /// 大于 (Greater)
469    G,
470    /// 大于等于 (Greater or Equal)
471    GE,
472    /// 无符号低于 (Below)
473    B,
474    /// 无符号低于等于 (Below or Equal)
475    BE,
476    /// 无符号高于 (Above)
477    A,
478    /// 无符号高于等于 (Above or Equal)
479    AE,
480    /// 进位 (Carry)
481    C,
482    /// 无进位 (Not Carry)
483    NC,
484    /// 溢出 (Overflow)
485    O,
486    /// 无溢出 (Not Overflow)
487    NO,
488    /// 符号 (Sign)
489    S,
490    /// 无符号 (Not Sign)
491    NS,
492    /// 奇偶性为偶 (Parity Even)
493    PE,
494    /// 奇偶性为奇 (Parity Odd)
495    PO,
496}
497
498impl Operand {
499    /// 创建寄存器操作数
500    pub fn reg(reg: Register) -> Self {
501        Operand::Reg(reg)
502    }
503    /// 创建立即数操作数
504    pub fn imm(value: i64, size: u8) -> Self {
505        Operand::Imm { value, size }
506    }
507    /// 创建内存操作数
508    pub fn mem(base: Option<Register>, index: Option<Register>, scale: u8, displacement: i32) -> Self {
509        Operand::Mem { base, index, scale, displacement }
510    }
511    /// 创建标签操作数
512    pub fn label(name: String) -> Self {
513        Operand::Label(name)
514    }
515}
516
517impl std::fmt::Display for Operand {
518    /// 格式化显示操作数
519    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
520        match self {
521            Operand::Reg(reg) => write!(f, "{:?}", reg),
522            Operand::Imm { value, .. } => write!(f, "0x{:x}", value),
523            Operand::Mem { base, index, scale, displacement } => {
524                write!(f, "[")?;
525                if let Some(base) = base {
526                    write!(f, "{:?}", base)?;
527                }
528                if let Some(index) = index {
529                    if base.is_some() {
530                        write!(f, " + ")?;
531                    }
532                    write!(f, "{:?}", index)?;
533                    if *scale > 1 {
534                        write!(f, " * {}", scale)?;
535                    }
536                }
537                if *displacement != 0 {
538                    if base.is_some() || index.is_some() {
539                        if *displacement > 0 {
540                            write!(f, " + 0x{:x}", displacement)?;
541                        }
542                        else {
543                            write!(f, " - 0x{:x}", -displacement)?;
544                        }
545                    }
546                    else {
547                        write!(f, "0x{:x}", displacement)?;
548                    }
549                }
550                write!(f, "]")?;
551                Ok(())
552            }
553            Operand::Label(name) => write!(f, "{}", name),
554        }
555    }
556}