-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.py
87 lines (72 loc) · 2.54 KB
/
loader.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
import pass1
import re
base_address = 0
print "Enter number of programs: "
num_prog = int(raw_input())
code = {}
symboltable = {}
length = {}
base_address = {}
print "Base address: "
base = int(raw_input())
base_address[1]=base
for i in range(num_prog):
print "Enter the file name: "
prog_name = raw_input()
temp = file(prog_name)
code_temp = temp.read()
lines_temp = code_temp.split('#')[0].lstrip().rstrip()
lines= lines_temp.split("\n")
code[i+1]=lines
symboltable_temp = code_temp.split('#')[1].lstrip().rstrip()
symboltable_lines=symboltable_temp.split('\n')
dict_temp = {}
for line in symboltable_lines:
dict_temp[line.split('$')[0].lstrip().rstrip()] = [line.split('$')[1].lstrip().rstrip(),line.split('$')[2].lstrip().rstrip()]
symboltable[i+1] = dict_temp
length[i+1] = int(code_temp.split('#')[2].lstrip().rstrip())
if i>0:
base_address[i+1] = base_address[i]+length[i]
for i in range(num_prog):
arr=[]
lines=code[i+1]
for j in range(0,len(lines)):
if "EXTRN" in lines[j]:
temp= lines[j].split(" ")[-1].lstrip().rstrip()
arr=temp.split(",")
lines[j]=";"+lines[j]
if "OFFSET" in lines[j]:
t1=int(lines[j].split("OFFSET")[1].lstrip().rstrip().split(" ")[0].lstrip().rstrip())
lines[j]=re.sub("OFFSET"+r'(\s)(\d)*',str(base_address[i+1]+t1),lines[j])
if len(arr)>0:
temp=" "
for z in arr:
for index in range(num_prog):
if z in symboltable[index+1]:
if symboltable[index+1][z][1]=="v":
temp=str(int(symboltable[index+1][z][0])+base_address[i+1])
#print str(int(symboltable[index+1][z][0])+base_address[i+1])
break
else:
temp=symboltable[index+1][z][0]
#print symboltable[index+1][z][0]
break
for index in range(len(lines)):
lines=code[i+1]
if z in lines[index]:
if ";" in lines[index]:
continue;
lines[index]=lines[index].replace(z,temp)
for i in range(num_prog):
lines=code[i+1]
for j in range(0,len(lines)):
print lines[j]
temp1=file("l1.txt",'w')
for i in range(num_prog):
lines=code[i+1]
for j in range(0,len(lines)):
if "HLT" in lines[j]:
continue;
temp1.write(lines[j]+'\n')
temp1.write("HLT"+'\n')
temp1.close()