def push_static_to_stack(file, index): file_name = file.replace('.vm', '').split('.vm')[0] segment_name = file_name.replace('Test', '').split('Test')[0] return '//push {} {}\n'.format(segment_name, index) + \ '@{}.{}\n'.format(file_name, index) + \ 'D=M\n' + \ push_segmentValue_to_stack()
def handling_call(functionName, nArgs): ''' push returnAddress \\ Ussing the label declarated below Save frame of the caller \\ LCL, ARG, THIS, THAT ARG= SP -5 - nArgs \\ Repositions ARG LCL=SP \\ Repositions LCL goto function Name \\ Transfers control to the called function (return Addres) \\ Declares a label for the return - address ''' call_counter = 0 label_addr = '{}_{}'.format(functionName, str(call_counter)) call_counter += 1 return '// call {} locals: {}\n'.format(functionName, nArgs) + \ '@{}\n'.format(label_addr) + \ 'D=A\n' + \ push_segmentValue_to_stack() + \ saved_frame_of_the_caller() + \ '@SP\n' + \ 'D=M\n' + \ '@5\n' + \ 'D=D-A\n' + \ '@{}\n'.format(nArgs) + \ 'D=D-A\n' + \ '@ARG\n' + \ 'M=D\n' + \ '@SP\n' + \ 'D=M\n' + \ '@LCL\n' + \ 'M=D\n' + \ handling_goto(functionName) + \ handling_label(label_addr)
def push_constant_to_stack(constant): ''' push constant to stack incrementing the stack by one ''' return '//push constant {}\n'.format(constant) + \ '@{}\n'.format(constant) + \ 'D=A\n' + \ push_segmentValue_to_stack()
def push_segmentAddr_to_stack(segment, index): ''' temp: addr = 5 + i, *SP=*addr, SP++ pointer: *SP=THIS/THAT, SP++ ''' address = baseAddress_segment(segment) addr = address.get(segment) register = str(addr + int(index)) return '//push {} {}\n'.format(segment, index) + \ '@R{}\n'.format(register) + \ 'D=M\n' + \ push_segmentValue_to_stack()
def push_segment_i(segment, index): ''' 1. compute target addr addr = segment pointer(base addr) + index 2. push segment value to stack *SP=*addr 3. incrementing stak by one SP++ ''' address = baseAddress_segment(segment) addr = address.get(segment) return '//push {} {}\n'.format(segment, index) + \ '@{}\n'.format(index) + \ 'D=A\n' + \ '@{}\n'.format(addr) + \ 'A=M+D\n' + \ 'D=M\n' + \ push_segmentValue_to_stack()