Sha256: e5f932242902cc8d4f38fa78fc3ffbe698a9c648913b3d02046d9fb3b4ca32ef

Contents?: true

Size: 851 Bytes

Versions: 1

Compression:

Stored size: 851 Bytes

Contents

module Vm
  module Instructions
    module ArithmeticOperations
      class BinaryOperation
        OPERATION_TO_INSTRUCTION = {
          "add" => "M=M+D",
          "sub" => "M=M-D",
          "and" => "M=M&D",
          "or" => "M=M|D",
        }.freeze

        attr_reader :operation

        def initialize(operation)
          @operation = operation
        end

        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

        def self.operations
          OPERATION_TO_INSTRUCTION.keys
        end

        private

        def instruction
          OPERATION_TO_INSTRUCTION[operation]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hackasm-0.1.0 lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb