1use fNET::{Decoder, DecoderOptions, FmtFormatter, Formatter, FormatterOptions, MonoDialect};
2
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut args = std::env::args().skip(1);
5 let Some(path) = args.next() else {
6 eprintln!(
7 "Usage: cargo run --features std --example disasm -- <raw-cil.bin> [hex-base-ip] [modern|legacy|july2002|early2002|may2004]"
8 );
9 std::process::exit(2);
10 };
11 let ip = args
12 .next()
13 .map(|value| u64::from_str_radix(value.trim_start_matches("0x"), 16))
14 .transpose()?
15 .unwrap_or(0);
16 let dialect = match args.next().as_deref().unwrap_or("modern") {
17 "modern" => MonoDialect::Modern,
18 "legacy" => MonoDialect::Legacy,
19 "july2002" => MonoDialect::July2002,
20 "early2002" => MonoDialect::Early2002,
21 "may2004" => MonoDialect::May2004,
22 _ => return Err("unknown Mono dialect".into()),
23 };
24 let bytes = std::fs::read(path)?;
25 let formatter = FmtFormatter::with_options(FormatterOptions {
26 show_address: true,
27 ..FormatterOptions::default()
28 });
29 for instruction in Decoder::new(&bytes, ip, DecoderOptions::NONE).with_mono_dialect(dialect) {
30 let mut text = String::new();
31 formatter.format(&instruction, &mut text)?;
32 if instruction.is_invalid() {
33 println!("{text} ; {}", instruction.error());
34 } else {
35 println!("{text}");
36 }
37 }
38 Ok(())
39}