-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPASS1.java
More file actions
185 lines (167 loc) · 3.86 KB
/
PASS1.java
File metadata and controls
185 lines (167 loc) · 3.86 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
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
import java.util.*;
class Assembler1{
ArrayList<ArrayList<String>> ic = null;
ArrayList<ArrayList<String>> sym = null;
ArrayList<ArrayList<String>> lit = null;
ArrayList<ArrayList<String>> pol = null;
public String[][] assignLC(String[][] asm)
{
int start_value = 0;
for(int i = 0;i<asm.length;i++)
{
for(int j = 0;j<4;j++)
{
if(asm[i][j].equals("START")) {
start_value = Integer.parseInt(asm[i][j+1]);
}
}
if(i==0)
{
continue;
}
start_value = start_value + 1;
asm[i][0] = String.valueOf(start_value);
}
return asm;
}
public void printCode(String[][] asm)
{
for(int i = 0;i<asm.length;i++)
{
for(int j = 0;j<4;j++)
{
System.out.print(" " + asm[i][j]);
}
System.out.println();
}
}
public void generateOutput(String[][] asm,String[][] mot)
{
ArrayList<ArrayList<String>> ic = new ArrayList(asm.length);
ArrayList<ArrayList<String>> sym = new ArrayList(asm.length);
ArrayList<ArrayList<String>> lit = new ArrayList(asm.length);
ArrayList<ArrayList<String>> pol = new ArrayList(asm.length);
for(int i = 0;i<asm.length;i++)
{
ic.add(new ArrayList());
sym.add(new ArrayList());
lit.add(new ArrayList());
pol.add(new ArrayList());
}
int sym_index = 0;
int lit_index = 0;
int pol_index = 0;
for(int i = 0;i<asm.length;i++)
{
for(int j = 0;j<4;j++)
{
for(int m = 0;m<mot.length;m++)
{
if(asm[i][j].equals(mot[m][0]))
{
String icStr = "("+mot[m][1] + "," + mot[m][2]+")";
ic.get(i).add(icStr);
}
}
if(asm[i][j].equals(mot[15][0]) || asm[i][j].equals(mot[12][0]))
{
String polStr = "("+pol_index+","+asm[i][j] + "," + asm[i][0]+")";
pol.get(i).add(polStr);
}
if(asm[i][j].matches("[a-zA-Z]"))
{
String symStr = "("+ sym_index +","+ asm[i][j] + "," + asm[i][0]+")";
sym.get(i).add(symStr);
sym_index = sym_index + 1;
String icStr = "(S" +"," + asm[i][j]+")";
ic.get(i).add(icStr);
}
if(asm[i][j].matches("[0-9]+") && asm[i][j] != asm[i][0])
{
String litStr = "("+ lit_index +"," +asm[i][j] + "," + asm[i][0]+")";
lit.get(i).add(litStr);
lit_index = lit_index + 1;
}
}
}
this.ic = ic;
this.sym = sym;
this.lit = lit;
this.pol = pol;
}
}
public class PASS1 {
public static void main(String[] args) {
String asm_code[][]= {
{"","","START","200",""},
{"","","MOVER","AREG","X"},
{"","","MOVEM","BREG","Y"},
{"","X","DS","1",""},
{"","","END","",""}
};
String mot[][] = {
{"STOP","IS","00"},
{"ADD","IS","01"},
{"SUB","IS","02"},
{"MULT","IS","03"},
{"MOVER","IS","04"},
{"MOVEM","IS","05"},
{"COMP","IS","06"},
{"BC","IS","07"},
{"DIV","IS","08"},
{"READ","IS","09"},
{"PRINT","IS","10"},
{"START","AD","01"},
{"END","AD","02"},
{"ORIGIN","AD","03"},
{"EQU","AD","04"},
{"LTORG","AD","05"},
{"DS","DL","01"},
{"DC","DL","02"},
{"AREG","RG","01"},
{"BREG","RG","02"},
{"CREG","RG","03"},
};
Assembler1 assembler = new Assembler1();
String[][] asm_with_lc;
asm_with_lc = assembler.assignLC(asm_code);
assembler.printCode(asm_with_lc);
assembler.generateOutput(asm_with_lc, mot);
System.out.println("IC");
for(ArrayList<String> str:assembler.ic)
{
for(String st:str)
{
System.out.print(st);
}
System.out.println();
}
System.out.println("SYM");
for(ArrayList<String> str:assembler.sym)
{
for(String st:str)
{
System.out.print(st);
}
System.out.println();
}
System.out.println("LIT");
for(ArrayList<String> str:assembler.lit)
{
for(String st:str)
{
System.out.print(st);
}
System.out.println();
}
System.out.println("POOL");
for(ArrayList<String> str:assembler.pol)
{
for(String st:str)
{
System.out.print(st);
}
System.out.println();
}
}
}