feat: initial set of instructions
This commit is contained in:
commit
921136e91e
4 changed files with 74 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
/target
|
||||||
|
|
||||||
|
# ASM Program
|
||||||
|
code.asm
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -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"
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "tinypc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
57
src/main.rs
Normal file
57
src/main.rs
Normal file
|
@ -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<String, i64> = HashMap::new();
|
||||||
|
|
||||||
|
for instruction in buffer.lines() {
|
||||||
|
let ops: Vec<String> = 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::<i64>().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::<i64>().unwrap(),
|
||||||
|
"SUB" => accumulator -= &ops[1].clone().parse::<i64>().unwrap(),
|
||||||
|
"HLT" => std::process::exit(0),
|
||||||
|
&_ => {
|
||||||
|
match ops[1].as_str() {
|
||||||
|
"DAT" => {
|
||||||
|
memory.insert(
|
||||||
|
ops[0].clone(),
|
||||||
|
ops[2].clone().parse::<i64>().unwrap(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
&_ => {
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in a new issue