From e73e3e0003e76f91745ab65699e2b5bcd9f23e3d Mon Sep 17 00:00:00 2001 From: coderick14 Date: Tue, 10 Oct 2017 22:37:39 +0530 Subject: [PATCH] Suppress error for empty instruction (No-op) --- Memory/InstructionMemory.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Memory/InstructionMemory.go b/Memory/InstructionMemory.go index 7b491df..32c8d9f 100644 --- a/Memory/InstructionMemory.go +++ b/Memory/InstructionMemory.go @@ -42,6 +42,11 @@ func IsValidPC(PC int64) bool { return isValidPC } +// isEmptyInstruction is a method to check for null instructions (NoOps) +func isEmptyInstruction(currentInstruction string) bool { + return len(currentInstruction) == 0 +} + // ExtractLabels is a method to extract labels from instructions. func (instructionMemory *InstructionMemory) ExtractLabels() { @@ -65,6 +70,11 @@ func (instructionMemory *InstructionMemory) ValidateAndExecuteInstruction() erro //get next instruction to be executed from instruction memory currentInstruction := instructionMemory.Instructions[instructionMemory.PC] + if isEmptyInstruction(currentInstruction) { + instructionMemory.updatePC() + return nil + } + var err error if strings.HasPrefix(currentInstruction, "ADD ") { @@ -450,7 +460,7 @@ func (instruction *AddImmediateInstruction) parse() error { instruction.constant = uint(constant) address := getRegisterValue(instruction.reg2) + int64(instruction.constant) - if address > MEMORY_SIZE * WORD_SIZE { + if address > MEMORY_SIZE*WORD_SIZE { return errors.New("Stack underflow error in : " + instruction.inst) } @@ -518,7 +528,7 @@ func (instruction *SubImmediateInstruction) parse() error { instruction.constant = uint(constant) address := getRegisterValue(instruction.reg2) + int64(instruction.constant) - if address < (MEMORY_SIZE - STACK_SIZE) * WORD_SIZE { + if address < (MEMORY_SIZE-STACK_SIZE)*WORD_SIZE { return errors.New("Stack overflow error in : " + instruction.inst) }