Class: Vm::Instructions::MemoryAccessOperations::VirtualSegmentOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb

Constant Summary collapse

VIRTUAL_SEGMENT_TO_POINTER_NAME =
{
  'argument' => 'ARG',
  'local' => 'LCL',
  'this' => 'THIS',
  'that' => 'THAT',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, segment, index) ⇒ VirtualSegmentOperation

Returns a new instance of VirtualSegmentOperation.



15
16
17
18
19
# File 'lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb', line 15

def initialize(operation, segment, index)
  @operation = operation
  @segment = segment
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



13
14
15
# File 'lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb', line 13

def index
  @index
end

#operationObject (readonly)

Returns the value of attribute operation.



13
14
15
# File 'lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb', line 13

def operation
  @operation
end

#segmentObject (readonly)

Returns the value of attribute segment.



13
14
15
# File 'lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb', line 13

def segment
  @segment
end

Class Method Details

.segmentsObject



59
60
61
# File 'lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb', line 59

def self.segments
  VIRTUAL_SEGMENT_TO_POINTER_NAME.keys
end

Instance Method Details

#to_asmObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb', line 21

def to_asm
  if operation == "push"
    %Q{
      @#{index}
      D=A
      @#{VIRTUAL_SEGMENT_TO_POINTER_NAME[segment]}
      A=M+D
      D=M
      @SP
      A=M
      M=D
      @SP
      M=M+1
    }.strip
  else
    %Q{
      @SP
      M=M-1
      @SP
      A=M
      D=M
      @R14
      M=D
      @#{index}
      D=A
      @#{VIRTUAL_SEGMENT_TO_POINTER_NAME[segment]}
      D=M+D
      @R15
      M=D
      @R14
      D=M
      @R15
      A=M
      M=D
    }.strip
  end
end