125 lines
2.3 KiB
Plaintext
125 lines
2.3 KiB
Plaintext
Design suitable data structures and implement pass1 of a two pass asmbler
|
|
import java.util.*;
|
|
import java.io.*;
|
|
class TableRow{
|
|
String name;
|
|
int index;
|
|
int address;
|
|
TableRow(String name,int address){
|
|
super();
|
|
this.name=name;
|
|
this.address=address;
|
|
this.index=0;
|
|
}
|
|
TableRow(String name,int index,int address){
|
|
this.address=address;
|
|
this.name=name;
|
|
this.index=index;
|
|
}
|
|
public void setName(String name) {
|
|
this.name=name;
|
|
}
|
|
public void setIndex(int index) {
|
|
this.index=index;
|
|
}
|
|
public void setAddress(int address) {
|
|
this.address=address;
|
|
}
|
|
public String getName() {
|
|
return this.name;
|
|
}
|
|
public int getIndex() {
|
|
return this.index;
|
|
}
|
|
public int getAddress() {
|
|
return this.address;
|
|
}
|
|
}
|
|
class Initialize{
|
|
HashMap<String,Integer> AD;
|
|
HashMap<String,Integer> IS;
|
|
HashMap<String,Integer> CC;
|
|
HashMap<String,Integer> RG;
|
|
HashMap<String,Integer> DL;
|
|
Initialize(){
|
|
AD=new HashMap<>();
|
|
IS=new HashMap<>();CC=new HashMap<>();
|
|
RG=new HashMap<>();
|
|
DL=new HashMap<>();
|
|
AD.put("START",1);
|
|
AD.put("END",2);
|
|
AD.put("ORIGIN",3);
|
|
AD.put("EQU",4);
|
|
AD.put("LTORG",5);
|
|
DL.put("DC", 1);
|
|
DL.put("DS", 2);
|
|
RG.put("AREG", 1);
|
|
RG.put("BREG", 2);
|
|
RG.put("CREG", 3);
|
|
RG.put("DREG", 4);
|
|
CC.put("LT", 1);
|
|
CC.put("LTE",2);
|
|
CC.put("EQ", 3);
|
|
CC.put("NEQ",4);
|
|
CC.put("GT",5);
|
|
CC.put("GTE",6);
|
|
IS.put("STOP",0);
|
|
IS.put("ADD",1);
|
|
IS.put("SUB",2);
|
|
IS.put("MULT",3);
|
|
IS.put("MOVER",4);
|
|
IS.put("MOVEM",5);
|
|
IS.put("COMP",6);
|
|
IS.put("BC",7);
|
|
IS.put("DIV",8);
|
|
IS.put("READ", 9);
|
|
IS.put("PRINT",10);
|
|
}
|
|
}
|
|
class assembler {
|
|
public static void main(String args[]) throws IOException{
|
|
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
|
|
FileWriter pass1=new FileWriter("pass1.txt");
|
|
FileWriter symtab=new FileWriter("symtab.txt");
|
|
Initialize in=new Initialize();
|
|
int symindex=0;
|
|
int index=0;
|
|
String line;int flag=0;
|
|
while((line=br.readLine())!=null) {
|
|
String parts[]=line.split("\\s+");
|
|
for(int i=0;i<parts.length;i++) {
|
|
parts[i]=parts[i].replaceAll(",","");
|
|
if((in.AD.containsKey(parts[i]))==false) {
|
|
if((in.CC.containsKey(parts[i]))==false) {
|
|
if((in.DL.containsKey(parts[i]))==false){
|
|
if((in.IS.containsKey(parts[i]))==false) {
|
|
if((in.RG.containsKey(parts[i]))==false) {
|
|
symtab.write(parts[i]+"\t"+ +
|
|
+symindex+"\n");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
symtab.close();
|
|
br.close();
|
|
pass1.close();
|
|
}
|
|
}
|
|
INPUT:
|
|
START 100
|
|
L1 MOVER AREG, X
|
|
MOVEM AREG, Y
|
|
ORIGIN L1+3
|
|
NEXT ADD AREG, X
|
|
SUB BREG, Y
|
|
BC LT, L1
|
|
ORIGIN NEXT+5
|
|
MULT CREG, Z
|
|
STOP
|
|
X DS 2
|
|
Y DS 1
|
|
Z DC '9'
|
|
END |