Ejemplo n.º 1
0
def _push_from_memory_to_stack(segment, index):
    return ('// push from segment {} to stack {} \n'.format(segment, index) +
            '@{}\n'.format(index) + 'D=A\n'
            '@{}\n'.format(segment) +
            'A=M+D\n' +  # Pointer arithmetic to index in memory
            'D=M\n' +  # Assign to D the value of current memory index
            push_D_to_stack())
def _push_from_memory_to_stack(segment, index):
    return ('// push from segment {} to stack {} \n'.format(segment, index) +
            '@{}\n'.format(index) +
            'D=A\n'
            '@{}\n'.format(segment) +
            'A=M+D\n' +  # Pointer arithmetic to index in memory
            'D=M\n' +  # Assign to D the value of current memory index
            push_D_to_stack())
def _push_constant_to_stack(val):
    """
    Stores value in stack, incrementing by 1 the value of @SP
    """
    return ('// push constant {} \n'.format(val) +
            '@{}\n'.format(val) +
            'D=A\n' +
            push_D_to_stack())
Ejemplo n.º 4
0
def call_handler(function_name, num_args):
    global label_counter
    continuation_address = 'continuation_{}_{}'.format(function_name,
                                                       label_counter)
    label_counter += 1
    return ('// call fn {} with locals {}\n'.format(function_name, num_args) +
            '@{}\n'.format(continuation_address) +
            'D=A\n' +
            push_D_to_stack() +  # Store return address (caller) in stack
            '@LCL\n' +
            'D=M\n' +
            push_D_to_stack() +  # Store LCL address (caller) in stack
            '@ARG\n' +
            'D=M\n' +
            push_D_to_stack() +  # Store ARG address (caller) in stack
            '@THIS\n' +
            'D=M\n' +
            push_D_to_stack() +  # Store THIS address (caller) in stack
            '@THAT\n' +
            'D=M\n' +
            push_D_to_stack() +  # Store THAT address (caller) in stack
            '@SP\n' +
            'D=M\n' +
            '@5\n' +
            'D=D-A\n' +
            '@{}\n'.format(num_args) +
            'D=D-A\n' +
            '@ARG\n' +  # NOQA Set current ARG address to stack head -5 (state of caller) - num_args (prev pushed to stack)
            'M=D\n' +
            '@SP\n' +
            'D=M\n' +
            '@LCL\n' +
            'M=D\n' +  # Set current LCL address to stack head
            goto_handler(function_name) +
            label_handler(continuation_address)
            )
Ejemplo n.º 5
0
def _push_from_static_to_stack(index, file):
    return ('// push from static {} to stack \n'.format(index) +
            '@{}_{}\n'.format(file, index) + 'D=M\n' + push_D_to_stack())
Ejemplo n.º 6
0
def _push_from_register_to_stack(register):
    return ('@{}\n'.format(register) + 'D=M\n' + push_D_to_stack())
Ejemplo n.º 7
0
def _push_constant_to_stack(val):
    """
    Stores value in stack, incrementing by 1 the value of @SP
    """
    return ('// push constant {} \n'.format(val) + '@{}\n'.format(val) +
            'D=A\n' + push_D_to_stack())
def _push_from_static_to_stack(index, file):
    return ('// push from static {} to stack \n'.format(index) +
            '@{}_{}\n'.format(file, index) +
            'D=M\n' +
            push_D_to_stack())
def _push_from_register_to_stack(register):
    return ('@{}\n'.format(register) +
            'D=M\n' +
            push_D_to_stack())