def main(): try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) filename = args[0][:args[0].find(".")] cCommandBase = 0xe000 # 1110 0000 0000 0000 table = SymbolTable() p = Parser(args[0]) instructionAdress = 0 ramIndex = 16 while p.hasMoreCommands(): if p.commandType() == "L_COMMAND": table.addEntry(p.symbol(), instructionAdress) else: instructionAdress = instructionAdress + 1 p.close() print(table.getTable()) with open(filename + ".hack", "w") as outputfile: # outputfile.write("//Hello world!!") p = Parser(args[0]) code = Code() while p.hasMoreCommands(): # print(p.commandType(), ":", p.advance) if p.commandType() == "C_COMMAND": print('%s %s %s' % (p.dest(), p.comp(), p.jump())) instruction = cCommandBase | code.dest( p.dest()) | code.comp(p.comp()) | code.jump(p.jump()) outputfile.write('{0:b}'.format(instruction) + "\n") elif p.commandType() == "A_COMMAND": if p.symbol().isdigit(): outputfile.write('{0:b}'.format( 0x8000 | int(p.symbol())).replace("1", "0", 1) + "\n") elif table.contains(p.symbol()): outputfile.write( '{0:b}'.format(0x8000 | table.getAddress( p.symbol())).replace("1", "0", 1) + "\n") else: table.addEntry(p.symbol(), ramIndex) # print(table.getTable()) outputfile.write('{0:b}'.format( 0x8000 | ramIndex).replace("1", "0", 1) + "\n") ramIndex = ramIndex + 1 p.close() except Exception as e: print(e) raise e
def main(): try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) filename = args[0][:args[0].find(".")] cCommandBase = 0xe000 # 1110 0000 0000 0000 with open(filename + ".hack", "w") as outputfile: # outputfile.write("//Hello world!!") p = Parser(args[0]) code = Code() while p.hasMoreCommands(): # print(p.commandType(), ":", p.advance) if p.commandType() == "C_COMMAND": print('%s %s %s' % (p.dest(), p.comp(), p.jump())) instruction = cCommandBase | code.dest( p.dest()) | code.comp(p.comp()) | code.jump(p.jump()) outputfile.write('{0:b}'.format(instruction) + "\n") elif p.commandType() == "A_COMMAND": print(p.symbol()) outputfile.write('{0:b}'.format( 0x8000 | int(p.symbol())).replace("1", "0", 1) + "\n") # outputfile.write('%x' % int(p.symbol()) + "\n") except Exception as e: print(e) raise e
def main(): args, unknown = parse_args() path = os.path.abspath(args.asmfile) # Initialize parser code = AsmCode() table = SymbolTable() print("=== LOOP 1 : Create symbol table phase ===") index = 0 parser = Parser(path) while parser.has_more_commands(): parser.advance() if parser.command_type() is Command.L: table.add_entry(parser.symbol(), index) index -= 1 index += 1 pprint.pprint(table.table()) print("=== LOOP 2 output assembler phase ===") in_name = os.path.basename(path) # *.asm f = open("{}/{}".format(os.path.dirname(path), os.path.splitext(in_name)[0] + ".hack"), "w") # Re-initialize parser parser = Parser(path) l_index = 16 while parser.has_more_commands(): parser.advance() # For debugging # print("{:>3}: {:<20}{:<20}{:<20}{:<20}{:<20}{:<20}".format( # parser.index, # parser.command, # parser.command_type(), # str(parser.symbol()), # str(parser.dest()) + ": " + code.dest(parser.dest()), # str(parser.comp()) + ": " + code.comp(parser.comp()), # str(parser.jump()) + ": " + code.jump(parser.jump()) # )) if parser.command_type() is Command.A: if is_pos_int(parser.symbol()): f.write("0" + str(bin(int(parser.symbol())))[2:].zfill(15) + '\n') else: if table.contains(parser.symbol()): f.write(table.get_address(parser.symbol()) + '\n') else: table.add_entry(parser.symbol(), l_index) f.write(table.get_address(parser.symbol()) + '\n') l_index += 1 elif parser.command_type() is Command.C: f.write("111" + code.comp(parser.comp()) + code.dest(parser.dest()) + code.jump(parser.jump()) + '\n') pprint.pprint(table.table()) f.close()