Exemple #1
0
class Assembler:
    """Handles file I/O"""
    def __init__(self, files):
        self.files = files
        self.symtable = SymbolTable()

    def hasFiles(self):
        """Are there more files to process?"""
        return len(self.files) > 0

    def dummyPass(self):
        """Do a soft pass to determine the location for labels (Xxx)"""
        filename = self.files[0]
        p = Parser(filename)
        current_address = 0
        while p.hasMoreCommands():
            p.advance()
            cmd_type = p.commandType()
            if cmd_type is A_COMMAND or cmd_type is C_COMMAND:
                current_address += 1
            elif cmd_type is L_COMMAND:
                self.symtable.addEntry(p.symbol(), current_address)

    def processFile(self):
        """Translates the next file in the queue"""
        filename = self.files.pop(0)
        p = Parser(filename)
        if filename.endswith('.asm'):
            fileout = filename.replace('.asm', '.hack')
        else:
            fileout = filename + '.hack'
        f = open(fileout, 'w')
        print("Translating %s" % (filename))
        self.current_address = 16
        while p.hasMoreCommands():
            p.advance()
            if p.commandType() is A_COMMAND:
                address = self._getAddress(p.symbol())
                instruction = '{0:016b}'.format(int(address))
            elif p.commandType() is C_COMMAND:  # dest=comp;jump
                instruction = ''.join([
                    '111',
                    Code.comp(p.comp()),
                    Code.dest(p.dest()),
                    Code.jump(p.jump())
                ])
            else:  # L_COMMAND (Xxx)
                continue
            #print("Symbol: %s. Instruction: %s" % (p.symbol(), instruction))
            print(instruction, end='\n', file=f)
        f.close()

    def _getAddress(self, symbol):
        if symbol.isdigit():
            return symbol
        else:
            if not self.symtable.contains(symbol):
                self.symtable.addEntry(symbol, self.current_address)
                self.current_address += 1
            return self.symtable.GetAddress(symbol)
Exemple #2
0
line = rfile.readline()
flag = Parser.hasMoreCommands(line)

while flag:
    while line == '\n' or line.startswith('//'):
        line = rfile.readline()

    ctype = Parser.commandType(line)

    if ctype is 'A_COMMAND':
        AS = Parser.symbol(line)
        if not AS.isdigit():
            if not SymbolTable.contains(AS, symboldict):
                symboldict = SymbolTable.addEntry(AS, j + 16, symboldict)
                j += 1
            binAS = bin(SymbolTable.GetAddress(AS, symboldict))[2:]

        else:
            binAS = bin(int(AS))[2:]

        AString = binAS.zfill(15)
        wfile.write('0' + AString + '\n')

    elif ctype is 'C_COMMAND':
        DestString = Code.dest(line)
        CompString = Code.comp(line)
        JumpString = Code.jump(line)
        wfile.write('111' + CompString + DestString + JumpString + '\n')

    line = Parser.advance(rfile, line)
    flag = Parser.hasMoreCommands(line)
Exemple #3
0
            instr_str.append(x.strip('\r'))

address = 16
sym = SymbolTable.Constructor()

for x in instr_str:
    if x.find('@') >= 0 or x.find('(') >= 0:
        symbol = Parser.symbol(x)
        if not SymbolTable.contains(symbol, sym) and not symbol.isdigit():
            sym = SymbolTable.addEntry(symbol, address, sym)
            address = address + 1

while Parser.hasMoreCommands(i, instr_str):
    c_type = Parser.commandType(instr_str[i])
    if c_type == 'A':
        str1 = Parser.symbol(instr_str[i])
        if str1.isdigit():
            str1 = bin(int(str1))[2:]
            address = str1.zfill(16)
            print(address)
        else:
            if SymbolTable.contains(str1, sym):
                str2 = bin(SymbolTable.GetAddress(str1, sym))[2:]
                address1 = str2.zfill(16)
                print(address1)
    if c_type == 'C':
        c_str = Parser.comp(instr_str[i])
        d_str = Parser.dest(instr_str[i])
        j_str = Parser.jump(instr_str[i])
        print('111' + c_str + d_str + j_str)
    i = i + 1