Class: Vm::Instructions::MemoryAccessOperations::FixedSegmentOperation

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

Constant Summary collapse

FIXED_SEGMENT_TO_START_INDEX =
{
  'temp' => '5',
  'pointer' => '3',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, segment, index) ⇒ FixedSegmentOperation

Returns a new instance of FixedSegmentOperation.



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

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

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



10
11
12
# File 'lib/hackasm/vm/instructions/memory_access_operations/fixed_segment_operation.rb', line 10

def index
  @index
end

#operationObject (readonly)

Returns the value of attribute operation.



10
11
12
# File 'lib/hackasm/vm/instructions/memory_access_operations/fixed_segment_operation.rb', line 10

def operation
  @operation
end

#segmentObject (readonly)

Returns the value of attribute segment.



10
11
12
# File 'lib/hackasm/vm/instructions/memory_access_operations/fixed_segment_operation.rb', line 10

def segment
  @segment
end

Class Method Details

.segmentsObject



56
57
58
# File 'lib/hackasm/vm/instructions/memory_access_operations/fixed_segment_operation.rb', line 56

def self.segments
  FIXED_SEGMENT_TO_START_INDEX.keys
end

Instance Method Details

#to_asmObject



18
19
20
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
# File 'lib/hackasm/vm/instructions/memory_access_operations/fixed_segment_operation.rb', line 18

def to_asm
  if operation == "push"
    %Q{
      @#{index}
      D=A
      @#{FIXED_SEGMENT_TO_START_INDEX[segment]}
      A=A+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
      @#{FIXED_SEGMENT_TO_START_INDEX[segment]}
      D=A+D
      @R15
      M=D
      @R14
      D=M
      @R15
      A=M
      M=D
    }.strip
  end
end