예제 #1
0
def _prepare_for_binary():
    """
    Loads the first 2 entries of stack to D and M
    """
    return (load_stack_head_to_D() +
            '@SP\n' +
            'A=M-1\n')
예제 #2
0
def return_handler():
    frame_address_pointer = 'R13'  # NOQA temp address used to store the base frame address
    continue_address_pointer = 'R14'  # NOQA temp address used to store the address of
    return ('// return\n'
            '@LCL\n'
            'D=M\n'
            '@{}\n'.format(frame_address_pointer) +
            'M=D\n'  # NOQA Store in frame_address_pointer (temp) the value of LCL (head of stack at beginning of call execution)
            '@5\n'
            'D=D-A\n'  # D now holds the address of return pointer
            'A=D\n'
            'D=M\n'
            '@{}\n'.format(continue_address_pointer) +
            'M=D\n' +  # NOQA Store continuation_address in continue_address_pointer (return address was previously set in call_handler)
            load_stack_head_to_D() +
            '@ARG\n'
            'A=M\n'
            'M=D\n'  # Set ARG [0] to return value of function
            'D=A+1\n'  # Next stack head should be the value of ARG[0] + 1
            '@SP\n'
            'M=D\n'  # NOQA Set stack head to point to one address above where we store the returned value
            '@{}\n'.format(frame_address_pointer) +
            'A=M-1\n'
            'D=M\n'
            '@THAT\n'
            'M=D\n'  # Set the caller's THAT to its previous value
            '@2\n'
            'D=A\n'
            '@{}\n'.format(frame_address_pointer) +
            'D=M-D\n'
            'A=D\n'
            'D=M\n'
            '@THIS\n'
            'M=D\n'  # Set the caller's THIS to its previous value
            '@3\n'
            'D=A\n'
            '@{}\n'.format(frame_address_pointer) +
            'D=M-D\n'
            'A=D\n'
            'D=M\n'
            '@ARG\n'
            'M=D\n'  # Set the caller's ARG to its previous value
            '@4\n'
            'D=A\n'
            '@{}\n'.format(frame_address_pointer) +
            'D=M-D\n'
            'A=D\n'
            'D=M\n'
            '@LCL\n'
            'M=D\n'  # Set the caller's LCL to its previous value
            '@{}\n'.format(continue_address_pointer) +
            'A=M\n'  # Load the return address to continue executing caller
            '0;JMP\n'
            )
def _from_stack_to_memory_transporter(segment, index):
    return ('// pop from stack to segment {} {} \n'.format(segment, index) +
            '@{}\n'.format(index) +
            'D=A\n' +
            '@{}\n'.format(segment) +
            'D=D+M\n' +
            '@R13\n' +
            'M=D\n' +  # Store memory index where we want to store in R13
            load_stack_head_to_D() +
            '@R13\n' +
            'A=M\n' +
            'M=D\n')
예제 #4
0
def _from_stack_to_static_transporter(index, file):
    return ('// pop from statck to static {}\n'.format(index) +
            load_stack_head_to_D() + '@{}_{}\n'.format(file, index) + 'M=D')
예제 #5
0
def _from_stack_to_register_transporter(register):
    return ('// pop from stack to register {}\n'.format(register) +
            load_stack_head_to_D() + '@{}\n'.format(register) + 'M=D')
예제 #6
0
def _from_stack_to_memory_transporter(segment, index):
    return ('// pop from stack to segment {} {} \n'.format(segment, index) +
            '@{}\n'.format(index) + 'D=A\n' + '@{}\n'.format(segment) +
            'D=D+M\n' + '@R13\n' +
            'M=D\n' +  # Store memory index where we want to store in R13
            load_stack_head_to_D() + '@R13\n' + 'A=M\n' + 'M=D\n')
def _from_stack_to_static_transporter(index, file):
    return ('// pop from statck to static {}\n'.format(index) +
            load_stack_head_to_D() +
            '@{}_{}\n'.format(file, index) +
            'M=D')
def _from_stack_to_register_transporter(register):
    return ('// pop from stack to register {}\n'.format(register) +
            load_stack_head_to_D() +
            '@{}\n'.format(register) +
            'M=D')
def if_goto_handler(label):
    return ('// goto handler {}\n'.format(label) +
            load_stack_head_to_D() +
            '@{}\n'.format(label) +
            'D;JNE\n')
예제 #10
0
def if_goto_handler(label):
    return ('// goto handler {}\n'.format(label) + load_stack_head_to_D() +
            '@{}\n'.format(label) + 'D;JNE\n')