예제 #1
0
def _push(sgm, index):
    if sgm == 'constant':
        asm.push_constant(index)
        return
    else:
        asm.push(sgm, index)
        return
예제 #2
0
def _unary(compute):
    # compute Example: 'M=-D', 'M=!D''
    cs.dec_sp()
    cs.st_2_dreg()
    asm.append_lines(compute)
    cs.inc_sp()
    return
예제 #3
0
def _binary(compute):
    # compute Example: 'M=M+D', 'M=M-D', 'M=M&D', 'M=M|D'
    cs.dec_sp()
    cs.st_2_dreg()
    cs.dec_sp()
    cs.st_2_areg()
    asm.append_lines(compute)
    cs.inc_sp()
    return
예제 #4
0
def _push_static(index, fileName):
    variable= fileName + '.' + index
    asm.set_areg(variable)  # @index
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg_from_reg('SP')  # @SP, A=M
    asm.set_sgm_from_dreg()  # M=D
    inc_sp()  # Mem[SP] = Mem[SP] + 1
예제 #5
0
def _pop_static(sgm, index, fileName):
    variable = fileName + '.' + index
    dec_sp()  # Mem[SP] = Mem[SP] - 1
    asm.set_areg_from_sgm()  # A=M
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg(variable)    # @XXX.3
    asm.set_sgm_from_dreg()  # M=D
예제 #6
0
def _set_bool(bool):
    asm.set_areg('SP')
    asm.set_areg_from_sgm()

    if bool == 'TRUE':
        asm.append_lines('M=-1')
    elif bool == 'FALSE':
        asm.append_lines('M=0')
예제 #7
0
def _comp(jump):  # TODO use Label!
    # jmp Example: 'JEQ', 'JGT', 'JLT'
    cs.dec_sp()
    cs.st_2_dreg()
    cs.dec_sp()
    cs.st_2_areg()
    asm.append_lines('D=M-D')
    #asm.set_areg(str(len(asm.asmLines) + 7))  # 1Line  # 7 = 1 + 3 + 1 + 1 + 1
    asm.set_areg(str(_count_unlabeld_line() + 7))
    asm.append_lines('D;' + jump)  # 1Line
    _set_bool('FALSE')  # 3Lines
    #asm.set_areg(str(len(asm.asmLines) + 5))  # 1Line # 5 = 1 + 3 + 1
    asm.set_areg(str(_count_unlabeld_line() + 5))
    asm.append_lines('0;JMP')  # 1Line
    _set_bool('TRUE')  # 3Lines
    cs.inc_sp()
    return
예제 #8
0
def _pop(sgm, index):
    asm.pop(sgm, index)
    return
예제 #9
0
def _save_register(reg):
    asm.set_areg(reg)  # @reg
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg_from_reg('SP')  # @SP, A=M
    asm.set_sgm_from_dreg()  # M=D
    cs.inc_sp()  # Mem[SP] = Mem[SP] + 1
예제 #10
0
def _set_if_goto(label):
    cs.dec_sp()  # SP--
    cs.st_2_dreg()  # A=M, D=A
    asm.set_areg(label)  # A=Label
    asm.append_lines('D;JNE')  # If D != 0; then goto label.
예제 #11
0
def _set_label(label, funcName):
    if funcName:
        asm.append_lines('(' + funcName + '$' + label + ')')
    else:
        asm.append_lines('(' + label + ')')
예제 #12
0
def _set_goto(label, funcName):
    if funcName:
        asm.set_areg(funcName + '$' + label)
    else:
        asm.set_areg(label)
    asm.append_lines('0;JMP')
예제 #13
0
def set_stack(value):
    asm.set_dreg(value)
    asm.set_areg('SP')
    asm.set_areg_from_sgm()  # A=M
    asm.set_sgm_from_dreg()  # M=D
    inc_sp()
예제 #14
0
 def _boot_strap(self):
     asm.set_vreg(value='256', reg='SP')
     asm._init_vsgm()
     sc.write_call('Sys.init', '0')
예제 #15
0
def _set_return():
    asm.set_areg('FRAME')  # @FRAME
    asm.set_areg_from_sgm()  # A=M
    asm.dec_areg(5)  # A=A-5
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg('RETURN')  # @RETURN
    asm.set_sgm_from_dreg()  # M=D
예제 #16
0
def _unary(compute):
    # compute Example: 'M=-D', 'M=!D''
    asm.dec_sp()
    asm.st_2_dreg()
    asm.append_lines(compute)
    asm.inc_sp()
예제 #17
0
def _return():
    # FLAME=*LCL
    asm.set_areg('LCL')  # @LCL
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg('FRAME')  # @FRAME
    asm.set_sgm_from_dreg()  # M=D

    # RET=*(FRAME-5)
    _set_return()

    # *ARG=pop()
    cs.dec_sp()  # Mem[SP] = Mem[SP] - 1
    asm.set_areg_from_sgm()  # A=M
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg('ARG')  # @ARG
    asm.set_areg_from_sgm()  # A=M
    asm.set_sgm_from_dreg()  # M=D

    # SP=ARG+1
    asm.set_areg('ARG')  # @ARG
    asm.set_dreg_from_sgm()  # D=M
    asm.append_lines('D=D+1')  # D=D+1
    asm.set_areg('SP')  # @SP
    asm.set_sgm_from_dreg()  # M=D

    _set_return_segment('THAT', 1)  # THAT=*(FRAME-1)
    _set_return_segment('THIS', 2)  # THIS=*(FRAME-2)
    _set_return_segment('ARG', 3)  # ARG=*(FRAME-3)
    _set_return_segment('LCL', 4)  # LCL=*(FRAME-4)

    # GOTO RET.
    asm.set_areg('RETURN')  # @RETURN
    asm.set_areg_from_sgm()  # A=M
    asm.append_lines('0;JMP')
예제 #18
0
def st_2_areg():
    asm.append_lines('@SP', 'A=M')
예제 #19
0
def st_2_dreg():
    asm.append_lines('@SP', 'A=M', 'D=M')
예제 #20
0
def inc_sp():
    asm.append_lines('@SP', 'M=M+1')
예제 #21
0
def dec_sp():
    asm.append_lines('@SP', 'M=M-1')
예제 #22
0
def _pop_2_mem(sgm, index):
    dec_sp()  # Mem[SP] = Mem[SP] - 1
    asm.set_areg_from_sgm()  # A=M
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg('%s' % asm.sgm_2_reg(sgm))  # @reg
    asm.inc_areg(index)  # Loop: A=A+1
    asm.set_sgm_from_dreg()  # M=D
예제 #23
0
def _set_return_segment(seg, index):  # seg=*(FRAME-index)
    asm.set_areg('FRAME')  # @FRAME
    asm.set_areg_from_sgm()  # A=M
    asm.dec_areg(index)  # A=A-index
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg(seg)  # @seg
    asm.set_sgm_from_dreg()  # M=D
예제 #24
0
def _call(fName, nArgs):
    # Push RETURN-ADDRESS.
    # cs.set_stack(fName + '$' + 'retAddr')
    _push_return_addr(fName)

    _save_register('LCL')  # Push LCL.
    _save_register('ARG')  # Push ARG.
    _save_register('THIS')  # Push THIS.
    _save_register('THAT')  # Push  TAHT.

    # ARG=SP-n-5
    asm.set_areg('SP')  # @SP
    asm.set_dreg_from_sgm()  # D=M
    index = int(nArgs) + 5
    asm.dec_dreg(str(index))
    asm.set_areg('ARG')  # @
    asm.set_sgm_from_dreg()  # M=D

    # LCL=SP
    asm.set_areg('SP')  # @SP
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg('LCL')  # @LCL
    asm.set_sgm_from_dreg()  # M=D

    pf.write_goto(fName, None)  # Goto Function.

    # SET Label
    # pf.write_label('retAddr', fName)
    _wrie_return_addr_label(fName)
예제 #25
0
def _push(sgm, index):
    asm.set_areg_from_reg('%s' % asm.sgm_2_reg(sgm))  # @REGISTER, A=M
    asm.inc_areg(index)  # Loop: A=A+1
    asm.set_dreg_from_sgm()  # D=M
    asm.set_areg_from_reg('SP')  # @SP, A=M
    asm.set_sgm_from_dreg()  # M=D
    inc_sp()  # Mem[SP] = Mem[SP] + 1
예제 #26
0
def _comp(jump):
    # jmp Example: 'JEQ', 'JGT', 'JLT'
    asm.dec_sp()
    asm.st_2_dreg()
    asm.dec_sp()
    asm.st_2_areg()
    asm.append_lines('D=M-D')
    asm.set_areg(str(len(asm.asmLines) + 7))  # 1Line  # 7 = 1 + 3 + 1 + 1 + 1
    asm.append_lines('D;' + jump)  # 1Line
    asm.set_bool('FALSE')  # 3Lines
    asm.set_areg(str(len(asm.asmLines) + 5))  # 1Line # 5 = 1 + 3 + 1
    asm.append_lines('0;JMP')  # 1Line
    asm.set_bool('TRUE')  # 3Lines
    asm.inc_sp()