Class: SCASM::Instruction

Inherits:
Statement show all
Defined in:
lib/scasm/statement.rb

Instance Attribute Summary collapse

Attributes inherited from Statement

#addr

Instance Method Summary collapse

Methods inherited from Statement

#length

Constructor Details

#initialize(opsym, a, b) ⇒ Instruction

Returns a new instance of Instruction.



28
29
30
31
32
# File 'lib/scasm/statement.rb', line 28

def initialize opsym, a, b
  @opsym = opsym
  @a = a
  @b = b
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



25
26
27
# File 'lib/scasm/statement.rb', line 25

def a
  @a
end

#bObject

Returns the value of attribute b.



25
26
27
# File 'lib/scasm/statement.rb', line 25

def b
  @b
end

#opsymObject (readonly)

Returns the value of attribute opsym.



25
26
27
# File 'lib/scasm/statement.rb', line 25

def opsym
  @opsym
end

Instance Method Details

#assemble(io) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scasm/statement.rb', line 34

def assemble io
  if opcode = BASIC_OPCODES[@opsym]
    code_a, imm_a = @a.assemble
    code_b, imm_b = @b.assemble
    code = opcode | (code_a<<4) | (code_b<<10)
    io.write [code, imm_a, imm_b].compact.pack('v*')
  elsif opcode = EXTENDED_OPCODES[@opsym]
    code_a, imm_a = @a.assemble
    fail unless @b == nil
    code = (opcode<<4) | (code_a<<10)
    io.write [code, imm_a].compact.pack('v*')
  else
    fail "unknown opsym #{@opsym.inspect}"
  end
end

#to_sObject



50
51
52
# File 'lib/scasm/statement.rb', line 50

def to_s
  "%s %s, %s" % [@opsym, @a, @b]
end