From 804da393a82713b8f088be3e16460208be35ecfe Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Fri, 31 Mar 2023 14:36:00 +0200 Subject: [PATCH] Make 'pop' operation safe --- examples/testforth.asm | 3 +++ examples/testforth.fth | 2 ++ hence/src/emulator.rs | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/examples/testforth.asm b/examples/testforth.asm index 1c5491f..8a25fa2 100644 --- a/examples/testforth.asm +++ b/examples/testforth.asm @@ -131,4 +131,7 @@ return_call_stack_jump: .std_alu CORE_ALU_GT tlr CORE_REG_A .std_cond_jump loop_strings_1 + dbg + pop + dbg .std_stop diff --git a/examples/testforth.fth b/examples/testforth.fth index 2694a60..4f7c413 100644 --- a/examples/testforth.fth +++ b/examples/testforth.fth @@ -2,3 +2,5 @@ ." test\n" ." jdsafhjfnjfn\n" + +debug drop debug diff --git a/hence/src/emulator.rs b/hence/src/emulator.rs index aaa58e1..64ed074 100644 --- a/hence/src/emulator.rs +++ b/hence/src/emulator.rs @@ -247,7 +247,11 @@ impl Emulator { self.reg_sp = self.reg_sp.wrapping_add(1); } 0x02 => { + let before = self.reg_sp; self.reg_sp = self.reg_sp.wrapping_sub(1); + if before < self.reg_sp { + self.reg_sp = 0; + } } 0x03 => { self.tmp = self.reg_arg;