class Rips::Instructions::Instruction

Public Class Methods

new(name, format) click to toggle source

@name: mnemonic name @format: instruction format @output: array with coded instruction

# File lib/rips/instructions/instruction.rb, line 18
def initialize (name, format)
  @name,@format = name,format
  @opcode = format.opcode
  @output = []
end

Public Instance Methods

args_number() click to toggle source

Return number of arguments

# File lib/rips/instructions/instruction.rb, line 40
def args_number
  @format.args_number
end
code() click to toggle source

Coding to Machine Code

# File lib/rips/instructions/instruction.rb, line 50
def code

  # Add opcode
  @output = [@opcode.to_bin(@length[:op])]

  # Add arguments
  @format.args.each do |key,value|
    @output << value.to_bin(@length[key])
  end

  # Add blanks
  if (@length.key? :blank)
    add_blank
  end

  @output.reverse.join.to_s
end
set_arguments(args) click to toggle source

Pass all arguments at once

# File lib/rips/instructions/instruction.rb, line 45
def set_arguments (args)
  @format.set_arguments(args)
end

Private Instance Methods

add_blank() click to toggle source

Add blanks (0 values) for instructions with free space

# File lib/rips/instructions/instruction.rb, line 27
def add_blank
  if @variables.empty?
    @output.push(0.to_bin(@length[:blank]))
  elsif @variables.size == 1
    @output.insert(-2,0.to_bin(@length[:blank]))
  else
    @output.insert(-@variables.size,0.to_bin(@length[:blank]))
  end
end