Ejemplo n.º 1
0
 def _assemble(self):
     return InstructionSeq("Bootstrap") \
                 .a_instruction("256") \
                 .c_instruction(dest="D", comp="A") \
                 .a_instruction("R0") \
                 .c_instruction(dest="M", comp="D") \
                 .call(Bootstrap.fxn_name, 0)
Ejemplo n.º 2
0
 def _assemble(self):
     insts = InstructionSeq("{} {} {}".format(self.cmd_name, self.source, self.offset))
     if self.source == MemorySegment.CONSTANT:
         insts = insts.const_to_stack(self.offset)
     elif self.source in [MemorySegment.LCL, MemorySegment.ARG,
         MemorySegment.THIS, MemorySegment.THAT, MemorySegment.TEMP]:
         insts = insts.mem_seg_to_stack(self.source, self.offset)
     elif self.source == MemorySegment.STATIC:
         insts = insts.static_to_stack(self._filename, self.offset)
     elif self.source == MemorySegment.POINTER:
         insts = insts.pointer_to_stack(self.offset)
     return insts.inc_sp()
Ejemplo n.º 3
0
 def assemble(self, kind):
     cmp_label = next(ComparisonCommand.label)
     jmp_label = next(ComparisonCommand.label)
     return InstructionSeq(self.cmd_name).dec_sp() \
                 .stack_to_register("D") \
                 .dec_sp() \
                 .stack_to_register("A") \
                 .c_instruction(dest="D", comp="A-D") \
                 .a_instruction(cmp_label) \
                 .c_instruction(comp="D", jump=kind) \
                 .push(False) \
                 .a_instruction(jmp_label) \
                 .c_instruction(comp="0", jump="JMP") \
                 .label(cmp_label) \
                 .push(True) \
                 .label(jmp_label) \
Ejemplo n.º 4
0
 def _assemble(self):
     insts = InstructionSeq("{} {} {}".format(self.cmd_name, self.source, self.offset))
     insts = insts.dec_sp()
     if self.source in [MemorySegment.LCL, MemorySegment.ARG,
         MemorySegment.THIS, MemorySegment.THAT, MemorySegment.TEMP]:
         insts = insts.stack_to_mem_seg(self.source, self.offset)
     elif self.source == MemorySegment.STATIC:
         insts = insts.stack_to_static(self._filename, self.offset)
     elif self.source == MemorySegment.POINTER:
         insts = insts.stack_to_pointer(self.offset)
     elif self.source == MemorySegment.CONSTANT:
         raise ValueError("pop constant _ is an invalid instruction")
     return insts
Ejemplo n.º 5
0
 def _assemble(self):
     return InstructionSeq(self.cmd_name) \
                 .a_instruction(MemorySegment.LCL) \
                 .c_instruction(dest="D", comp="M") \
                 .a_instruction(R_ENDFRAME) \
                 .store_from("D") \
                 .a_instruction(5) \
                 .c_instruction(dest="A", comp="D-A") \
                 .c_instruction(dest="D", comp="M") \
                 .a_instruction(R_RETADDR) \
                 .store_from("D") \
                 .dec_sp() \
                 .stack_to_mem_seg(MemorySegment.ARG, 0) \
                 .a_instruction(MemorySegment.ARG) \
                 .c_instruction(dest="D", comp="M+1") \
                 .a_instruction("SP") \
                 .store_from("D") \
                 .dereference_endframe(MemorySegment.THAT) \
                 .dereference_endframe(MemorySegment.THIS) \
                 .dereference_endframe(MemorySegment.ARG) \
                 .dereference_endframe(MemorySegment.LCL) \
                 .dereference(R_RETADDR) \
                 .c_instruction(comp="0", jump="JMP")
Ejemplo n.º 6
0
 def _assemble(self):
     return InstructionSeq("{} {} {}".format(self.cmd_name, self.fxn_name, self.nargs)) \
                 .call(self.fxn_name, self.nargs)
Ejemplo n.º 7
0
 def _assemble(self):
     insts = InstructionSeq("{} {} {}".format(self.cmd_name, self.name, self.nargs)) \
                 .label(self.name)
     for i in range(int(self.nargs)):
         insts = insts.const_to_stack(0).inc_sp()
     return insts
Ejemplo n.º 8
0
 def _assemble(self):
     return InstructionSeq("{} {}".format(self.cmd_name, self.label)) \
                 .dec_sp() \
                 .stack_to_register("D") \
                 .a_instruction(self.label) \
                 .c_instruction(comp="D", jump="JNE")
Ejemplo n.º 9
0
 def _assemble(self):
     return InstructionSeq("{} {}".format(self.cmd_name, self.label)) \
                 .goto(self.label)
Ejemplo n.º 10
0
 def assemble(self, comp):
     return InstructionSeq(self.cmd_name).dec_sp() \
                 .dereference("SP") \
                 .c_instruction(dest="M", comp=comp) \
                 .inc_sp()
Ejemplo n.º 11
0
 def assemble(self, comp):
     return InstructionSeq(self.cmd_name).binary_op(comp)