Skip to content

Commit

Permalink
Suppress error for empty instruction (No-op)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderick14 committed Oct 10, 2017
1 parent f627cd4 commit e73e3e0
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Memory/InstructionMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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 ") {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit e73e3e0

Please sign in to comment.