Class: Vm::Instructions::ArithmeticOperations::Comparison

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation) ⇒ Comparison

Returns a new instance of Comparison.



9
10
11
# File 'lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb', line 9

def initialize(operation)
  @operation = operation
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



7
8
9
# File 'lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb', line 7

def operation
  @operation
end

Class Method Details

.operationsObject



43
44
45
# File 'lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb', line 43

def self.operations
  %w[gt lt eq]
end

Instance Method Details

#to_asmObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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