def pass1(self, infile, outfile): parser = Parser(infile) outf = open( outfile, 'w' ) code = Code() while parser.has_more_commands(): parser.advance() cmd = parser.command_type() if cmd == parser.A_COMMAND: outf.write( code.gen_a(self._get_address(parser.symbol())) + '\n' ) elif cmd == parser.C_COMMAND: outf.write( code.gen_c(parser.dest(), parser.comp(), parser.jmp()) + '\n' ) elif cmd == parser.L_COMMAND: pass outf.close()
def assemble(inf: str, outf: str) -> None: parser = Parser(inf) symbols = create_table(parser) f = open(outf, 'w') for expr in parser: out = expr.translate(symbols) if out: f.write(out + '\n')
def pass0(self, file): parser = Parser(file) cur_address = 0 while parser.has_more_commands(): parser.advance() cmd = parser.command_type() if cmd == parser.A_COMMAND or cmd == parser.C_COMMAND: cur_address += 1 elif cmd == parser.L_COMMAND: self.symbols.add_entry( parser.symbol(), cur_address )
def main(argv): # Set inputfile and ouptut file to <input filename> and <input file name or default> inputfile = '' outputfile = "output.mntdw" try: opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "ofile="]) except getopt.GetoptError: print_help_and_exit() for opt, arg in opts: if opt == '-h': print_help_and_exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg # If inputfile is still "" then throw an error. if inputfile == "": print("No input file specified.") print_help_and_exit() # Create a Parser Object parser = Parser(inputfile) # Tell the parser to assemble the file. parser.assemble() # Tell it to save the file. parser.saveFile(outputfile)
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(): 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()
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
import pdb import sys import os from assembler.parser import Parser from assembler.code import Code from assembler.symbol_table import SymbolTable if len(sys.argv) < 2: exit("You need to pass in a file name to be Assembled.") file = sys.argv[1] out_file = file.rsplit(".")[0] + ".hack" sym_table = SymbolTable() address = 0 with Parser(file) as p: # pdb.set_trace() for command in p: if command: command_type = p.command_type() if command_type == "L_COMMAND": sym_table.add_entry(p.symbol(), address) if command_type in ("A_COMMAND", "C_COMMAND"): address += 1 next_symbol = 16 debug_line_count = 1 binary = '' with Parser(file) as p: with open(out_file, 'w') as out_f: # pdb.set_trace()