-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassembler.py
108 lines (92 loc) · 3.64 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
def compile(source, address, var, symbols, myASCII):
ASMfile = []
file = open(source, "r")
for line in file:
ASMfile.append(line.strip())
file.close()
newProgram = []
for line in ASMfile:
if line == "" or line[0] == "#" or line[0] == ";":
pass
else:
newProgram.append(line)
print(newProgram)
labels = {}
#symbols = symbols
pc = address
vars = var
varcount = 0
binProgram = []
for line in newProgram:
if line[0] == ":":
if line not in labels.keys():
labels[line] = pc
else:
exit("ERROR Label already used : " + line)
elif line[0] == "@":
if line not in symbols.keys():
symbols[line] = pc
else:
exit("ERROR Symbol already used : " + line)
elif line[0] == ".":
_line = line.split()
if _line[1] not in symbols.keys():
symbols[_line[1]] = (vars + varcount)
varcount = varcount + int(_line[2]) # +1 if lenght must be stored
else:
exit("ERROR address already used : " + _line[1])
elif line[0] == "%":
_line = line.split()
if _line[1] in symbols.keys():
i = 0
for char in _line[2]:
if char in myASCII.keys():
newline = symbols[_line[1]] + i, myASCII[char]
else:
newline = symbols[_line[1]] + i, myASCII["#"]
binProgram.append(newline)
i = i + 1
newline = symbols[_line[1]] + i, myASCII["null"]
binProgram.append(newline)
else:
exit("ERROR address undefined : " + _line[1])
else:
pc = pc +1
print(symbols)
pc =address
for line in newProgram:
instruction = line.split()
#print(instruction)
if instruction[0][0] in ["@", ".", ":", "%"]:
pass
elif instruction[0] in [ 'out', 'in']:
newLine = (pc, (instruction[0], int(instruction[1])))
binProgram.append(newLine)
pc = pc +1
elif instruction[0] in ['lix', 'iix', 'dix', 'lda', 'ldb', 'stx', 'lxa', 'lxb', 'sto', 'sta', 'stb', 'lma', 'lmb']:
if instruction[1][0] == "$" or instruction[1][0] == "@":
newLine = (pc, (instruction[0], int(symbols[instruction[1]])))
else:
newLine = (pc, (instruction[0], int(instruction[1])))
binProgram.append(newLine)
pc = pc +1
elif instruction[0] in ['call', 'jmp', 'jmpx', 'jmpt', 'jmpf']:
if instruction[1] in labels.keys():
newLine = (pc, (instruction[0], labels[instruction[1]]))
elif instruction[1] in symbols.keys():
newLine = (pc, (instruction[0], symbols[instruction[1]]))
else:
newLine = (pc, (instruction[0], int(instruction[1])))
binProgram.append(newLine)
pc = pc +1
elif instruction[0] in ['test', 'inc', 'dec']:
newLine = (pc, (instruction[0], (instruction[1])))
binProgram.append(newLine)
pc = pc +1
elif instruction[0] in ['halt', 'ise', 'isz', 'idx','push', 'pop', 'ret', 'add', 'sub', 'mul', 'div', 'skip']:
newLine = (pc, (instruction[0], None))
binProgram.append(newLine)
pc = pc +1
else:
exit("Assembler ERROR: Unkown instruction: " + instruction[0])
return(binProgram, vars + varcount, symbols)