-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_write_2.java
More file actions
64 lines (63 loc) · 3.26 KB
/
Copy pathread_write_2.java
File metadata and controls
64 lines (63 loc) · 3.26 KB
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
import java.io.*;
import java.util.*;
public class read_write_2 {
public static void main(String[] args) throws IOException, FileNotFoundException, ArrayIndexOutOfBoundsException {
int lc = 0;
BufferedReader br = new BufferedReader(
new FileReader("F:\\AVCOE\\College\\TE\\SEM-5\\SPOSL\\01-02\\pass 1\\src\\SYMTAB.txt"));
ArrayList<TableRow> symtab = new ArrayList<>();
String line;
System.out.println("*****Symbol Table*****");
while ((line = br.readLine()) != null) {
String parts[] = line.split("\\s+");
symtab.add(new TableRow(parts[1], Integer.parseInt(parts[2]), Integer.parseInt(parts[0])));
System.out.println(parts[1] + " " + Integer.parseInt(parts[2]) + " " + Integer.parseInt(parts[0]));
}
br.close();
br = new BufferedReader(
new FileReader("F:\\AVCOE\\College\\TE\\SEM-5\\SPOSL\\01-02\\pass 1\\src\\IC.txt"));
BufferedWriter bw = new BufferedWriter(
new FileWriter("op.txt"));
String code;
System.out.println("*****OUTPUT*****");
while ((line = br.readLine()) != null) {
String parts[] = line.split("\\s+");
if (parts[0].contains("DL,02")) {
int constant = Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
lc = lc + constant;
}
if (parts[0].contains("AD") || (parts[0].contains("DL,02"))) {
//ssbw.write("\n");
} else if (parts.length == 1) {
int opcode = Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
code = String.format("%02d", opcode) + "\t0\t" + String.format("%03d", 0) ;
bw.write(code);
} else if (parts.length == 2) {
if (parts[0].contains("DL,01")) {
int constant = Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
code = String.format("%02d", 0) + "\t0\t" + String.format("%03d", constant);
bw.write(/*lc + ")" +*/ code + "\n");
System.out.println(/*lc + ")" +*/ code);
} else if (parts[0].contains("IS")) {
int opcode = Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
int symindex = Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
int add = symtab.get(symindex - 1).getAddress();
code = String.format("%02d", opcode) + "\t0\t" + String.format("%03d", add);
bw.write(/*lc + ")" +*/ code + "\n");
System.out.println(/*lc + ")" +*/ code);
}
} else if (parts.length == 3) {
int opcode = Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
int regcode = Integer.parseInt(parts[1]);
int symindex = Integer.parseInt(parts[2].replaceAll("[^0-9]", ""));
int add = symtab.get(symindex - 1).getAddress();
code = String.format("%02d", opcode) + "\t" + regcode + "\t" + String.format("%03d", add);
bw.write(/*lc + ")" + */code +"\n");
System.out.println(/*lc + ")" +*/ code);
}
lc++;
}
br.close();
bw.close();
}
}