Class: Rips::Instructions::Instruction

Inherits:
Object
  • Object
show all
Defined in:
lib/rips/instructions/instruction.rb

Direct Known Subclasses

Add, And, Beqz, Bnez, J, Jal, Ji, Jr, Lesr, Li, Move, Neg, Nop, Not, Or, Sesm, Sesr, Sub

Instance Method Summary collapse

Constructor Details

#initialize(name, format) ⇒ Instruction

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



13
14
15
16
17
# File 'lib/rips/instructions/instruction.rb', line 13

def initialize (name, format)
  @name,@format = name,format
  @opcode = format.opcode
  @output = []
end

Instance Method Details

#add_blankObject

Add blanks (0 values) for instructions with free space



30
31
32
33
34
35
36
37
38
# File 'lib/rips/instructions/instruction.rb', line 30

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

#args_numberObject

Return number of arguments



20
21
22
# File 'lib/rips/instructions/instruction.rb', line 20

def args_number
  @format.args_number
end

#codeObject

Coding to Machine Code



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rips/instructions/instruction.rb', line 41

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) ⇒ Object

Pass all arguments at once



25
26
27
# File 'lib/rips/instructions/instruction.rb', line 25

def set_arguments (args)
  @format.set_arguments(args)
end