def secondPass(self): """ Converts commands from Hack assembly to binary instructions """ parser = Parser(self.inFile) code = Code() address = 16 try: output = open(self.outFile, 'wb') except IOError: print "Unable to open output file" exit(-2) while parser.hasMoreCommands(): parser.advance() command = parser.commandType() if command is parser.A_COMMAND: # If the command is an A instruction, write the symbol's # binary number to the output file symbol = parser.symbol() toWrite = '0' # Get decimal number to write if symbol.isdigit(): symbol = bin(int(symbol))[2:] else: if not symbol in self.symbols: # If A instruction create temporary variable self.symbols[symbol] = address address = address + 1 symbol = bin(self.symbols[symbol])[2:] # Add 0s until the length is appropriate toWrite = toWrite + symbol toWrite = toWrite.zfill(16) # Write to output file output.write(toWrite+'\n') elif command is parser.C_COMMAND: # If the command is a C instruction, write # dest, comp and jump to the output file # Write first 3 bits output.write('111') # Write 'a' bit comp = parser.comp() if comp.find('M') is not -1: output.write('1') else: output.write('0') # Write 'comp', 'dest' and 'jump' bits output.write(code.comp(comp)) output.write(code.dest(parser.dest())) output.write(code.jump(parser.jump())) output.write('\n') parser.close()
def firstPass(self): """ Adds all user-defined symbols to symbols dictionary """ parser = Parser(self.inFile) commandLine = 0 while parser.hasMoreCommands(): parser.advance() command = parser.commandType() if command is parser.L_COMMAND: newSymbol = parser.symbol() if not newSymbol in self.symbols: self.symbols[newSymbol] = commandLine elif command is parser.C_COMMAND or command is parser.A_COMMAND: # If C instruction add 1 to the # of commands commandLine = commandLine + 1 parser.close()