From 1a813edae1b3f626a78c3bbbf73e0b073450f0fe Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Tue, 28 Jan 2025 10:30:38 +0000 Subject: [PATCH] feat: progress towards `BRP` and `BRZ` --- src/main.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 84d2147..28264f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ fn process( accumulator: &mut i64, memory: &mut HashMap, labels: &HashMap, - pc: &mut usize, + pc: usize, ) -> usize { let ops: Vec = line.split_whitespace().map(String::from).collect(); @@ -59,9 +59,27 @@ fn process( } "BRA" => { if ops.len() >= 3 { - *pc = *labels.get(&ops[2]).unwrap(); + return *labels.get(&ops[2]).unwrap(); } else { - *pc = *labels.get(&ops[1]).unwrap(); + return *labels.get(&ops[1]).unwrap(); + } + } + "BRP" => { + if *accumulator >= 0 { + if ops.len() >= 3 { + return *labels.get(&ops[2]).unwrap(); + } else { + return *labels.get(&ops[1]).unwrap(); + } + } + } + "BRZ" => { + if *accumulator == 0 { + if ops.len() >= 3 { + return *labels.get(&ops[2]).unwrap(); + } else { + return *labels.get(&ops[1]).unwrap(); + } } } "HLT" => std::process::exit(0), @@ -76,7 +94,7 @@ fn process( } } - 0 + return pc + 1; } fn main() -> io::Result<()> { @@ -94,28 +112,19 @@ fn main() -> io::Result<()> { } let ops: Vec = line.split_whitespace().map(String::from).collect(); if ops.len() > 1 && ops[1] == "DAT" { - labels.insert(ops[0].clone(), index); + labels.insert(ops[0].clone(), ops[2].clone().parse::().unwrap()); + println!("{:?}", labels); } else if ops.len() > 1 { labels.insert(ops[1].clone(), index); + println!("{:?}", labels); } code.push(line); } - let mut pc: usize = 0; + let mut pc: usize = 1; while pc < code.len() { let line = &code[pc]; - let next_pc = process( - line.to_string(), - &mut accumulator, - &mut memory, - &labels, - &mut pc, - ); - if next_pc != 0 { - pc = next_pc; - } else { - pc += 1; - } + pc = process(line.to_string(), &mut accumulator, &mut memory, &labels, pc); } Ok(())