Class: Vm::Instructions::MemoryAccessOperations::StaticOperation

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, index) ⇒ StaticOperation

Returns a new instance of StaticOperation.



8
9
10
11
# File 'lib/hackasm/vm/instructions/memory_access_operations/static_operation.rb', line 8

def initialize(object_name, index)
  @index = index
  @object_name = object_name
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/hackasm/vm/instructions/memory_access_operations/static_operation.rb', line 6

def index
  @index
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



6
7
8
# File 'lib/hackasm/vm/instructions/memory_access_operations/static_operation.rb', line 6

def object_name
  @object_name
end

Class Method Details

.segmentsObject



37
38
39
# File 'lib/hackasm/vm/instructions/memory_access_operations/static_operation.rb', line 37

def self.segments
  %w[static]
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
# File 'lib/hackasm/vm/instructions/memory_access_operations/static_operation.rb', line 13

def to_asm
  if operation == "push"
    %Q{
      @#{object_name}.#{index}
      D=M
      @SP
      A=M
      M=D
      @SP
      M=M+1
    }.strip
  else
    %Q{
      @SP
      M=M-1
      @SP
      A=M
      D=M
      @#{object_name}.#{index}
      M=D
    }.strip
  end
end