diff --git a/lib/register/instruction.rb b/lib/register/instruction.rb index 3e53acb2..13501cde 100644 --- a/lib/register/instruction.rb +++ b/lib/register/instruction.rb @@ -117,6 +117,7 @@ module Register end +require_relative "instructions/setter" require_relative "instructions/set_slot" require_relative "instructions/get_slot" require_relative "instructions/set_byte" diff --git a/lib/register/instructions/set_byte.rb b/lib/register/instructions/set_byte.rb index 81641851..c2a3116c 100644 --- a/lib/register/instructions/set_byte.rb +++ b/lib/register/instructions/set_byte.rb @@ -4,22 +4,8 @@ module Register # indexes are 1 based ! - class SetByte < Instruction + class SetByte < Setter - # If you had a c array (off int8) and index offset (>0) - # the instruction would do array[index] = register - # So SetByte means the register (first argument) moves to the slot (array and index) - def initialize source , register , array , index - super(source) - @register = register - @array = array - @index = index - raise "index 0 " if index == 0 - raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) - raise "Not register #{register}" unless RegisterValue.look_like_reg(register) - raise "Not register #{array}" unless RegisterValue.look_like_reg(array) - end - attr_accessor :register , :array , :index def to_s "SetByte: #{register} -> #{array} [#{index}]" end diff --git a/lib/register/instructions/set_slot.rb b/lib/register/instructions/set_slot.rb index 37a4ef7b..d0a3f1f0 100644 --- a/lib/register/instructions/set_slot.rb +++ b/lib/register/instructions/set_slot.rb @@ -4,30 +4,22 @@ module Register # GetSlot moves data into a register from memory. # Both use a base memory (a register) - # While the virtual machine has only one instruction (Set) to move data between slots, - # the register has two, namely GetSlot and SetSlot - # # This is because that is what cpu's can do. In programming terms this would be accessing # an element in an array, in the case of SetSlot setting the register in the array. # btw: to move data between registers, use RegisterTransfer - class SetSlot < Instruction + class SetSlot < Setter # If you had a c array and index offset # the instruction would do array[index] = register # So SetSlot means the register (first argument) moves to the slot (array and index) - def initialize source , register , array , index - super(source) - @register = register - @array = array - @index = index - raise "index 0 " if index == 0 - raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) - raise "Not register #{register}" unless RegisterValue.look_like_reg(register) - raise "Not register #{array}" unless RegisterValue.look_like_reg(array) - end - attr_accessor :register , :array , :index + + # def initialize source , register , array , index + # super + # end + # attr_accessor :register , :array , :index + def to_s "SetSlot: #{register} -> #{array} [#{index}]" end diff --git a/lib/register/instructions/setter.rb b/lib/register/instructions/setter.rb new file mode 100644 index 00000000..1d72109f --- /dev/null +++ b/lib/register/instructions/setter.rb @@ -0,0 +1,29 @@ +module Register + # Setter is a base class for set instructions (SetSlot and SetByte , possibly more coming) + # + # The instruction that is modelled is loading data from an array into a register + # + # Setter has a + # - Register that the data is moved to + # - an array where the data comes from + # - and (array) index + class Setter < Instruction + + # If you had a c array and index offset + # the instruction would do array[index] = register + # So Setter means the register (first argument) moves to the slot (array and index) + def initialize source , register , array , index + super(source) + @register = register + @array = array + @index = index + raise "index 0 " if index == 0 + raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) + raise "Not register #{register}" unless RegisterValue.look_like_reg(register) + raise "Not register #{array}" unless RegisterValue.look_like_reg(array) + end + attr_accessor :register , :array , :index + + end + +end