Skip to content

Commit 921136e

Browse files
committed
feat: initial set of instructions
0 parents  commit 921136e

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
3+
# ASM Program
4+
code.asm

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "tinypc"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]

src/main.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::fs::File;
2+
use std::io::{self, Result, prelude::*, BufReader};
3+
use std::collections::HashMap;
4+
5+
fn main() -> Result<()> {
6+
let code = File::open("code.asm")?;
7+
let buffer = BufReader::new(&code);
8+
let mut accumulator = 0;
9+
10+
let mut memory: HashMap<String, i64> = HashMap::new();
11+
12+
for instruction in buffer.lines() {
13+
let ops: Vec<String> = instruction?
14+
.split_whitespace()
15+
.map(String::from)
16+
.collect();
17+
18+
match ops[0].as_str() {
19+
"INP" => {
20+
let mut s = String::new();
21+
io::stdin().read_line(&mut s).unwrap();
22+
accumulator = s.trim().parse::<i64>().unwrap();
23+
}
24+
"OUT" => println!("{}", accumulator),
25+
"STA" => {
26+
memory.insert(
27+
ops[1].clone(),
28+
accumulator
29+
);
30+
}
31+
"LDA" => {
32+
match memory.get(&ops[1].clone()) {
33+
Some(value) => accumulator = *value,
34+
None => panic!(),
35+
}
36+
}
37+
"ADD" => accumulator += &ops[1].clone().parse::<i64>().unwrap(),
38+
"SUB" => accumulator -= &ops[1].clone().parse::<i64>().unwrap(),
39+
"HLT" => std::process::exit(0),
40+
&_ => {
41+
match ops[1].as_str() {
42+
"DAT" => {
43+
memory.insert(
44+
ops[0].clone(),
45+
ops[2].clone().parse::<i64>().unwrap(),
46+
);
47+
}
48+
&_ => {
49+
panic!()
50+
}
51+
}
52+
}
53+
}
54+
}
55+
56+
Ok(())
57+
}

0 commit comments

Comments
 (0)