-
Notifications
You must be signed in to change notification settings - Fork 0
/
linker.py
72 lines (63 loc) · 2.03 KB
/
linker.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
import assembler
error = "False"
startaddfile = {}
filelentab = assembler.filelentab
globtab = assembler.globtab
def findfile(string, files):
for file in files:
if string in globtab[file.split('.')[0]] and "$" in globtab[file.split('.')[0]][string]:
return file.split('.')[0], globtab[file.split('.')[0]][string].split("$")[1]
return "Not found", "-1"
def link(fileNames):
global error
global startaddfile
global filelentab
global globtab
startaddfile = {}
filelentab = assembler.filelentab
globtab = assembler.globtab
error = "False"
memadd = 0
for filename in fileNames:
startaddfile[filename.split('.')[0]] = memadd
memadd = memadd + filelentab[filename.split('.')[0]]
lincode = []
for filenam in fileNames:
filename = filenam.split('.')[0]
with open(filename + '.pass2', 'r') as file:
lines = file.read().split('\n')
file.close()
print("Linker start -------------------------------------------------------------")
for line in lines:
# No variables to link
if '$' not in line and '@' not in line:
lincode.append(line)
# Unlinked local variables
if '@' in line:
addr = line.split('@')[1]
addrtmp = addr.split(',')[0]
# print(addr1[0])
addr2 = str(int(addrtmp) + startaddfile[filename])
line = line.replace('@' + addrtmp, '@' + addr2)
lincode.append(line)
# Unlinked Global Variables
if '$' in line:
var = line.split('$')[1]
vara = var.split(',')[0]
# print(var, vara)
# fname, add = findfile(line.split('$')[1], fileNames)
fname, add = findfile(var, fileNames)
if fname == "Not found":
error = "External variable " + line.split('$')[1] + " not found: " + line
print(error)
return
line = line.replace('$' + vara, "@" + str(int(add) + startaddfile[fname]))
lincode.append(line)
# print(line)
with open(fileNames[0].split('.')[0] + '.linked', 'w') as file:
file.write("\n".join(lincode))
file.close()
if __name__ == "__main__":
FilesTesting = ['test_ext_a.txt','test_ext_b.txt']
assembler.pass1(FilesTesting)
link(FilesTesting)