From 921136e91e6f4e4df7432cda5e28d70a14002815 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 22 Jan 2025 12:45:43 +0000 Subject: [PATCH] feat: initial set of instructions --- .gitignore | 4 ++++ Cargo.lock | 7 +++++++ Cargo.toml | 6 ++++++ src/main.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86d31ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target + +# ASM Program +code.asm diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ec5cbfc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "tinypc" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..273cf04 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tinypc" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bdb4a4b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,57 @@ +use std::fs::File; +use std::io::{self, Result, prelude::*, BufReader}; +use std::collections::HashMap; + +fn main() -> Result<()> { + let code = File::open("code.asm")?; + let buffer = BufReader::new(&code); + let mut accumulator = 0; + + let mut memory: HashMap = HashMap::new(); + + for instruction in buffer.lines() { + let ops: Vec = instruction? + .split_whitespace() + .map(String::from) + .collect(); + + match ops[0].as_str() { + "INP" => { + let mut s = String::new(); + io::stdin().read_line(&mut s).unwrap(); + accumulator = s.trim().parse::().unwrap(); + } + "OUT" => println!("{}", accumulator), + "STA" => { + memory.insert( + ops[1].clone(), + accumulator + ); + } + "LDA" => { + match memory.get(&ops[1].clone()) { + Some(value) => accumulator = *value, + None => panic!(), + } + } + "ADD" => accumulator += &ops[1].clone().parse::().unwrap(), + "SUB" => accumulator -= &ops[1].clone().parse::().unwrap(), + "HLT" => std::process::exit(0), + &_ => { + match ops[1].as_str() { + "DAT" => { + memory.insert( + ops[0].clone(), + ops[2].clone().parse::().unwrap(), + ); + } + &_ => { + panic!() + } + } + } + } + } + + Ok(()) +}