-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_assembler.java
More file actions
193 lines (181 loc) · 4.35 KB
/
Copy pathsmart_assembler.java
File metadata and controls
193 lines (181 loc) · 4.35 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
186
187
188
189
190
191
192
193
/*
Question asked in coding round of Smartprix.
Program to execute commands of assembly language. Set of commands given were:
1. ECHO 1: prints the number. Eg. ECHO 1 prints 1
2. Exit: exits the program.
3. SET a 0: assign variable a value 0.
4. ADD 2 3 z: this means z = 2+3 assign sum of first two values to third one.
5. GOTO and LABEL: works as label and Goto defined in c language but label can be before or after goto
6. IF and END: If IF condition is true then statements between IF and END commands get executed otherwise not. Eg. IF a 10 statement1 statement2 END i.e., if a=10.
7. CONTINUE: works as defined in c language.
Variable names will only be alphabetic [a-z] and default values of variables is 0 no need to define or set before use.
*/
import java.util.*;
class P{
public static void main(String args[])
{
StringBuilder ab=new StringBuilder("");
String s1, s2;
Scanner sc=new Scanner(System.in);
/*do
{
s2=sc.nextLine();
ab.append(s2);
if(s2.length()>0)
ab.append("\n");
}while(s2.length()>0);
String inp=ab.toString();*/
String inp="SET a 0\nLABEL 100\nADD a 1 a\nECHO a\nIF a 5\nSET a 9\nIF 2 1\nECHO a\nCONTINUE\nEND\nEXIT\nEND\nGOTO 100";
//"SET a 0\nLABEL 100\nADD a 1 a\nECHO a\nIF a 5\nEXIT\nEND\nGOTO 100";
HashMap<String, Integer> om=new HashMap<String, Integer>();//operand mapping to integer value
Stack<Integer> ifc=new Stack<Integer>();//the most recent if for corresponding continue
int j=0, x=0, x1=0;
List<String> l=new LinkedList<String>();//lines of input in index-wise in LL
for(String s:inp.split("\n"))
l.add(s);
while(j<l.size())
{//System.out.println(ifc);
s1=l.get(j);//one instruction at a time
if(s1.length()==0)
{
j++;
continue;
}
ArrayList<String> a=new ArrayList<String>();
for(String s:s1.split(" "))//parts of instruction
a.add(s);//s1 no longer needed
if(a.get(0).equals("LABEL"))
{//next could be integer or variable /*char c=a.get(1).charAt(0); if(Character.isDigit(c))lm.put((int)c, j);if(Character.isLetter(c))lm.put(om.get(c), j);*/
j++; }
if(a.get(0).equals("GOTO"))//LABEL MAY BE USED AFTER USING GOTO=>search for that label whenever reach
{
int i; s1=a.get(1);
try{
x=Integer.parseInt(s1);
}
catch(NumberFormatException e)
{//GOTO TARGET IN VARIABLE
x=om.get(s1);
}
for(i=0;i<l.size();i++)
{
if(l.get(i).split(" ")[0].equals("LABEL"))
{
s1=l.get(i).split(" ")[1];
try{
x1=Integer.parseInt(s1);
}
catch(NumberFormatException e)
{
x1=om.get(s1);
}
if(x==x1)
{
j=i+1;//next of that label
}
}
}
}
if(a.get(0).equals("IF"))//CONTINUE END
{
String is1=a.get(1), is2=a.get(2);
try{
x1=Integer.parseInt(is1);
}
catch(NumberFormatException e)
{x1=om.get(is1);
}
try{
x=Integer.parseInt(is2);
}
catch(NumberFormatException e)
{x=om.get(is2);
}
if(x1==x)
{
ifc.push(j);//last if instance
j++;//move to next instruction
}
else
{x1=1;
do{
j++;
s1=l.get(j);
if(s1.split(" ")[0].equals("IF"))
x1++;
if(s1.equals("END"))
x1--;
}while(x1>0);
j++;
}
}
if(a.get(0).equals("SET"))
{
s1=a.get(2);//second operand
try{
x=Integer.parseInt(s1);
}
catch(Exception e)//second operand is a variable
{
x=om.get(s1);
}
if(om.containsKey(a.get(1))==false)
{
om.put(a.get(1), x);
}
else
om.replace(a.get(1), x);
j++;
}
if(a.get(0).equals("ADD"))
{
String lab=a.get(3);
s1=a.get(1); s2=a.get(2);
try{
x1=Integer.parseInt(s1);
}
catch(NumberFormatException e){//it's in a variable
x1=om.get(s1);
}
try{
x=Integer.parseInt(s2);
}
catch(NumberFormatException e){
x=om.get(s2);
}
if(om.containsKey(lab)==false)
{
om.put(lab,(x1+x));
}
else
om.replace(lab,(x1+x));
j++;
}
if(a.get(0).equals("ECHO"))
{
s1=a.get(1);
try{
x=Integer.parseInt(s1);
System.out.println(x);
}
catch(NumberFormatException e)
{//variable
x=om.get(s1);
System.out.println(x);
}
j++;
}
if(a.get(0).equals("CONTINUE"))
if(ifc.size()>0)
j=ifc.pop();
if(a.get(0).equals("END"))
{
if(ifc.size()>0)
ifc.pop();//last if record deleted
j++;
}
if(a.get(0).equals("EXIT"))
System.exit(0);
}
}
}