disassembler disassembles

This commit is contained in:
2024-10-21 13:18:46 -04:00
parent e927f6785f
commit 665309c2e4
6 changed files with 122 additions and 35 deletions
+40 -12
View File
@@ -15,34 +15,50 @@ struct DisassemblerApp {
output_file: Option<Box<Path>>,
}
struct Disassembler {}
impl Disassembler {
pub fn disassemble(from_data: Vec<u8>) -> String {
String::new()
}
}
fn main() {
println!("Taxation is Theft");
let result = DisassemblerApp::parse();
println!("PREPARING TO DISASSEMBLE {:?}", result.input_file.file_name());
let mut last_byte: u8 = 0x00;
// let target_file =
let source_file = result.input_file.to_str().unwrap();
let mut working_instruction: u16 = 0x0000;
let mut dump_as_data: bool = false;
// read the input file and loop through it byte by byte.
for (offset, byte) in std::fs::read(result.input_file).unwrap().iter().enumerate() {
let mut output_string = String::new();
for (offset, byte) in std::fs::read(source_file).unwrap().iter().enumerate() {
working_instruction = (working_instruction << 8) | (*byte as u16);
if offset % 2 != 0 {
let decoded = Chip8CpuInstructions::decode(working_instruction);
let decoded_string = decoded.to_string();
let mut current_parts = String::new();
match decoded {
XXXXERRORINSTRUCTION => {
println!("DW 0x{working_instruction:04x}{:>16}; {working_instruction:04x}", " ");
}
Chip8CpuInstructions::JPA(x) => {
if (offset + 0x200) == x as usize {
println!("INFINITE LOOP")
}
current_parts = format!("DW 0x{:04x}", working_instruction);
}
_ => {
let fill_len = 25 - decoded.to_string().len();
println!("{}{:<fill_len$}; Bytes [{:04x}] offset [{:04x}]", decoded, " ", working_instruction, offset - 1);
current_parts = format!("{}", decoded);
}
}
};
let target_length: i32 = 25;
let spacing_length = target_length.saturating_sub(current_parts.len() as i32);
// now add the rest after the string
let x = spacing_length as usize;
current_parts = format!("{}{:<x$}; Bytes [0x{:04x}] offset [0x{:04x}]\n", current_parts, " ", working_instruction, offset -1);
// println!("SHOULD OUTPUT: [{current_parts}]");
output_string = output_string.to_string() + &*current_parts.to_string();
working_instruction = 0x0000;
}
}
@@ -50,9 +66,21 @@ fn main() {
match result.output_file {
None => {
println!("Output to console.");
println!("OS: \n\n{}", output_string);
}
Some(target) => {
println!("Output to {:?}", target);
std::fs::write(target, output_string).unwrap();
}
}
}
mod test {
#[test]
fn smoke() { assert!(true); }
#[test]
fn assemble_test_file() {
}
}