-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssembler.py
66 lines (61 loc) · 2.09 KB
/
Assembler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#Taylor Gray
import sys
import Parser
import Code
import SymbolTable
def main():
symbolTable = SymbolTable.SymbolTable()
symbolTable.addEntry('SP', 0)
symbolTable.addEntry('LCL', 1)
symbolTable.addEntry('ARG', 2)
symbolTable.addEntry('THIS', 3)
symbolTable.addEntry('THAT', 4)
for i in range(16):
symbolTable.addEntry('R' + str(i), i)
symbolTable.addEntry('SCREEN', 16384)
symbolTable.addEntry('KBD', 24576)
indexToStop = sys.argv[1].find('.')
hackFile = open(sys.argv[1][:indexToStop] + '.hack', 'w')
parser = Parser.Parser(sys.argv[1])
parsedCode = parser.parseMain()
#FirstPass
assemblerIndex = 0
elementsToRemove = []
for element in parsedCode:
if element[0] == '(':
if not symbolTable.contains(element):
symbolTable.addEntry(element[1:], assemblerIndex)
elementsToRemove.append(element)
else:
assemblerIndex += 1
parsedCode = [elem for elem in parsedCode if elem not in elementsToRemove]
startingAddressForVars = 16
for element in parsedCode:
if element.isnumeric():
element = int(element)
binaryInstruction = bin(element)[2:]
while (len(binaryInstruction) != 16):
binaryInstruction = '0' + binaryInstruction
hackFile.write(binaryInstruction + '\n')
elif symbolTable.contains(element):
locationInTable = symbolTable.GetAddress(element)
binaryInstruction = bin(locationInTable)[2:]
while (len(binaryInstruction) != 16):
binaryInstruction = '0' + binaryInstruction
hackFile.write(binaryInstruction + '\n')
elif len(element.split(',')) < 3:
symbolTable.addEntry(element, startingAddressForVars)
binaryInstruction = bin(startingAddressForVars)[2:]
while len(binaryInstruction) != 16:
binaryInstruction = '0' + binaryInstruction
hackFile.write(binaryInstruction + '\n')
startingAddressForVars += 1
else:
destCompJump = element.split(',')
dest = Code.dest(destCompJump[0])
comp = Code.comp(destCompJump[1])
jump = Code.jump(destCompJump[2])
binaryInstruction = '111' + comp + dest + jump
hackFile.write(binaryInstruction + '\n')
hackFile.close()
main()