Class: Tataru::Instructions::GotoIfInstruction

Inherits:
ImmediateModeInstruction show all
Defined in:
lib/tataru/instructions/goto_if_instruction.rb

Overview

goto if temp result is non zero

Instance Attribute Summary

Attributes inherited from Tataru::Instruction

#memory

Instance Method Summary collapse

Methods inherited from ImmediateModeInstruction

#initialize

Methods inherited from Tataru::Instruction

#execute, expects

Constructor Details

This class inherits a constructor from Tataru::Instructions::ImmediateModeInstruction

Instance Method Details

#label_branch!Object



17
18
19
20
21
22
23
# File 'lib/tataru/instructions/goto_if_instruction.rb', line 17

def label_branch!
  unless memory.hash[:labels]&.key?(@param)
    raise "Label '#{@param}' not found"
  end

  memory.hash[:labels][@param] - 1
end

#runObject



7
8
9
10
11
12
13
14
15
# File 'lib/tataru/instructions/goto_if_instruction.rb', line 7

def run
  return if memory.hash[:temp][:result].zero?

  memory.program_counter = if @param.is_a? Integer
                             @param - 1
                           else
                             label_branch!
                           end
end