Name: Shubhangi Mapare Roll No: 17112 Div: TE A Subject: SPOS Lab
Name: Shubhangi Mapare Roll No: 17112 Div: TE A Subject: SPOS Lab
Assignment No: A1
Problem statement: Design suitable data structures and implement pass-I of a two-pass assembler for
pseudo- machine in Java using object oriented feature. Implementation should consist of a few
instructions from each category and few assembler directives.
Input code:
PassOne.java
package A1;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import A1.LitTuple;
import A1.SymTuple;
import A1.Tuple;
if(curTuple.mnemonic.equalsIgnoreCase("START")){
intermediateStr += lc + " (" + curTuple.m_class + "," + curTuple.opcode + ") ";
lc = Integer.parseInt(s_arr[curIndex+1]);
intermediateStr += "(C," + (s_arr[curIndex+1]) + ") ";
}
else if(curTuple.mnemonic.equalsIgnoreCase("LTORG")){
intermediateStr +=processLTORG();
}
else if(curTuple.mnemonic.equalsIgnoreCase("END")){
intermediateStr += lc + " (" + curTuple.m_class + "," + curTuple.opcode + ") \n";
intermediateStr +=processLTORG();
//break;
}
}
else if(curTuple.m_class.equalsIgnoreCase("DL")){
intermediateStr += lc + " (" + curTuple.m_class + "," + curTuple.opcode + ") ";
if(curTuple.mnemonic.equalsIgnoreCase("DS")){
lc += Integer.parseInt(s_arr[curIndex+1]);
}
else if(curTuple.mnemonic.equalsIgnoreCase("DC")){
lc += curTuple.length;
}
intermediateStr += "(C," + s_arr[curIndex+1] + ") ";
}
//Print the instruction in the intermediate file
System.out.println(intermediateStr);
out_pass1.println(intermediateStr);
//Add the length of the instruction in the location counter
}
//Close intermediate file
out_pass1.flush();
out_pass1.close();
//Print symbol table
System.out.println("====== Symbol Table ======");
SymTuple tuple;
Iterator<SymTuple> it = symtable.values().iterator();
String tableEntry;
while(it.hasNext()){
tuple = it.next();
tableEntry = tuple.symbol + "\t" + tuple.address ;
out_symtable.println(tableEntry);
System.out.println(tableEntry);
}
out_symtable.flush();
out_symtable.close();
//Print literal table
System.out.println("====== Literal Table ======");
LitTuple litTuple;
//Iterator<LitTuple> iterator = littable.values().iterator();
tableEntry = "";
for(int i=0; i<littable.size(); i++){
litTuple = littable.get(i);
tableEntry = litTuple.literal + "\t" + litTuple.address ;
out_littable.println(tableEntry);
System.out.println(tableEntry);
}
out_littable.flush();
out_littable.close();
}
static String processLTORG(){
//Process literal table and assign addresses to every literal in the table
LitTuple litTuple;
String intermediateStr = "";
for(int i=poolTable[iPoolTabPtr-1]; i<littable.size(); i++){
litTuple = littable.get(i);
litTuple.address = lc+"";
intermediateStr += lc + " (DL,02) (C," + litTuple.literal + ") \n";
lc++;
}
//Make a new entry in pool table;
poolTable[iPoolTabPtr] = iLitTabPtr;
iPoolTabPtr++;
return intermediateStr;
}
static String processOperands(String operands){
StringTokenizer st = new StringTokenizer(operands, ",", false);
//Separate out the tokens separated by comma
String s_arr[] = new String[st.countTokens()];
for(int i=0 ; i < s_arr.length ; i++) {
s_arr[i] = st.nextToken();
}
String intermediateStr = "", curToken;
for(int i=0; i <s_arr.length; i++){
curToken = s_arr[i];
if(curToken.startsWith("=")){
//Operand is a literal
//Extract literal from the string
StringTokenizer str = new StringTokenizer(curToken, "'", false);
//Separate out the tokens separated by comma
String tokens[] = new String[str.countTokens()];
for(int j=0 ; j < tokens.length ; j++) {
tokens[j] = str.nextToken();
}
String literal = tokens[1];
insertIntoLitTab(literal,"");
intermediateStr += "(L," + (iLitTabPtr -1) + ")";
}
else if(regAddressTable.containsKey(curToken)){
//Operand is a register name
intermediateStr += "(RG," + regAddressTable.get(curToken) + ") ";
}
else{
//Operand is a symbol
insertIntoSymTab(curToken,"");
intermediateStr += "(S," + (iSymTabPtr -1) + ")";
}
}
return intermediateStr;
}
static void insertIntoSymTab(String symbol, String address){
//Check if the symbol is already present in the symbol table
if(symtable.containsKey(symbol)== true){
//Extract entry from symbol table
SymTuple s = symtable.get(symbol);
//Update its address field
s.address = address;
}
else{
//If symbol is not present in the symbol table, create a new entry
symtable.put(symbol, new SymTuple(symbol, address, 1));
}
iSymTabPtr++;
}
static void insertIntoLitTab(String literal, String address){
//If label is not present in the literal table, create a new entry
littable.add(iLitTabPtr, new LitTuple(literal, address, 1));
iLitTabPtr++;
}
static void initializeTables() throws Exception {
symtable = new LinkedHashMap<>();
littable = new ArrayList<>();
regAddressTable = new HashMap<>();
MOT = new HashMap<>();
String s,mnemonic;
BufferedReader br;
br = new BufferedReader(new InputStreamReader(new FileInputStream("src/A1/mot.txt")));
while((s = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s, "", false);
mnemonic = st.nextToken();
MOT.put(mnemonic, (new Tuple(mnemonic, st.nextToken(), st.nextToken(), st.nextToken())));
}
br.close();
//Initiallize register address table
regAddressTable.put("AREG", "1");
regAddressTable.put("BREG", "2");
regAddressTable.put("CREG", "3");
regAddressTable.put("DREG", "4");
//Initiallize pool table
poolTable[iPoolTabPtr] = iLitTabPtr;
iPoolTabPtr++;
}
}
input.txt
START 100
MOVER AREG,B
ADD BREG,='6'
MOVEM AREG,A
SUB CREG,='1'
LTORG
ADD DREG,='5'
A DS 10
LTORG
SUB AREG,='1'
B DC 1
C DC 1
END
mot.txt
START AD 01 0
END AD 02 0
LTORG AD 05 0
ADD IS 01 1
SUB IS 02 1
MULT IS 03 1
MOVER IS 04 1
MOVEM IS 05 1
DS DL 01 0
DC DL 02 1
Output:
output_pass1.txt
0 (AD,01) (C,100)
100 (IS,04) (RG,1) (S,0)
101 (IS,01) (RG,2) (L,0)
102 (IS,05) (RG,1) (S,1)
103 (IS,02) (RG,3) (L,1)
104 (DL,02) (C,6)
105 (DL,02) (C,1)
106 (IS,01) (RG,4) (L,2)
107 (DL,01) (C,10)
117 (DL,02) (C,5)
118 (IS,02) (RG,1) (L,3)
119 (DL,02) (C,1)
120 (DL,02) (C,1)
121 (AD,02)
121 (DL,02) (C,1)
symtable.txt
B 119
A 107
C 120
littable.txt
6 104
1 105
5 117
1 121