Class: Assembler::Instructions::AddressInstruction

Inherits:
Object
  • Object
show all
Defined in:
lib/hackasm/assembler/instructions/address_instruction.rb

Instance Method Summary collapse

Constructor Details

#initialize(instruction, symbol_table) ⇒ AddressInstruction

Returns a new instance of AddressInstruction.



4
5
6
7
# File 'lib/hackasm/assembler/instructions/address_instruction.rb', line 4

def initialize(instruction, symbol_table)
  @instruction = instruction.compact.map { |key, value| [key, value.to_s] }.to_h
  @symbol_table = symbol_table
end

Instance Method Details

#to_bObject



9
10
11
12
13
14
15
# File 'lib/hackasm/assembler/instructions/address_instruction.rb', line 9

def to_b
  if memory_address = @instruction[:memory_address]
    memory_address.to_i
  elsif identifier = @instruction[:identifier]
    @symbol_table[identifier]
  end.to_s(2).rjust(16, '0')
end