Class: Vm::Instructions::ArithmeticOperations::BinaryOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb

Constant Summary collapse

OPERATION_TO_INSTRUCTION =
{
  "add" => "M=M+D",
  "sub" => "M=M-D",
  "and" => "M=M&D",
  "or" => "M=M|D",
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation) ⇒ BinaryOperation

Returns a new instance of BinaryOperation.



14
15
16
# File 'lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb', line 14

def initialize(operation)
  @operation = operation
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



12
13
14
# File 'lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb', line 12

def operation
  @operation
end

Class Method Details

.operationsObject



35
36
37
# File 'lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb', line 35

def self.operations
  OPERATION_TO_INSTRUCTION.keys
end

Instance Method Details

#to_asmObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb', line 18

def to_asm
  %Q{
    @SP
    M=M-1
    @SP
    A=M
    D=M
    @SP
    M=M-1
    @SP
    A=M
    #{instruction}
    @SP
    M=M+1
  }.strip
end