Sha256: 4eb7e3947bd57652ccf5acc55dbbd578fc885bf5ce8ec0f9a281ca8dac7cab01

Contents?: true

Size: 927 Bytes

Versions: 1

Compression:

Stored size: 927 Bytes

Contents

require 'securerandom'

module Vm
  module Instructions
    module ArithmeticOperations
      class Comparison
        attr_reader :operation

        def initialize(operation)
          @operation = operation
        end

        def to_asm
          if_label = 'c_if_' + SecureRandom.hex(10)
          else_label = 'c_else_' + SecureRandom.hex(10)
          %Q{
            @SP
            M=M-1
            @SP
            A=M
            D=M
            @SP
            M=M-1
            @SP
            A=M
            D=M-D
            @#{if_label}
            D;J#{operation.upcase}
            D=0
            @#{else_label}
            0;JEQ
            (#{if_label})
            D=-1
            (#{else_label})
            @SP
            A=M
            M=D
            @SP
            M=M+1
          }.strip
        end

        def self.operations
          %w[gt lt eq]
        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/comparison.rb