From 0a8f8dfb8ddb853c2cd8e657c29a3d8b6af701dd Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Sat, 20 Jan 2024 04:35:16 +0700 Subject: [PATCH] fix jmp illegal condition --- elm.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/elm.go b/elm.go index 1306cb8..5283ca8 100644 --- a/elm.go +++ b/elm.go @@ -139,7 +139,7 @@ func (m* Machine) Execute() error { m.sp-- m.ip++ case JMP: - if (m.ip < instr.Operand || instr.Operand < 0) { + if (len(m.program) < instr.Operand || instr.Operand < 0) { return errors.New("Illegal access") } @@ -149,7 +149,7 @@ func (m* Machine) Execute() error { return errors.New("Stack Underflow") } - if (m.ip < instr.Operand || instr.Operand < 0) { + if (len(m.program) <= instr.Operand || instr.Operand < 0) { return errors.New("Illegal access") } @@ -184,20 +184,22 @@ func main() { // m.Push(Inst{Operation: DIV}) // m.Push(Inst{Operation: HALT}) - m.Push(Inst{Operation: PUSH, Operand: 0}) m.Push(Inst{Operation: PUSH, Operand: 1}) - m.Push(Inst{Operation: ADD}) - m.Push(Inst{Operation: JMP, Operand: 1}) + m.Push(Inst{Operation: JMPIF, Operand: 3}) + m.Push(Inst{Operation: HALT}) + m.Push(Inst{Operation: PUSH, Operand: 1}) + m.Push(Inst{Operation: PUSH, Operand: 2}) m.Push(Inst{Operation: HALT}) stackCounter := 0 for !m.isHalted && stackCounter < 69 { - m.Print() err := m.Execute() + m.Print() if err != nil { fmt.Fprintf(os.Stderr, "[ERR] %s\n", err); os.Exit(1) } + stackCounter++ }