Files
8086-rs/src/main.rs
2025-07-08 10:42:58 +09:00

71 lines
1.8 KiB
Rust

use clap::{Parser, Subcommand};
use disasm::Disassembler;
use env_logger::Builder;
use interpreter::interpreter::Interpreter;
use std::io::Write;
mod aout;
mod disasm;
mod disasm_macros;
mod instructions;
mod interpreter;
mod operands;
mod register;
#[derive(Subcommand, Debug)]
enum Command {
/// Disassemble the binary into 8086 instructions
#[clap(visible_alias("d"))]
Disassemble,
/// Interpret the 8086 instructions
#[clap(visible_alias("i"))]
Interpret,
}
/// Simple program to disassemble and interpret 8086 a.out compilates, e.g.
/// such for MINIX.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[command(subcommand)]
command: Command,
/// Path of the binary
#[arg(short, long, global = true)]
path: Option<String>,
/// Dump progress of disassembly, in case of encountering an error.
#[arg(short, long, global = true, action)]
dump: bool,
/// argv passed to the program, which will be interpreted
#[arg(trailing_var_arg = true, global = true)]
argv: Vec<String>,
}
fn main() {
Builder::new()
.format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args()))
.parse_default_env()
.init();
let args = Args::parse();
log::debug!("{:?}", args);
match args.command {
Command::Disassemble => {
let mut disasm = Disassembler::new(&args);
let instructions = disasm.disassemble(args.dump);
match instructions {
Ok(instrs) => instrs.iter().for_each(|i| println!("{i}")),
_ => {}
}
}
Command::Interpret => {
let mut interpreter = Interpreter::new(&args);
interpreter.interpret().unwrap();
}
}
}