From 3181d2c80d2463830dbf0c1c72be5aad352f7f3f Mon Sep 17 00:00:00 2001 From: Richard Date: Sat, 23 Aug 2025 12:24:37 +0200 Subject: [PATCH] disas --- src/bin/umdis.rs | 30 ++++++++++++++++++++++++++---- src/instruction.rs | 31 ++++++++++++++++--------------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/bin/umdis.rs b/src/bin/umdis.rs index e3b96b8..eb69db3 100644 --- a/src/bin/umdis.rs +++ b/src/bin/umdis.rs @@ -1,10 +1,32 @@ extern crate um; use um::instruction::{Instruction, ParsingError}; +use std::io::Read; fn main() { - let i = Instruction::parse(0x81234567); - match i { - Ok(x) => println!("Ins {:?}!", x.fmt()), - Err(ParsingError) => println!("OOPS") + let mut stdin = std::io::stdin().lock(); + let mut cont = true; + while cont { + let mut buffer = [0u8; 4]; + cont = match stdin.read(&mut buffer) { + Ok(4) => hdl(buffer), + Ok(_) => false, + Err(_) => false, + }; + } +} + +fn hdl(buffer: [u8; 4]) -> bool{ + let num = u32::from_be_bytes( + buffer.try_into().unwrap()); + let i = Instruction::parse(num); + match i { + Ok(x) => { + println!("{}", x.fmt()); + true + }, + Err(ParsingError) => { + println!("parsingerror"); + false + } } } diff --git a/src/instruction.rs b/src/instruction.rs index 09c2324..52a02e8 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -39,22 +39,23 @@ const fn u3at(d: u32, pos: u8) -> u3 { } impl Instruction { - pub fn fmt(&self) -> &'static str { + pub fn fmt(&self) -> String { match self { - Instruction::ConditionalMove(i) => "CMV", - Instruction::ArrayIndex(i) => "IDX", - Instruction::ArrayAmend(i) => "AMD", - Instruction::Add(i) => "ADD", - Instruction::Multiply(i) => "MUL", - Instruction::Divide(i) => "DIV", - Instruction::NotAnd(i) => "NAD", - Instruction::Halt(i) => "HLT", - Instruction::Allocate(i) => "ALC", - Instruction::Abandon(i) => "ABN", - Instruction::Output(i) => "OUT", - Instruction::Input(i) => "INP", - Instruction::Load(i) => "LOD", - Instruction::Ortho(i) => "ORT" + Instruction::ConditionalMove(i) => format!("CMV r{}=r{} if r{}", i. +a, i.b, i.c), + Instruction::ArrayIndex(i) => format!("IDX r{}=r{}[r{}]", i.a, i.b, i.c) , + Instruction::ArrayAmend(i) => format!("AMD r{}[r{}]=r{}", i.a, i.b, i.c), + Instruction::Add(i) => format!("ADD r{}=r{}+r{}", i.a, i.b, i.c), + Instruction::Multiply(i) => format!("MUL r{}=r{}*r{}", i.a, i.b, i.c), + Instruction::Divide(i) => format!("DIV r{}=r{}/r{}", i.a, i.b, i.c), + Instruction::NotAnd(i) => format!("NAD r{}=r{} nand r{}", i.a, i.b, i.c), + Instruction::Halt(i) => format!("HLT"), + Instruction::Allocate(i) => format!("ALC r{} r{}", i.b, i.c), + Instruction::Abandon(i) => format!("ABN r{}", i.c), + Instruction::Output(i) => format!("OUT r{}", i.c), + Instruction::Input(i) => format!("INP r{}", i.c), + Instruction::Load(i) => format!("LOD r{}@r{}", i.b, i.c), + Instruction::Ortho(i) => format!("ORT r{}={}", i.a, i.v) } }