Merge pull request 'Новая инструкция: rjmp' (#1) from rjmp into master

Reviewed-on: #1
This commit is contained in:
n3tael 2024-02-08 18:45:11 +00:00
commit 84724f8000
3 changed files with 12 additions and 1 deletions

View File

@ -89,6 +89,7 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
// Labels // Labels
"jmp" => instructions::jmp::jmp(&mut stack.labels, &mut stack.program_counter, instruction.data), "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), "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), "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), "kjnz" => instructions::kjnz::kjnz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, instruction.data),

View File

@ -38,4 +38,5 @@ pub mod kjz;
pub mod print; pub mod print;
pub mod println; pub mod println;
pub mod insert; pub mod insert;
pub mod random; pub mod random;
pub mod rjmp;

9
src/instructions/rjmp.rs Normal file
View File

@ -0,0 +1,9 @@
use crate::{errors::RunError, stack::StackVec};
pub fn rjmp(memory: &mut StackVec, labels: &mut [Option<i16>; 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[rand].expect(&format!("{}", RunError::UnknownLabel(rand as u16))) - 1) as u16;
}