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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
H = ""
T = []
M = []
E = []
number = 0
count = 0
TcardNumber = 0
for i,k in enumerate(data):
Extend = 0
#------------------------------------------------------------------------------------------------------
if(i == length):
break
if(k.code == "START"):
H = "H,"+ str(k.label) +","+ str(k.loc).zfill(4) + ","+str('{0:x}'.format(int(str(ProgramEnd[0]),16) + int(str(ProgramEnd[1]),16) + int(str(ProgramEnd[1]),16)))
k.obj = "NULL"
# print('%-2s %-4s %-8s %-8s %-8s %-8s %-15s %-8s' % (i,str(k.loc).zfill(4),k.block,correctPosition(k.block,k.loc).zfill(4),k.label,k.code,k.value,k.obj))
continue
elif(k.code == "END"):
E = "E," + str(k.value)
elif(k.code == "USE" or k.code == "RESW" or k.code == "EQU" or k.code == "LTORG" or k.code == "ORG" or k.code == "BASE"):
k.obj = "NULL"
elif(k.code == "COMPR"):
behind = str(codeDict[str(k.code)][0])
k.obj = behind + str(LEETTERTABLE[str(k.value[0])]) + str(LEETTERTABLE[str(k.value[len(k.value)-1])])
elif(k.code == "CLEAR"):
behind = str(codeDict[str(k.code)][0])
k.obj = behind + str(LEETTERTABLE[str(k.value)]) + "0"
elif(k.code == "RSUB"):
k.obj = str(codeDict[str(k.code)][0]) + "0000"
elif(k.value == ""):
k.obj = "000000"
elif(k.code == "RESB"):
k.obj = "NULL"
elif(k.code == "BYTE"):
if(k.value[0] == 'C'):
k.obj = binascii.b2a_hex(str(data[i].value[3:len(data[i].value)-1]).encode('utf-8'))
else:
k.obj = k.value[2:len(k.value)-1]
elif(k.value[0] == "#" or k.value[0] == "@" or k.code[0] == "+" or k.value in SYMBOLTABLE or k.value[0:len(k.value)-2] in SYMBOLTABLE):
#如果value是在SYMBOLTABLE裡
if(k.value in SYMBOLTABLE or k.value[1::] in SYMBOLTABLE or k.value[0:len(k.value)-2] in SYMBOLTABLE):
#------------------------------------------------------------------------------------------------------
if(k.value[1::] in SYMBOLTABLE):
NOW = correctPosition(SYMBOLTABLE[k.value[1::]][1],SYMBOLTABLE[k.value[1::]][2])
Mcard = "M," + str(k.loc).zfill(6) + ",+" + str(k.value[1::])
elif(k.value[0:len(k.value)-2] in SYMBOLTABLE):
NOW = correctPosition(SYMBOLTABLE[k.value[0:len(k.value)-2]][1],SYMBOLTABLE[k.value[0:len(k.value)-2]][2])
Mcard = "M," + str(k.loc).zfill(6) + ",+" + str(k.value[0:len(k.value)-2])
elif(k.value in SYMBOLTABLE):
NOW = correctPosition(SYMBOLTABLE[k.value][1],SYMBOLTABLE[k.value][2])
if(k.code != "*"):
Mcard = "M," + str(k.loc).zfill(6) + ",+" + str(k.value)
M.append(Mcard)
#======================================================================================================
if(k.code[0] == "+"):#有加號
behind = str(codeDict[str(k.code[1::])][0]) + "00"
else:
behind = str(codeDict[str(k.code)][0])
#======================================================================================================
if(data[i].code[0] != "+"):
PC = '{0:x}'.format((int(str(correctPosition(data[i].block,data[i].loc)),16)) + int(add(data[i].code,data[i].value,codeDict[str(data[i].code)][1])))
else:#沒有+號後面是label
PC = '{0:x}'.format((int(str(correctPosition(data[i].block,data[i].loc)),16)) + int(1) + int(add(data[i].code[1::],data[i].value,codeDict[str(data[i].code[1::])][1])))
# print("behind:" + str(behind))
# print("Now:" + str(behind))
#------------------------------------------------------------------------------------------------------
if(k.code[0] == "+" and k.value[1::] in SYMBOLTABLE):#+號後面是字符直接放
k.obj = behind +str(correctPosition(SYMBOLTABLE[k.value[1::]][1],SYMBOLTABLE[k.value[1::]][2]))
elif(k.code[0] == "+" and k.value in SYMBOLTABLE):#+號後面是字符直接放
k.obj = behind +str(correctPosition(SYMBOLTABLE[k.value][1],SYMBOLTABLE[k.value][2]))
elif( (int(str(NOW),16)-int(str(PC),16) > 1000 or int(str(NOW),16)-int(str(PC),16) < -1000) and Extend == 0):
small = i
flag = 0
for n,m in enumerate(BASETABLE):
# print(closedBasePosition)
if(flag==0):
closedBasePosition = m
flag = 1
if(int(closedBasePosition) - i < small):
closedBasePosition = m
if(BASETABLE[str(closedBasePosition)][2] in SYMBOLTABLE):
BASE = correctPosition(SYMBOLTABLE[BASETABLE[str(closedBasePosition)][2]][1],SYMBOLTABLE[BASETABLE[str(closedBasePosition)][2]][2])
else:
BASE = BASETABLE[str(closedBasePosition)][2]
temp = "BASE"
# print(NOW)
# print(BASE)
k.obj = behind + SUB(int(str(NOW),16),int(str(BASE),16),0)
else:
# print(NOW)
# print(PC)
temp = "PC"
if(k.code[0] == "+"):
k.obj = behind + SUB(int(str(NOW),16),int(str(PC),16),1)
else:
k.obj = behind + SUB(int(str(NOW),16),int(str(PC),16),0)
else:#如果value是數字
if(k.code[0] == "+"):
behind = str(codeDict[str(k.code[1::])][0]) + "00"
k.obj = behind + '{0:x}'.format(int(k.value[1::]))
else:
k.obj = str(codeDict[str(k.code)][0]) + str(int(str(k.value[1::]),16)).zfill(4)
# print(k.obj)
k = symbol(k,temp)
k.obj = str(k.obj).zfill(6)
#------------------------------------------------------------------------------------------------------
elif(k.value[0] == "="):
NOW = correctPosition(SYMBOLTABLE[k.value[1:len(k.value)]][1],SYMBOLTABLE[k.value[1:len(k.value)]][2])
PC = '{0:x}'.format((int(str(correctPosition(data[i].block,data[i].loc)),16)) + int(add(data[i].code[1::],data[i].value,codeDict[str(data[i].code[1::])][1])))
behind = str(codeDict[str(k.code)][0])
k.obj = behind + str(SUB(int(str(NOW),16),int(str(PC),16))).zfill(4)
else:
behind = str(codeDict[str(k.code)][0])
if(k.value in LEETTERTABLE):
k.obj = behind + str(LEETTERTABLE[k.value]).zfill(2)
else:
k.obj = behind + "0000"
#------------------------------------------------------------------------------------------------------
print('%-2s %-4s %-8s %-8s %-8s %-8s %-15s %-8s' % (i,str.upper(str(k.loc)).zfill(4),k.block,str.upper(str(correctPosition(k.block,k.loc).zfill(4))),k.label,k.code,k.value,str.upper(str(k.obj))))
# print(ProgramBlockTable)
for i,k in enumerate(data):
if(k.obj != "NULL"):
count = count + len(k.obj)
TcardNumber = TcardNumber + 1
if(count+len(data[i+1].obj)>30 or data[i+1].obj == "NULL" or data[i+1].code == "END"):
Tposition = i-TcardNumber+1
# print("Tposition:" + str(Tposition))
Tcard = "T," + str(correctPosition(data[Tposition].block,data[Tposition].loc)).zfill(6) + "," + str('{0:x}'.format(int(count)))
for j in range(i-TcardNumber+1,i+1):
Tcard = Tcard + "," +str(data[j].obj)
# print(Tcard)
T.append(Tcard)
TcardNumber = 0
count = 0
if(k.code == "END"):
break
if os.path.exists("card.txt"):
os.remove("card.txt")
a = open("card.txt", 'w')
a.writelines(str(H))
a.writelines("\n")
for i in range(len(T)):
a.writelines(T[i])
a.writelines("\n")
a.writelines(E)
a.close()
if os.path.exists("list.txt"):
os.remove("list.txt")
file_write_obj = open("list.txt", 'w')
for i,k in enumerate(data):
#index
file_write_obj.writelines('%-4s' %i)
#location
if(k.loc == "NULL"):
file_write_obj.writelines("%-6s" %(" "))
else:
file_write_obj.writelines('%-6s' %str(k.loc).zfill(4))
#block
if(k.block == "NULL"):
file_write_obj.writelines("%-3s" %(" "))
else:
file_write_obj.writelines('%-3s' %k.block)
#correctPosition
if(k.block == "NULL"):
file_write_obj.writelines("%-8s" %(" "))
else:
file_write_obj.writelines('%-8s' %str.upper(str(correctPosition(k.block,k.loc).zfill(4))))
#label,code,value
file_write_obj.writelines('%-8s%-8s%-15s' %(k.label,k.code,str(k.value)))
#object code
if(k.obj == "NULL"):
file_write_obj.writelines("%-8s" %(" "))
else:
file_write_obj.writelines('%-8s' %(str.upper(str(k.obj))))
# print('%-2s %-4s %-8s %-8s %-8s %-8s %-15s %-8s' % (i,str(k.loc).zfill(4),k.block,correctPosition(k.block,k.loc).zfill(4),k.label,k.code,k.value,k.obj))
file_write_obj.write('\n')
if(k.code == "END"):
break
file_write_obj.close()
print("Success")
|