This repository was archived by the owner on Jun 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJFFsicAssemblr.cpp
386 lines (364 loc) · 8.59 KB
/
JFFsicAssemblr.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中相關的字串程式
#include <iomanip>
#include <map>
#include <string>
#include <fstream>
#include <stdio.h>
// std 為標準程式庫的名稱空間
using namespace std;
map<string, string> optable;
map<string, int> address;
// del white space
std::string& trim(std::string &s) {
if (s.empty()) {
return s;
}
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
// have ,X?
int check_x(string str) {
if (str.find(",X") != -1) {
return 1;
}
else {
return 0;
}
}
// have +?
int check_plus(string str) {
if (str.find("+") != -1) {
return 1;
}
else {
return 0;
}
}
// have @?
int check_at(string str) {
if (str.find("@") != -1) {
return 1;
}
else {
return 0;
}
}
// have #?
int check_sharp(string str) {
if (str.find("#") != -1) {
return 1;
}
else {
return 0;
}
}
// 確定是否為Base-relative
int in_base(int num, int b) {
int range = num - b;
if(0 <= range && range <= 4095) {
return 1;
}
else {
return 0;
}
}
// 確定是否為PC-relative
int in_pc(int num, int pc) {
int range = num - pc;
if(-2048 <= range && range <= 2047)
return 1;
else
return 0;
}
int dec2bin(int num) {
int a, b=1, c=0;
while(num != 0){
a= num % 2;
num = num / 2;
c = c + a * b;
b = b * 10;
}
return c;
}
int hex2dec(string num) {
unsigned int x;
std::stringstream ss;
ss << std::uppercase << std::hex << num;
ss >> x;
return static_cast<int>(x);
}
string hex2bin(char hex_num) {
if (hex_num == '0')
return "0000";
else if (hex_num == '1')
return "0001";
else if (hex_num == '2')
return "0010";
else if (hex_num == '3')
return "0011";
else if (hex_num == '4')
return "0100";
else if (hex_num == '5')
return "0101";
else if (hex_num == '6')
return "0110";
else if (hex_num == '7')
return "0111";
else if (hex_num == '8')
return "1000";
else if (hex_num == '9')
return "1001";
else if (hex_num == 'A')
return "1010";
else if (hex_num == 'B')
return "1011";
else if (hex_num == 'C')
return "1100";
else if (hex_num == 'D')
return "1101";
else if (hex_num == 'E')
return "1110";
else if (hex_num == 'F')
return "1111";
}
// flag bits
struct nixbpe {
int n, i, x, b, p, e;
};
int main(void) {
optable["ADD"] = "18";
optable["ADDF"] = "58";
optable["ADDR"] = "90";
optable["AND"] = "40";
optable["CLEAR"] = "B4";
optable["COMP"] = "28";
optable["COMPF"] = "88";
optable["COMPR"] = "A0";
optable["DIV"] = "24";
optable["DIVF"] = "64";
optable["DIVR"] = "9C";
optable["FIX"] = "C4";
optable["FLOAT"] = "C0";
optable["HIO"] = "F4";
optable["J"] = "3C";
optable["JEQ"] = "30";
optable["JGT"] = "34";
optable["JLT"] = "38";
optable["JSUB"] = "48";
optable["LDA"] = "00";
optable["LDB"] = "68";
optable["LDCH"] = "50";
optable["LDF"] = "70";
optable["LDL"] = "08";
optable["LDS"] = "6C";
optable["LDT"] = "74";
optable["LDX"] = "04";
optable["LPS"] = "D0";
optable["MUL"] = "20";
optable["MULF"] = "60";
optable["MULR"] = "98";
optable["NORM"] = "C8";
optable["OR"] = "44";
optable["RD"] = "D8";
optable["RMO"] = "AC";
optable["RSUB"] = "4C";
optable["SHIFTL"] = "A4";
optable["SHIFTR"] = "A8";
optable["SIO"] = "F0";
optable["SSK"] = "EC";
optable["STA"] = "0C";
optable["STB"] = "78";
optable["STCH"] = "54";
optable["STF"] = "80";
optable["STI"] = "D4";
optable["STL"] = "14";
optable["STS"] = "7C";
optable["STSW"] = "E8";
optable["STT"] = "84";
optable["STX"] = "10";
optable["SUB"] = "1C";
optable["SUBF"] = "5C";
optable["SUBR"] = "94";
optable["SVC"] = "B0";
optable["TD"] = "E0";
optable["TIO"] = "F8";
optable["TIX"] = "2C";
optable["TIXR"] = "B8";
optable["WD"] = "DC";
/*
ln: input data total line
src_code: input data
base: BASE num
flag: nixbpe flag
loc: 經計算後的local
object_code: 經計算後的object_code
*/
char line[128];
int ln = 0, loc[10000], base=0;
struct nixbpe flag[200];
string src_code[10000][3];
string object_code[10000];
// init filesystem
fstream file;
file.open("input.txt", ios::in);
if (!file)
cout << "Open source file failed!" << endl;
while(file.getline(line, sizeof(line), '\n')) {
// get source statement
stringstream s(line);
string sub_str;
int src_num = 0;
// flag e
if(check_plus(line)) {
flag[ln].e = 1;
}
// flage n i
if(check_sharp(line)) {
flag[ln].n = 0;
flag[ln].i = 1;
}
if(check_at(line)) {
flag[ln].n = 1;
flag[ln].i = 0;
}
// flag x
if(check_x(line)) {
flag[ln].x = 1;
}
while(getline(s,sub_str,'|')) {
src_code[ln][src_num++] = trim(sub_str);
}
ln++;
}
ln--;
// start local space
loc[0] = loc[1] = hex2dec(src_code[0][2]);
for (int i= 1; i <= ln; i++) {
int format = 3;
string label = src_code[i][0];
string code1 = src_code[i][1];
string code1_op = optable[code1];
string code2 = src_code[i][2];
string opcode = hex2bin(code1_op[0]) + hex2bin(code1_op[1]).substr(0,1);
// if flag e is 1 => format 4
if(flag[i].e == 1) {
format = 4;
}
// 處理RESB的位址計算
if (code1 == "RESB") {
loc[i+1] = loc[i] + std::stoi(code2);
}
// 處理BYTE的位址計算
else if (code1 == "BYTE") {
if(code2 == "C'EOF'") {
loc[i+1] = loc[i] + 3;
}
else {
loc[i+1] = loc[i] + 1;
}
}
// 計算位址(以input.text的內容來看基本上都是format3
else {
loc[i+1] = loc[i] + format;
}
if (label != "") {
address[label] = loc[i];
}
}
// pass 2 get object_code
for (int i = 0; i<= ln; i++) {
string label = src_code[i][0];
string code1 = src_code[i][1];
string code1_op = optable[code1];
string code2 = src_code[i][2];
if (code1 == "BYTE") {
if(code2 == "C'EOF'") {
object_code[i] = "454F46";
}
else {
object_code[i] = "0000" + code2.substr(2,2);
}
}
else if (code1 == "WORD") {
std::string addzero(6 - code2.size(), '0');
object_code[i] = addzero + code2;
}
else if (code1 == "RSUB") {
std::string addzero(6 - code1_op.size(), '0');
object_code[i] = code1_op + addzero;
}
else if (code1 == "RESW" || code1 == "RESB" || code1 == "START" || code1 == "END") {
object_code[i] = "";
}
else {
std::stringstream stream;
stream << std::uppercase << std::hex << address[code2];
std::string result(stream.str());
object_code[i] = code1_op + result;
}
// 確定是PC-relative定址 or Base-relative定址
if(in_pc(hex2dec(object_code[i]), loc[i+1])) {
flag[i].p = 1;
}
else {
if(in_base(hex2dec(object_code[i]), base)) {
flag[i].b = 1;
}
}
// print sol
std::cout << std::uppercase << std::hex << loc[i] << setw(10) << label << setw(10) << code1 << setw(10) << code2 << setw(10) << object_code[i] << std::endl;
}
// 我只是分隔線 //
std::cout << "-----" << std::endl;
// 簡單的SIC組譯器
// 表頭記錄
std::cout << "H^" << src_code[0][0] << " 00" << std::uppercase << std::hex << loc[0] << "^00" << std::uppercase << std::hex << loc[ln]-loc[0] << std::endl;
// 本文記錄
/*
sic_run: 每行目的碼數目
ismax: 記錄目的碼的起始位址
stack_objcode: 目的碼
*/
int sic_run = 0, ismax = 0;
string stack_objcode = "";
for (int i=1; i< ln; i++) {
if(sic_run == 0) {
stack_objcode = "";
std::cout << "T^00" << std::uppercase << std::hex << loc[i] << "^";
stack_objcode = stack_objcode + object_code[i] + "^";
ismax += 3;
}
else {
if (object_code[i] != "") {
if (src_code[i][1] == "BYTE") {
if(src_code[i][2] == "C'EOF'") {
stack_objcode += object_code[i] + "^";
ismax += 3;
}
else {
stack_objcode += src_code[i][2] + "^";
ismax += 1;
}
}
else {
stack_objcode += object_code[i] + "^";
ismax += 3;
}
}
}
sic_run ++;
if(sic_run >= 10 || (i == ln-1)) {
std::cout << setw(2) << setfill('0') << std::uppercase << std::hex << ismax << "^" << stack_objcode << std::endl;
sic_run = 0;
ismax = 0;
}
}
// 結束記錄
std::cout << "E^00" << std::uppercase << std::hex << loc[0] << std::endl;
// close file
file.close();
return 0;
}