Class: Vm::Instructions::MemoryAccessOperations::VirtualSegmentOperation
- Inherits:
-
Object
- Object
- Vm::Instructions::MemoryAccessOperations::VirtualSegmentOperation
- 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
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#segment ⇒ Object
readonly
Returns the value of attribute segment.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(operation, segment, index) ⇒ VirtualSegmentOperation
constructor
A new instance of VirtualSegmentOperation.
- #to_asm ⇒ Object
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
#index ⇒ Object (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 |
#operation ⇒ Object (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 |
#segment ⇒ Object (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
.segments ⇒ Object
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_asm ⇒ Object
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 |