+/TECS

git clone https://git.julia.blue/+/TECS

Joseph RyanComplete tokenizer8cdba98

master
5.3 KiB150 linesraw
1# ASM to HACK assembler by Joseph Ryan
2import sys
3
4if len(sys.argv) > 1:
5    # Opens file
6    fileName = sys.argv[-1]
7    if not fileName.endswith(".asm"):
8        fileName += ".asm"
9    f = open(fileName)
10
11    if '-m' not in sys.argv:
12        print "Input file opened, parsing..."
13
14    # Initializes symbol table
15    symbols = {"SP": 0, "LCL": 1, "ARG": 2, "THIS": 3, "THAT": 4, "SCREEN": 16384, "KBD": 24576, "R0": 0, "R1": 1, "R2": 2, "R3": 3, "R4": 4, "R5": 5, "R6": 6, "R7": 7, "R8": 8, "R9": 9, "R10": 10, "R11": 11, "R12": 12, "R13": 13, "R14": 14, "R15": 15}
16
17    # Reads file into list
18    program = []
19    for line in f.readlines():
20        if not (line == "\n" or line[0] == '/'):
21            if line.find('/') > -1:
22                line = line[0:line.find('/')]
23            line = line.strip()
24            program.append(line)
25    f.close()
26
27    if '-m' not in sys.argv:
28        print "Reading symbols..."
29
30    # Adds jump symbols to table
31    i = 0
32    j = 16
33    for command in program:
34        if command.startswith('('):
35            symbols[command[1:-1]] = i
36            i -= 1
37        i += 1
38
39    # Adds A instruction symbols to table, and replaces symbols with numeric equivilants
40    for command in program:
41        if command.startswith('@'):
42            if not command[1:].isdigit():
43                if symbols.has_key(command[1:]):
44                    command = '@' + str(symbols.get(command[1:]))
45                else:
46                    symbols[command[1:]] = j
47                    j += 1
48
49    if '-m' not in sys.argv:
50        print "Assembling code..."
51
52    # Translates instructions into hack op codes
53    hack = []
54    for command in program:
55        # A instructions
56        if command.startswith('@'):
57            load = command.split('@')[1]
58            if not load.isdigit():
59                load = str(symbols[load])
60            hack.append(bin(int(load))[2:])
61            hack[len(hack) - 1] = hack[len(hack) - 1].zfill(16)
62    # C instructions
63        elif not command.startswith('('):
64            hack.append("111")
65            if command.find('=') > -1:
66                comp = command.split('=')[1]
67            else:
68                comp = command
69            if command.find(';') > -1:
70                comp = comp.split(';')[0]
71    # Computation
72            hack[len(hack) - 1] += str(int(comp.find('M') > -1))
73            if comp == '0':
74                hack[len(hack) - 1] += "101010"
75            if comp == '1':
76                hack[len(hack) - 1] += "111111"
77            if comp == "-1":
78                hack[len(hack) - 1] += "111010"
79            if comp == 'D':
80                hack[len(hack) - 1] += "001100"
81            if comp == 'A' or comp == 'M':
82                hack[len(hack) - 1] += "110000"
83            if comp == "!D":
84                hack[len(hack) - 1] += "001101"
85            if comp == "!A" or comp == "!M":
86                hack[len(hack) - 1] += "110001"
87            if comp == "-D":
88                hack[len(hack) - 1] += "001101"
89            if comp == "-A" or comp == "-M":
90                hack[len(hack) - 1] += "110001"
91            if comp == "D+1":
92                hack[len(hack) - 1] += "011111"
93            if comp == "A+1" or comp == "M+1":
94                hack[len(hack) - 1] += "110111"
95            if comp == "D-1":
96                hack[len(hack) - 1] += "001110"
97            if comp == "A-1" or comp == "M-1":
98                hack[len(hack) - 1] += "110010"
99            if comp == "D+A" or comp == "D+M":
100                hack[len(hack) - 1] += "000010"
101            if comp == "D-A" or comp == "D-M":
102                hack[len(hack) - 1] += "010011"
103            if comp == "A-D" or comp == "M-D":
104                hack[len(hack) - 1] += "000111"
105            if comp == "D&A" or comp == "D&M":
106                hack[len(hack) - 1] += "000000"
107            if comp == "D|A" or comp == "D|M":
108                hack[len(hack) - 1] += "010101"
109    # Destination
110            if command.find('=') > -1:
111                dest = command.split('=')[0]
112                hack[len(hack) - 1] += str(int(dest.find('A') > -1))
113                hack[len(hack) - 1] += str(int(dest.find('D') > -1))
114                hack[len(hack) - 1] += str(int(dest.find('M') > -1))
115            else:
116                hack[len(hack) - 1] += "000"
117    # Jump
118            if command.find(';') > -1:
119                jmp = command.split(';')[1][1:3]
120                if jmp == "GT":
121                    hack[len(hack) - 1] += "001"
122                if jmp == "EQ":
123                    hack[len(hack) - 1] += "010"
124                if jmp == "GE":
125                    hack[len(hack) - 1] += "011"
126                if jmp == "LT":
127                    hack[len(hack) - 1] += "100"
128                if jmp == "NE":
129                    hack[len(hack) - 1] += "101"
130                if jmp == "LE":
131                    hack[len(hack) - 1] += "110"
132                if jmp == "MP":
133                    hack[len(hack) - 1] += "111"
134            else:
135                hack[len(hack) - 1] += "000"
136
137    if '-m' not in sys.argv:
138        print "Writing output to file..."
139
140    # Write resulting binary
141    f = open(fileName[:-3] + "hack", "w")
142    for line in hack:
143        if '-p' in sys.argv:
144            print line
145        f.write(line + "\n")
146    if '-m' not in sys.argv:
147        print "Finished!"
148
149else:
150    print "usage: assembler.py sourceFile[.asm]\noptions:\n\t-m mutes status messages\n\t-p prints output to stdout"