class Vm::Instructions::ArithmeticOperations::Comparison

Attributes

operation[R]

Public Class Methods

new(operation) click to toggle source
# File lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb, line 9
def initialize(operation)
  @operation = operation
end
operations() click to toggle source
# File lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb, line 43
def self.operations
  %w[gt lt eq]
end

Public Instance Methods

to_asm() click to toggle source
# File lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb, line 13
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