From b3d1380eba073d7e6d45f93c677bb9fc72aa3433 Mon Sep 17 00:00:00 2001 From: n3tael Date: Thu, 8 Feb 2024 20:04:02 +0200 Subject: [PATCH 1/2] new instruction rjmp --- src/execute.rs | 1 + src/instructions/mod.rs | 3 ++- src/instructions/rjmp.rs | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 src/instructions/rjmp.rs diff --git a/src/execute.rs b/src/execute.rs index 25dc1ee..978a7bb 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -89,6 +89,7 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name // Labels "jmp" => instructions::jmp::jmp(&mut stack.labels, &mut stack.program_counter, instruction.data), + "rjmp" => instructions::rjmp::rjmp(&mut stack.memory, &mut stack.labels, &mut stack.program_counter), "jnz" => instructions::jnz::jnz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, instruction.data), "jz" => instructions::jz::jz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, instruction.data), "kjnz" => instructions::kjnz::kjnz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, instruction.data), diff --git a/src/instructions/mod.rs b/src/instructions/mod.rs index 516a864..dff22a4 100644 --- a/src/instructions/mod.rs +++ b/src/instructions/mod.rs @@ -38,4 +38,5 @@ pub mod kjz; pub mod print; pub mod println; pub mod insert; -pub mod random; \ No newline at end of file +pub mod random; +pub mod rjmp; \ No newline at end of file diff --git a/src/instructions/rjmp.rs b/src/instructions/rjmp.rs new file mode 100644 index 0000000..71b5f90 --- /dev/null +++ b/src/instructions/rjmp.rs @@ -0,0 +1,8 @@ +use crate::stack::StackVec; + +pub fn rjmp(memory: &mut StackVec, labels: &mut [Option; 256], program_counter: &mut u16) { + let start = memory.pop() as usize; + let end = memory.pop() as usize; + + *program_counter = (labels[fastrand::usize(start..end)].unwrap() - 1) as u16; +} \ No newline at end of file From de2d98bfec164e12c2711c8c394057d5678ae7ea Mon Sep 17 00:00:00 2001 From: n3tael Date: Thu, 8 Feb 2024 20:29:37 +0200 Subject: [PATCH 2/2] fix --- src/instructions/rjmp.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/instructions/rjmp.rs b/src/instructions/rjmp.rs index 71b5f90..c3922d2 100644 --- a/src/instructions/rjmp.rs +++ b/src/instructions/rjmp.rs @@ -1,8 +1,9 @@ -use crate::stack::StackVec; +use crate::{errors::RunError, stack::StackVec}; pub fn rjmp(memory: &mut StackVec, labels: &mut [Option; 256], program_counter: &mut u16) { let start = memory.pop() as usize; let end = memory.pop() as usize; + let rand = fastrand::usize(start..end + 1); - *program_counter = (labels[fastrand::usize(start..end)].unwrap() - 1) as u16; + *program_counter = (labels[rand].expect(&format!("{}", RunError::UnknownLabel(rand as u16))) - 1) as u16; } \ No newline at end of file