Saturday, April 5 2025

Header Ads

Pass-II of two pass assembler implementation

Problem Statement:-


Implement Pass-II of two pass assembler for pseudo-machine in Java using object oriented features. The output of assignment-1 (intermediate file and symbol table) should be input for this assignment.

You can download source code from this link. ==> SPOS P2_ASM

Assembler Process

Input:-


(AD,01) (C,100)
(DL,02) (C,3)
(IS,04) 1 (S,03)
(IS,01) 1 (S,04)
(IS,05) 1 (S,05)
(AD,04) (S,1)+1
(IS,010) (S,05)
(AD,03) (S,6)+1
(IS,00)
(DL,01) (C,19)
(DL,01) (C,17)
(AD,02)
view raw IC.txt hosted with ❤ by GitHub
START 100
A DS 3
L1 MOVER AREG, B
ADD AREG, C
MOVEM AREG, D
D EQU A+1
L2 PRINT D
ORIGIN L2+1
STOP
B DC '19
C DC '17
END
view raw input.asm hosted with ❤ by GitHub
1 0
2 0
view raw POOLTAB.txt hosted with ❤ by GitHub
1 A 100
2 L1 103
3 B 108
4 C 109
5 D 101
6 L2 106
view raw SYBMTAB.txt hosted with ❤ by GitHub

Support Classes:-


public class TableRow {
String symbol;
int address;
int index;
public TableRow(String symbol, int address) {
super();
this.symbol = symbol;
this.address = address;
}
public TableRow(String symbol, int address,int index) {
super();
this.symbol = symbol;
this.address = address;
this.index=index;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public int getAddress() {
return address;
}
public void setAddress(int address) {
this.address = address;
}
}
view raw TableRow.java hosted with ❤ by GitHub


Main Code:-


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
public class Pass2 {
ArrayList<TableRow> SYMTAB,LITTAB;
public Pass2()
{
SYMTAB=new ArrayList<>();
LITTAB=new ArrayList<>();
}
public static void main(String[] args) {
Pass2 pass2=new Pass2();
try {
pass2.generateCode("IC.txt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readtables()
{
BufferedReader br;
String line;
try
{
br=new BufferedReader(new FileReader("SYMTAB.txt"));
while((line=br.readLine())!=null)
{
String parts[]=line.split("\\s+");
SYMTAB.add(new TableRow(parts[1], Integer.parseInt(parts[2]),Integer.parseInt(parts[0]) ));
}
br.close();
br=new BufferedReader(new FileReader("LITTAB.txt"));
while((line=br.readLine())!=null)
{
String parts[]=line.split("\\s+");
LITTAB.add(new TableRow(parts[1], Integer.parseInt(parts[2]),Integer.parseInt(parts[0])));
}
br.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void generateCode(String filename) throws Exception
{
readtables();
BufferedReader br=new BufferedReader(new FileReader(filename));
BufferedWriter bw=new BufferedWriter(new FileWriter("PASS2.txt"));
String line,code;
while((line=br.readLine())!=null)
{
String parts[]=line.split("\\s+");
if(parts[0].contains("AD")||parts[0].contains("DL,02"))
{
bw.write("\n");
continue;
}
else if(parts.length==2)
{
if(parts[0].contains("DL")) //DC INSTR
{
parts[0]=parts[0].replaceAll("[^0-9]", "");
if(Integer.parseInt(parts[0])==1)
{
int constant=Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
code="00\t0\t"+String.format("%03d", constant)+"\n";
bw.write(code);
}
}
else if(parts[0].contains("IS"))
{
int opcode=Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
if(opcode==10)
{
if(parts[1].contains("S"))
{
int symIndex=Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
code=String.format("%02d", opcode)+"\t0\t"+String.format("%03d", SYMTAB.get(symIndex-1).getAddress())+"\n";
bw.write(code);
}
else if(parts[1].contains("L"))
{
int symIndex=Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
code=String.format("%02d", opcode)+"\t0\t"+String.format("%03d", LITTAB.get(symIndex-1).getAddress())+"\n";
bw.write(code);
}
}
}
}
else if(parts.length==1 && parts[0].contains("IS"))
{
int opcode=Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
code=String.format("%02d", opcode)+"\t0\t"+String.format("%03d", 0)+"\n";
bw.write(code);
}
else if(parts[0].contains("IS") && parts.length==3) //All OTHER IS INSTR
{
int opcode= Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
int regcode=Integer.parseInt(parts[1]);
if(parts[2].contains("S"))
{
int symIndex=Integer.parseInt(parts[2].replaceAll("[^0-9]", ""));
code=String.format("%02d", opcode)+"\t"+regcode+"\t"+String.format("%03d", SYMTAB.get(symIndex-1).getAddress())+"\n";
bw.write(code);
}
else if(parts[2].contains("L"))
{
int symIndex=Integer.parseInt(parts[2].replaceAll("[^0-9]", ""));
code=String.format("%02d", opcode)+"\t"+regcode+"\t"+String.format("%03d", LITTAB.get(symIndex-1).getAddress())+"\n";
bw.write(code);
}
}
}
bw.close();
br.close();
}
}
view raw Pass2.java hosted with ❤ by GitHub


Output:-


04 1 108
01 1 109
05 1 101
10 0 101
00 0 000
00 0 019
00 0 017
view raw Pass2.txt hosted with ❤ by GitHub


You can download source code from this link. ==> SPOS P2_ASM

No comments:

Powered by Blogger.