0% found this document useful (0 votes)
17 views7 pages

SPOS Assignment 1

The document outlines an assignment to implement the Pass-I of a two-pass assembler using Java, focusing on efficient mnemonic opcode retrieval and symbol table implementation. It includes a code snippet that processes assembly language statements, manages literal and symbol tables, and generates output files. The program reads input from a file, processes it, and outputs the results to another file, displaying various tables such as literal, symbol, pool, and opcode tables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

SPOS Assignment 1

The document outlines an assignment to implement the Pass-I of a two-pass assembler using Java, focusing on efficient mnemonic opcode retrieval and symbol table implementation. It includes a code snippet that processes assembly language statements, manages literal and symbol tables, and generates output files. The program reads input from a file, processes it, and outputs the results to another file, displaying various tables such as literal, symbol, pool, and opcode tables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NAME: TEJAS NALAWADE PRACTICAL NO: 1 ROLL NO: TCOD01

Title of Assignment: Design suitable Data structures and implement Pass-I of a two-pass assembler for
pseudo-machine.

Problem Statement: Implement one pass-I of TWO Pass assembler with hypothetical Instruction set using
Java language. Instruction set should include all types of assembly language statements such as Imperative,
Declarative and Assembler Directive. While designing stress should be given on a) How efficiently Mnemonic
opcode could be implemented so as to enable faster retrieval on op-code. b) Implementation of symbol table
for faster retrieval.

CODE:

import [Link].*;
import [Link].*;

class pass1 {
public static void main(String args[]) throws NullPointerException, FileNotFoundException {
String REG[] = {"ax", "bx", "cx", "dx"};
String IS[] = {"stop", "add", "sub", "mult", "mover", "movem", "comp", "bc", "div", "read"};
String DL[] = {"ds", "dc"};
int temp1 = 0;
int f = 0;
Obj[] literal_table = new Obj[10];
Obj[] symb_table = new Obj[10];
Obj[] optab = new Obj[60];
Pooltable[] pooltab = new Pooltable[5];
String line;

try {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
BufferedWriter bw = new BufferedWriter(new FileWriter("[Link]"));
Boolean start = false;
Boolean end = false, fill_addr = false, ltorg = false;
int total_symb = 0, total_ltr = 0, optab_cnt = 0, pooltab_cnt = 0, loc = 0, temp, pos;

while ((line = [Link]()) != null && !end) {


String tokens[] = [Link](" ", 4);
if (loc != 0 && !ltorg) {
if (f == 1) {
ltorg = false;
loc = loc + temp1 - 1;
[Link]("\n" + [Link](loc));
f = 0;
loc++;
} else {
[Link]("\n" + [Link](loc));
ltorg = false;
loc++;
}
}
ltorg = fill_addr = false;
for (int k = 0; k < [Link]; k++) {
pos = -1;
if (start == true) {
loc = [Link](tokens[k]);
start = false;
}
switch (tokens[k]) {
case "start":
start = true;
pos = 1;
[Link]("\t(AD,"+ pos +")");
break;
case "end":
end = true;
pos = 2;
[Link]("\t(AD," + pos +")\n");
for (temp = 0; temp < total_ltr; temp++) {
if (literal_table[temp].addr == 0) {
literal_table[temp].addr = loc - 1;
[Link]("\t(DL,2) \t (C," + literal_table[temp].name +")"+"\n" + loc++);
}
}
break;
case "origin":
pos = 3;
[Link]("\t (AD," + pos +")");
pos = search(tokens[++k], symb_table, total_symb);
k++;
[Link]("\t(C," +(symb_table[pos].addr) +")");
loc = symb_table[pos].addr;
break;
case "ltorg":
ltorg = true;
pos = 5;
[Link]("\t(AD," + pos +")\n");
for (temp = 0; temp < total_ltr; temp++) {
if (literal_table[temp].addr == 0) {
literal_table[temp].addr = loc - 1;
[Link]("\t(DL,2) \t (C," + literal_table[temp].name +")" +"\n" +loc++);
}
}
if (pooltab_cnt == 0) {
pooltab[pooltab_cnt++] = new Pooltable(0, temp);
} else {
pooltab[pooltab_cnt] = new Pooltable(pooltab[pooltab_cnt - 1].first + pooltab[pooltab_cnt
- 1].total_literals, total_ltr - pooltab[pooltab_cnt - 1].first - 1);
pooltab_cnt++;
}
break;
case "equ":
pos = 4;
[Link]("\t(AD," + pos +")");
String prev_token = tokens[k - 1];
int pos1 = search(prev_token, symb_table, total_symb);
pos = search(tokens[++k], symb_table, total_symb);
symb_table[pos1].addr = symb_table[pos].addr;
[Link]("\t(S," + (pos + 1) + ")");
break;
}
if (pos == -1) {
pos = search(tokens[k], IS);
if (pos != -1) {
[Link]("\t(IS," + (pos) +")");
optab[optab_cnt++] = new Obj(tokens[k], pos);
} else {
pos = search(tokens[k], DL); // DC/DS
if (pos != -1)
{
if(pos == 0)
{ f = 1;}
[Link]("\t(DL," + (pos + 1) + ")");
optab[optab_cnt++] = new Obj(tokens[k], pos);
fill_addr = true;

} else if (tokens[k].matches("[a-zA-Z]+:")) { //label


pos = search(tokens[k], symb_table, total_symb);
if (pos == -1) {
symb_table[total_symb++] = new Obj(tokens[k].substring(0, tokens[k].length() - 1), loc
- 1);
[Link]("\t(S," + total_symb + ")");
pos = total_symb;
}
}
}
}

if (pos == -1) {
pos = search(tokens[k], REG);
if (pos != -1) {
[Link]("\t(RG," + (pos + 1) +")"); //register
} else {
if (tokens[k].matches("='(\\d+)'")) { //literal
String s = tokens[k].substring(2, 3);
literal_table[total_ltr++] = new Obj(s, 0);
[Link]("\t(L," + total_ltr + ")");
}
else if (tokens[k].matches("\\d+") || tokens[k].matches("\\d+H") || tokens[k].matches("\\d+h")
|| tokens[k].matches("\\d+D") || tokens[k].matches("\\d+d")) { //constant
[Link]("\t(C," + tokens[k] + ")");
temp1 = [Link](tokens[k]);
}
else {
pos = search(tokens[k], symb_table, total_symb);
if (fill_addr && pos != -1 && symb_table[pos].addr == 0) {
symb_table[pos].addr = loc - 1;
fill_addr = false;
} else if (pos == -1) {
symb_table[total_symb++] = new Obj(tokens[k], 0);
[Link]("\t (S," + total_symb + ")");
} else {
[Link]("\t(S," + pos + ")");
}
}
}
}
}
}

pooltab[pooltab_cnt] = new Pooltable(pooltab[pooltab_cnt - 1].first + pooltab[pooltab_cnt -


1].total_literals, total_ltr - pooltab[pooltab_cnt - 1].first - 2);
pooltab_cnt++;

[Link]("\n*LITERAL TABLE*");
[Link]("\nIndex\tLITERAL\tADDRESS");
for (int i = 0; i < total_ltr; i++) {
if (literal_table[i].addr == 0) {
literal_table[i].addr = loc++;
}
[Link]((i) + "\t" + literal_table[i].name + "\t" + literal_table[i].addr);
}

[Link]("\n*SYMBOL TABLE*");
[Link]("\nSYMBOL\tADDRESS");
for (int i = 0; i < total_symb; i++) {
[Link](symb_table[i].name + "\t" + symb_table[i].addr);
}

[Link]("\n*POOL TABLE*");
[Link]("\nPOOL\tTOTAL LITERALS");
for (int i = 0; i < pooltab_cnt; i++) {
[Link](pooltab[i].first + "\t" + pooltab[i].total_literals);
}

[Link]("\n*OPTABLE*");
[Link]("\nMNEMONIC\tOPCODE");
for (int i = 0; i < [Link]; i++) {
[Link](IS[i] + "\t\t" + i);
}

[Link]();
[Link]();

} catch (Exception e) {
[Link]("error while reading the file");
[Link]();
}

try {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
[Link]("\n*[Link]\n");
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (IOException e) {
[Link]();
}
}

public static int search(String token, String[] list) {


for (int i = 0; i < [Link]; i++) {
if ([Link](list[i])) {
return i;
}
}
return -1;
}

public static int search(String token, Obj[] list, int cnt) {


for (int i = 0; i < cnt; i++) {
if ([Link](list[i].name)) {
return i;
}
}
return -1;
}
}
OUTPUT:

You might also like