x64asm/lib.rs
1// This file is part of "x64asm"
2// Under the MIT License
3// Copyright (c) 2023 Antonin Hérault
4
5pub mod convert;
6pub mod instruction;
7
8pub use instruction::{ Instruction, InstructionBuilder };
9
10// Macros
11pub use ddirective as dd;
12pub use operand_label as oplabel;
13pub use _instruction as instruction;
14pub use _instruction as i;
15pub use operand_string as opstring;
16pub use operand_expression as opexpr;
17pub use register as reg;
18
19/// Permit to use the macros easily.
20/// ## Example
21/// ```rust
22/// use x64asm::macros::*;
23///
24/// i!(Mov, reg!(Rax), Op::Literal(1));
25/// ```
26/// and not :
27/// ```
28/// use x64asm::{ i, reg };
29/// use x64asm::{ instruction, InstructionBuilder };
30/// use x64asm::instruction::{ Mnemonic, Register, Op };
31///
32/// i!(Mnemonic::Mov, reg!(Register::Rax), Op::Literal(1));
33/// ```
34pub mod macros {
35 pub use super::{ instruction, InstructionBuilder };
36 pub use super::instruction::{ Mnemonic::*, Section::*, Reg::*, DD::*, Op };
37 pub use crate::{ dd, oplabel, i, opstring, opexpr, reg, section, label };
38}
39
40#[cfg(test)]
41mod tests {
42 #[test]
43 fn example() {
44 use std::path::Path;
45 use std::fs::File;
46 use std::io::prelude::*;
47
48 use super::convert::{ ToAssembly, Separator };
49 use super::macros::*;
50
51 let instructions = vec![
52 i!(Global, oplabel!("_start")),
53
54 i!(section!(Text)),
55 i!(label!("_start")),
56 i!(Mov, reg!(Rax), Op::Literal(1)),
57 i!(Mov, reg!(Rdi), Op::Literal(1)),
58 i!(Mov, reg!(Rsi), oplabel!("msg")),
59 i!(Mov, reg!(Rdx), oplabel!("msg_len")),
60 i!(Syscall),
61
62 i!(Mov, reg!(Rax), Op::Literal(60)),
63 i!(Mov, reg!(Rdi), Op::Literal(0)),
64 i!(Syscall),
65
66 i!(section!(Data)),
67 i!(label!("msg"), dd!(Db), opstring!("Hello world")),
68 i!(label!("msg_len"), dd!(Equ), opexpr!("$ - msg")),
69 ];
70
71 let code = instructions.to_assembly(Separator::Space);
72 let mut stream = File::create(&Path::new("target/test.asm")).unwrap();
73 write!(stream, "{}", code).unwrap();
74 }
75
76 // /// This won't work with the new version of "x64asm". Here is a vestige of what
77 // /// it looked like before :
78 // #[test]
79 // fn old_example() {
80 // use super::{
81 // ddirective, ddirective::DefineDirective::*,
82 // formatter::Formatter,
83 // instruction as i, label,
84 // mnemonic::Mnemonic::*,
85 // operand::Op,
86 // reg, register::Register::*,
87 // section, section::Section::*,
88 // };
89
90 // let mut x64asm_formatter = Formatter::new(false); // false : do not replace spaces by tabulations
91
92 // x64asm_formatter.add_instructions(&mut vec![
93 // i!(Global, Op::Label("_start".to_string())),
94
95 // i!(section!(Text)),
96
97 // i!(label!("_start")),
98 // i!(Mov, reg!(Rax), Op::Literal(1)),
99 // i!(Mov, reg!(Rdi), Op::Literal(1)),
100 // i!(Mov, reg!(Rsi), Op::Label("msg".to_string())),
101 // i!(Mov, reg!(Rdx), Op::Label("msg_len".to_string())),
102 // i!(Syscall),
103
104 // i!(Mov, reg!(Rax), Op::Literal(60)),
105 // i!(Mov, reg!(Rdi), Op::Literal(0)),
106 // i!(Syscall),
107
108 // i!(section!(Data)),
109
110 // i!(label!("msg"), ddirective!(Db), Op::String("Hello world".to_string())),
111 // i!(label!("msg_len"), ddirective!(Equ), Op::Expression("$ - msg".to_string())),
112 // ]);
113
114 // x64asm_formatter.to_file(&Path::new("target/test.asm")).unwrap();
115 // }
116}