0% found this document useful (0 votes)
34 views

Spos Programss

Uploaded by

Vaibhav Kale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Spos Programss

Uploaded by

Vaibhav Kale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

**PROGRAM 1:-

/* PASS-1 OF TWO PASS ASSEMBLER */

import java.io.*;

class P1

public static void main(String ar[])throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int i;

String a[][]={{"","START","101",""},

{"","MOVER","BREG","ONE"},

{"AGAIN","MULT","BREG","TERM"},

{"","MOVER","CREG","TERM"},

{"","ADD","CREG","N"},

{"","MOVEM","CREG","TERM"},

{"N","DS","2",""},

{"RESULT","DS","2",""},

{"ONE","DC","1",""},

{"TERM","DS","1",""},

{"","END","",""}};

int lc=Integer.parseInt(a[0][2]);

String st[][]=new String[5][2];

int cnt=0,l;

for (i=1;i<11;i++)

if (a[i][0]!="")

st [cnt][0]=a[i][0];

st[cnt][1]=Integer.toString(lc);
cnt++;

if(a[i][1]=="DS")

int d=Integer.parseInt(a[i][2]);

lc=lc+d;

else

lc++;

else

lc++;

System.out.print("***SYMBOL TABLE****\n");

System.out.println("_____________________");

for(i=0;i<5;i++)

for(cnt=0;cnt<2;cnt++)

System.out.print(st[i][cnt]+"\t");

} System.out.println();

String
inst[]={"STOP","ADD","SUB","MULT","MOVER","MOVEM","COMP","BC","DIV","READ","PRINT"};

String reg[]={"NULL","AREG","BREG","CREG","DREG"};

int op[][]=new int[12][3];

int j,k,p=1,cnt1=0;

for(i=1;i<11;i++)
{

for(j=0;j<11;j++)

if(a[i][1].equalsIgnoreCase(inst[j]))

op[cnt1][0]=j;

else

if(a[i][1].equalsIgnoreCase("DS"))

p=Integer.parseInt(a[i][2]);

else if(a[i][1].equalsIgnoreCase("DC"))

op[cnt1][2]=Integer.parseInt(a[i][2]);

for(k=0;k<5;k++)

if(a[i][2].equalsIgnoreCase(reg[k]))

op[cnt1][1]=k;

for(l=0;l<5;l++)

if(a[i][3].equalsIgnoreCase(st[l][0]))

int mn=Integer.parseInt(st[l][1]);

op[cnt1][2]=mn;

}
}

cnt1=cnt1+p;

System.out.println("\n *****OUTPUT*****\n");

System.out.println("**********MOT TABLE**********");

int dlc=Integer.parseInt(a[0][2]);

for(i=0;i<12;i++)

System.out.print(dlc+++"\t");

for(j=0;j<3;j++)

System.out.print(" "+op[i][j]+" ");

System.out.println();

System.out.println("");

}
**OUTPUT:-

***SYMBOL TABLE****

_____________________

AGAIN 102

N 106

RESULT 108

ONE 110

TERM 111

*****OUTPUT*****

**********MOT TABLE**********

101 4 2 110

102 3 2 111

103 4 3 111

104 1 3 106

105 5 3 111

106 0 0 0

107 0 0 0

108 0 0 0

109 0 0 0

110 0 0 1

111 0 0 0

112 0 0 0
**PROGRAM 2:-

/*

Program to implement 2 Pass Assembler in Java

*/

import java.util.*;

import java.io.*;

class Tuple {

String mnemonic, bin_opcode, type;

int length;

Tuple() {}

Tuple(String s1, String s2, String s3, String s4) {

mnemonic = s1;

bin_opcode = s2;

length = Integer.parseInt(s3);

type = s4;

class SymTuple {

String symbol, ra;

int value, length;

SymTuple(String s1, int i1, int i2, String s2) {

symbol = s1;

value = i1;

length = i2;
ra = s2;

class LitTuple {

String literal, ra;

int value, length;

LitTuple() {}

LitTuple(String s1, int i1, int i2, String s2) {

literal = s1;

value = i1;

length = i2;

ra = s2;

class TwoPassAssembler {

static int lc;

static List<Tuple> mot;

static List<String> pot;

static List<SymTuple> symtable;

static List<LitTuple> littable;

static List<Integer> lclist;

static Map<Integer, Integer> basetable;

static PrintWriter out_pass2;

static PrintWriter out_pass1;

static int line_no;

public static void main(String args[]) throws Exception {


initializeTables();

System.out.println("====== PASS 1 ======\n");

pass1();

System.out.println("\n====== PASS 2 ======\n");

pass2();

static void pass1() throws Exception {

BufferedReader input = new BufferedReader(new InputStreamReader(new


FileInputStream("input.txt")));

out_pass1 = new PrintWriter(new FileWriter("output_pass1.txt"), true);

PrintWriter out_symtable = new PrintWriter(new FileWriter("out_symtable.txt"),


true);

PrintWriter out_littable = new PrintWriter(new FileWriter("out_littable.txt"), true);

String s;

while((s = input.readLine()) != null) {

StringTokenizer st = new StringTokenizer(s, " ", false);

String s_arr[] = new String[st.countTokens()];

for(int i=0 ; i < s_arr.length ; i++) {

s_arr[i] = st.nextToken();

if(searchPot1(s_arr) == false) {

searchMot1(s_arr);

out_pass1.println(s);

lclist.add(lc);

int j;

String output = new String();

System.out.println("Symbol Table:");

System.out.println("Symbol Value Length R/A");

for(SymTuple i : symtable) {
output = i.symbol;

for(j=i.symbol.length() ; j < 10 ; j++) {

output += " ";

output += i.value;

for(j=new Integer(i.value).toString().length() ; j < 7 ; j++) {

output += " ";

output += i.length + " " + i.ra;

System.out.println(output);

out_symtable.println(output);

System.out.println("\nLiteral Table:");

System.out.println("Literal Value Length R/A");

for(LitTuple i : littable) {

output = i.literal;

for(j=i.literal.length() ; j < 10 ; j++) {

output += " ";

output += i.value;

for(j=new Integer(i.value).toString().length() ; j < 7 ; j++) {

output += " ";

output += i.length + " " + i.ra;

System.out.println(output);

out_littable.println(output);

static void pass2() throws Exception {

line_no = 0;
out_pass2 = new PrintWriter(new FileWriter("output_pass2.txt"), true);

BufferedReader input = new BufferedReader(new InputStreamReader(new


FileInputStream("output_pass1.txt")));

String s;

System.out.println("Pass 2 input:");

while((s = input.readLine()) != null) {

System.out.println(s);

StringTokenizer st = new StringTokenizer(s, " ", false);

String s_arr[] = new String[st.countTokens()];

for(int i=0 ; i < s_arr.length ; i++) {

s_arr[i] = st.nextToken();

if(searchPot2(s_arr) == false) {

searchMot2(s_arr);

line_no++;

System.out.println("\nPass 2 output:");

input = new BufferedReader(new InputStreamReader(new


FileInputStream("output_pass2.txt")));

while((s = input.readLine()) != null) {

System.out.println(s);

static boolean searchPot1(String[] s) {

int i = 0;

int l = 0;

int potval = 0;

if(s.length == 3) {

i = 1;
}

s = tokenizeOperands(s);

if(s[i].equalsIgnoreCase("DS") || s[i].equalsIgnoreCase("DC")) {

potval = 1;

if(s[i].equalsIgnoreCase("EQU")) {

potval = 2;

if(s[i].equalsIgnoreCase("START")) {

potval = 3;

if(s[i].equalsIgnoreCase("LTORG")) {

potval = 4;

if(s[i].equalsIgnoreCase("END")) {

potval = 5;

switch(potval) {

case 1:

// DS or DC statement

String x = s[i+1];

int index = x.indexOf("F");

if(i == 1) {

symtable.add(new SymTuple(s[0], lc, 4, "R"));

if(index != 0) {

// Ends with F

l = Integer.parseInt(x.substring(0, x.length()-1));

l *= 4;
} else {

// Starts with F

for(int j=i+1 ; j<s.length ; j++) {

l += 4;

lc += l;

return true;

case 2:

// EQU statement

if(!s[2].equals("*")) {

symtable.add(new SymTuple(s[0], Integer.parseInt(s[2]),


"A"));

} else {

symtable.add(new SymTuple(s[0], lc, 1, "R"));

return true;

case 3:

// START statement

symtable.add(new SymTuple(s[0], Integer.parseInt(s[2]), 1, "R"));

return true;

case 4:

// LTORG statement

ltorg(false);

return true;

case 5:

// END statement
ltorg(true);

return true;

return false;

static void searchMot1(String[] s) {

Tuple t = new Tuple();

int i = 0;

if(s.length == 3) {

i = 1;

s = tokenizeOperands(s);

for(int j=i+1 ; j < s.length ; j++) {

if(s[j].startsWith("=")) {

littable.add(new LitTuple(s[j].substring(1, s[j].length()), -1, 4, "R"));

if((i == 1) && (!s[0].equalsIgnoreCase("END"))) {

symtable.add(new SymTuple(s[0], lc, 4, "R"));

for(Tuple x : mot) {

if(s[i].equals(x.mnemonic)) {

t = x;

break;

lc += t.length;

static void ltorg(boolean isEnd) {


Iterator<LitTuple> itr = littable.iterator();

LitTuple lt = new LitTuple();

boolean isBroken = false;

while(itr.hasNext()) {

lt = itr.next();

if(lt.value == -1) {

isBroken = true;

break;

if(!isBroken) {

return;

if(!isEnd) {

while(lc%8 != 0) {

lc++;

lt.value = lc;

lc += 4;

while(itr.hasNext()) {

lt = itr.next();

lt.value = lc;

lc += 4;

static boolean searchPot2(String[] s) {

int i = 0;

if(s.length == 3) {
i = 1;

if(Collections.binarySearch(pot, s[i]) >= 0) {

if(s[i].equalsIgnoreCase("USING")) {

s = tokenizeOperands(s);

if(s[i+1].equals("*")) {

s[i+1] = lclist.get(line_no) + "";

} else {

for(int j=i+1 ; j<s.length ; j++) {

int value = getSymbolValue(s[j]);

if(value != -1) {

s[j] = value + "";

basetable.put(new Integer(s[i+2].trim()), new Integer(s[i+1].trim()));

return true;

return false;

static void searchMot2(String[] s) {

Tuple t = new Tuple();

int i = 0;

int j;

if(s.length == 3) {

i = 1;

}
s = tokenizeOperands(s);

for(Tuple x : mot) {

if(s[i].equals(x.mnemonic)) {

t = x;

break;

String output = new String();

String mask = new String();

if(s[i].equals("BNE")) {

mask = "7";

} else if(s[i].equals("BR")) {

mask = "15";

} else {

mask = "0";

if(s[i].startsWith("B")) {

if(s[i].endsWith("R")) {

s[i] = "BCR";

} else {

s[i] = "BC";

List<String> temp = new ArrayList<>();

for(String x : s) {

temp.add(x);

temp.add(i+1, mask);

s = temp.toArray(new String[0]);

}
if(t.type.equals("RR")) {

output = s[i];

for(j=s[i].length() ; j<6 ; j++) {

output += " ";

for(j=i+1 ; j<s.length ; j++) {

int value = getSymbolValue(s[j]);

if(value != -1) {

s[j] = value + "";

output += s[i+1];

for(j=i+2 ; j<s.length ; j++) {

output += ", " + s[j];

} else {

output = s[i];

for(j=s[i].length() ; j<6 ; j++) {

output += " ";

for(j=i+1 ; j<s.length-1 ; j++) {

int value = getSymbolValue(s[j]);

if(value != -1) {

s[j] = value + "";

s[j] = createOffset(s[j]);

output += s[i+1];

for(j=i+2 ; j<s.length ; j++) {

output += ", " + s[j];

}
}

out_pass2.println(output);

static String createOffset(String s) {

String original = s;

Integer[] key = basetable.keySet().toArray(new Integer[0]);

int offset, new_offset;

int index = 0;

int value = -1;

int index_reg = 0;

if(s.startsWith("=")) {

value = getLiteralValue(s);

} else {

int paranthesis = s.indexOf("(");

String index_string = new String();

if(paranthesis != -1) {

s = s.substring(0, s.indexOf("("));

index_string = original.substring(original.indexOf("(")+1,
original.indexOf(")"));

index_reg = getSymbolValue(index_string);

value = getSymbolValue(s);

offset = Math.abs(value - basetable.get(key[index]));

for(int i=1 ; i<key.length ; i++) {

new_offset = Math.abs(value - basetable.get(key[i]));

if(new_offset < offset) {

offset = new_offset;

index = i;

}
}

String result = offset + "(" + index_reg + ", " + key[index] + ")";

return result;

static int getSymbolValue(String s) {

for(SymTuple st : symtable) {

if(s.equalsIgnoreCase(st.symbol)) {

return st.value;

return -1;

static int getLiteralValue(String s) {

s = s.substring(1, s.length());

for(LitTuple lt : littable) {

if(s.equalsIgnoreCase(lt.literal)) {

return lt.value;

return -1;

static String[] tokenizeOperands(String[] s) {

List<String> temp = new LinkedList<>();

for(int j=0 ; j<s.length-1 ; j++) {

temp.add(s[j]);

StringTokenizer st = new StringTokenizer(s[s.length-1], " ,", false);

while(st.hasMoreTokens()) {
temp.add(st.nextToken());

s = temp.toArray(new String[0]);

return s;

static void initializeTables() throws Exception {

symtable = new LinkedList<>();

littable = new LinkedList<>();

lclist = new ArrayList<>();

basetable = new HashMap<>();

mot = new LinkedList<>();

pot = new LinkedList<>();

String s;

BufferedReader br;

br = new BufferedReader(new InputStreamReader(new


FileInputStream("mot.txt")));

while((s = br.readLine()) != null) {

StringTokenizer st = new StringTokenizer(s, " ", false);

mot.add(new Tuple(st.nextToken(), st.nextToken(), st.nextToken(),


st.nextToken()));

br = new BufferedReader(new InputStreamReader(new FileInputStream("pot.txt")));

while((s = br.readLine()) != null) {

pot.add(s);

Collections.sort(pot);

}
**OUTPUT:-

Symbol Table:

Symbol Value Length R/A

PRGAM2 0 1 R

AC 2 1 A

INDEX 3 1 A

TOTAL 4 1 A

DATABASE 13 1 A

SETUP 6 1 R

LOOP 12 4 R

SAVE 64 4 R

DATAAREA 76 1 R

DATA1 76 4 R

Literal Table:

Literal Value Length R/A

A(DATA1) 48 4 R

F'5' 52 4 R

F'4' 56 4 R

F'8000' 60 4 R

====== PASS 2 ======

Pass 2 input:

USING *,15

LA 15,SETUP

SR TOTAL,TOTAL

USING SETUP,15

L DATABASE,=A(DATA1)

USING DATAAREA,DATABASE
SR INDEX,INDEX

LOOP L AC,DATA1(INDEX)

AR TOTAL,AC

A AC,=F'5'

ST AC,SAVE(INDEX)

A INDEX,=F'4'

C INDEX,=F'8000'

BNE LOOP

LR 1,TOTAL

BR 14

Pass 2 output:

LA 15, 6(0, 15)

SR 4, 4

L 13, 42(0, 15)

SR 3, 3

L 2, 0(3, 13)

AR 4, 2

A 2, 24(0, 13)

ST 2, 12(3, 13)

A 3, 20(0, 13)

C 3, 16(0, 13)

BC 7, 6(0, 15)

LR 1, 4

BCR 15, 14

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/Untitled Folder/a1$


**PROGRAM 3:-
MACRO:-

import java.util.*;

import java.io.*;

class MACRO

static String mnt[][]=new String[5][3]; //assuming 5 macros in 1 program

static String ala[][]=new String[10][2]; //assuming 2 arguments in each macro

static String mdt[][]=new String[20][1]; //assuming 4 LOC for each macro

static int mntc=0,mdtc=0,alac=0;

public static void main(String args[])

pass1();

System.out.println("\n*********PASS-1 MACROPROCESSOR***********\n");

System.out.println("MACRO NAME TABLE (MNT)\n");

System.out.println("i macro loc\n");

display(mnt,mntc,3);

System.out.println("\n");

System.out.println("ARGUMENT LIST ARRAY(ALA) for Pass1\n");

display(ala,alac,2);

System.out.println("\n");

System.out.println("MACRO DEFINITION TABLE (MDT)\n");

display(mdt,mdtc,1);

System.out.println("\n");

static void pass1()

int index=0,i;

String s,prev="",substring;

try

{
BufferedReader inp = new BufferedReader(new FileReader("input.txt"));

File op = new File("pass1_output.txt");

if (!op.exists())

op.createNewFile();

BufferedWriter output = new BufferedWriter(new FileWriter(op.getAbsoluteFile()));

while((s=inp.readLine())!=null)

if(s.equalsIgnoreCase("MACRO"))

prev=s;

for(;!(s=inp.readLine()).equalsIgnoreCase("MEND");mdtc++,prev=s)

if(prev.equalsIgnoreCase("MACRO"))

StringTokenizer st=new StringTokenizer(s);

String str[]=new String[st.countTokens()];

for(i=0;i<str.length;i++)

str[i]=st.nextToken();

mnt[mntc][0]=(mntc+1)+""; //mnt formation

mnt[mntc][1]=str[0];

mnt[mntc++][2]=(++mdtc)+"";

st=new StringTokenizer(str[1],","); //tokenizing the arguments

String string[]=new String[st.countTokens()];

for(i=0;i<string.length;i++)

string[i]=st.nextToken();

ala[alac][0]=alac+""; //ala table formation

index=string[i].indexOf("=");

if(index!=-1)

ala[alac++][1]=string[i].substring(0,index);

else
ala[alac++][1]=string[i];

else //automatically eliminates tagging of arguments in definition

{ //mdt formation

index=s.indexOf("&");

substring=s.substring(index);

for(i=0;i<alac;i++)

if(ala[i][1].equals(substring))

s=s.replaceAll(substring,"#"+ala[i][0]);

mdt[mdtc-1][0]=s;

mdt[mdtc-1][0]=s;

else

output.write(s);

output.newLine();

output.close();

catch(FileNotFoundException ex)

System.out.println("UNABLE TO END FILE ");

catch(IOException e)

e.printStackTrace();

}
}

static void display(String a[][],int n,int m)

int i,j;

for(i=0;i<n;i++)

for(j=0;j<m;j++)

System.out.print(a[i][j]+" ");

System.out.println();

}
**OUTPUT:-

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/a3$ javac MACRO.java

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/a3$ java MACRO

*********PASS-1 MACROPROCESSOR***********

MACRO NAME TABLE (MNT)

i macro loc

1 INCR1 1

2 INCR2 5

ARGUMENT LIST ARRAY(ALA) for Pass1

0 &FIRST

1 &SECOND

2 &ARG1

3 &ARG2

MACRO DEFINITION TABLE (MDT)

INCR1 &FIRST,&SECOND=DATA9

A 1,#0

L 2,#1

MEND

INCR2 &ARG1,&ARG2=DATA5

L 3,#2

ST 4,#3

MEND
**PROGRAM 4:-

/*

Program to implement 2 Pass Macro Assembler in Java

*/

import java.util.*;

import java.io.*;

class MntTuple {

String name;

int index;

MntTuple(String s, int i) {

name = s;

index = i;

public String toString() {

return("[" + name + ", " + index + "]");

class MacroProcessor {

static List<MntTuple> mnt;

static List<String> mdt;

static int mntc;

static int mdtc;

static int mdtp;

static BufferedReader input;

static List<List <String>> ala;


static Map<String, Integer> ala_macro_binding;

public static void main(String args[]) throws Exception {

initializeTables();

System.out.println("===== PASS 1 =====\n");

pass1();

System.out.println("\n===== PASS 2 =====\n");

pass2();

static void pass1() throws Exception {

String s = new String();

input = new BufferedReader(new InputStreamReader(new


FileInputStream("input.txt")));

PrintWriter output = new PrintWriter(new FileOutputStream("output_pass1.txt"),


true);

while((s = input.readLine()) != null) {

if(s.equalsIgnoreCase("MACRO")) {

processMacroDefinition();

} else {

output.println(s);

System.out.println("ALA:");

showAla(1);

System.out.println("\nMNT:");

showMnt();

System.out.println("\nMDT:");

showMdt();

static void processMacroDefinition() throws Exception {


String s = input.readLine();

String macro_name = s.substring(0, s.indexOf(" "));

mnt.add(new MntTuple(macro_name, mdtc));

mntc++;

pass1Ala(s);

StringTokenizer st = new StringTokenizer(s, " ,", false);

String x = st.nextToken();

for(int i=x.length() ; i<12 ; i++) {

x += " ";

String token = new String();

int index;

token = st.nextToken();

x += token;

while(st.hasMoreTokens()) {

token = st.nextToken();

x += "," + token;

mdt.add(x);

mdtc++;

addIntoMdt(ala.size()-1);

static void pass1Ala(String s) {

StringTokenizer st = new StringTokenizer(s, " ,", false);

String macro_name = st.nextToken();

List<String> l = new ArrayList<>();

int index;

while(st.hasMoreTokens()) {

String x = st.nextToken();

if((index = x.indexOf("=")) != -1) {


x = x.substring(0, index);

l.add(x);

ala.add(l);

ala_macro_binding.put(macro_name, ala_macro_binding.size());

static void addIntoMdt(int ala_number) throws Exception {

String temp = new String();

String s = new String();

List l = ala.get(ala_number);

boolean isFirst;

while(!s.equalsIgnoreCase("MEND")) {

isFirst = true;

s = input.readLine();

String line = new String();

StringTokenizer st = new StringTokenizer(s, " ,", false);

temp = st.nextToken();

for(int i=temp.length() ; i<12 ; i++) {

temp += " ";

line += temp;

while(st.hasMoreTokens()) {

temp = st.nextToken();

if(temp.startsWith("&")) {

int x = l.indexOf(temp);

temp = ",#" + x;

isFirst = false;

} else if(!isFirst) {

temp = "," + temp;


}

line += temp;

mdt.add(line);

mdtc++;

static void showAla(int pass) throws Exception {

PrintWriter out = new PrintWriter(new FileOutputStream("out_ala_pass" + pass +


".txt"), true);

for(List l : ala) {

System.out.println(l);

out.println(l);

static void showMnt() throws Exception {

PrintWriter out = new PrintWriter(new FileOutputStream("out_mnt.txt"), true);

for(MntTuple l : mnt) {

System.out.println(l);

out.println(l);

static void showMdt() throws Exception {

PrintWriter out = new PrintWriter(new FileOutputStream("out_mdt.txt"), true);

for(String l : mdt) {

System.out.println(l);

out.println(l);

}
}

static void pass2() throws Exception {

input = new BufferedReader(new InputStreamReader(new


FileInputStream("output_pass1.txt")));

PrintWriter output = new PrintWriter(new FileOutputStream("output_pass2.txt"),


true);

String token = new String();

String s;

while((s = input.readLine()) != null) {

StringTokenizer st = new StringTokenizer(s, " ", false);

while(st.hasMoreTokens()) {

token = st.nextToken();

if(st.countTokens() > 2) {

token = st.nextToken();

MntTuple x = null;

for(MntTuple m : mnt) {

if(m.name.equalsIgnoreCase(token)) {

x = m;

break;

if(x != null) {

mdtp = x.index;

List<String> l = pass2Ala(s);

mdtp++;

String temp = new String();

while(!(temp =
mdt.get(mdtp)).trim().equalsIgnoreCase("MEND")) {

String line = new String();


StringTokenizer st2 = new StringTokenizer(temp,
" ,",false);

for(int i=0 ; i<12 ; i++) {

line += " ";

String opcode = st2.nextToken();

line += opcode;

for(int i=opcode.length() ; i<24 ; i++) {

line += " ";

line += st2.nextToken();

while(st2.hasMoreTokens()) {

String token2 = st2.nextToken();

int index;

if((index = token2.indexOf("#")) != -1) {

line += "," +
l.get(Integer.parseInt(token2.substring(index+1,index+2)));

mdtp++;

output.println(line);

System.out.println(line);

break;

} else {

output.println(s);

System.out.println(s);

break;

System.out.println("\nALA:");
showAla(2);

static List<String> pass2Ala(String s) {

StringTokenizer st = new StringTokenizer(s, " ", false);

int num_tokens = st.countTokens();

String macro_name = st.nextToken();

int ala_no = ala_macro_binding.get(macro_name);

List<String> l = ala.get(ala_no);

int ctr = 0;

StringTokenizer st2 = null;

try {

st2 = new StringTokenizer(st.nextToken(), ",", false);

while(st2.hasMoreTokens()) {

l.set(ctr, st2.nextToken());

ctr++;

} catch(Exception e) {

// do nothing

if(ctr < num_tokens) {

String s2 = mdt.get(mdtp);

StringTokenizer st3 = new StringTokenizer(s2, " ,", false);

String token = new String();

int index = 0;

while(st3.hasMoreTokens()) {

token = st3.nextToken();

if((index = token.indexOf("=")) != -1) {

try {

l.set(ctr++, token.substring(index+1,
token.length()));
} catch(Exception e) {

// do nothing

ala.set(ala_no, l);

return l;

static void initializeTables() {

mnt = new LinkedList<>();

mdt = new ArrayList<>();

ala = new LinkedList<>();

mntc = 0;

mdtc = 0;

ala_macro_binding = new HashMap<>();

**OUTPUT:-

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/a4$ javac MacroProcessor.java

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/a4$ java MacroProcessor

===== PASS 1 =====

ALA:

[&FIRST, &SECOND]

[&ARG1, &ARG2]

MNT:
[INCR1, 0]

[INCR2, 4]

MDT:

INCR1 &FIRST,&SECOND=DATA9

A 1,#0

L 2,#1

MEND

INCR2 &ARG1,&ARG2=DATA5

L 3,#0

ST 4,#1

MEND

===== PASS 2 =====

PRG2 START

USING *,BASE

A 1,DATA1

L 2,DATA2

L 3,DATA3

ST 4,DATA4

FOUR DC F'4'

FIVE DC F'5'

BASE EQU 8

TEMP DS 1F

DROP 8

END

ALA:

[DATA1, DATA2]

[DATA3, DATA4]
**PROGRAM 5:-

FCFS:-

import java.util.Scanner;

class Fcfs

public static void main(String args[]){

int bst[],process[],wt[],tat[],i,j,n,total=0,pos,temp;

float wait_avg, TAT_avg;

Scanner s = new Scanner(System.in);

System.out.print("Enter number of process: ");

n = s.nextInt();

process = new int[n];

bst = new int[n];

wt = new int[n];

tat = new int[n];

System.out.println("\nEnter CPU time:");

for(i=0;i<n;i++)

System.out.print("\nProcess["+(i+1)+"]: ");

bst[i] = s.nextInt();;

process[i]=i+1; //Process Number

System.out.println("\t\t\t**********FCFS Scheduling*********");

//First process has 0 waiting time

wt[0]=0;//calculate waiting time

for(i=1;i<n;i++)

wt[i]=0;

for(j=0;j<i;j++)
wt[i]+=bst[j];

total+=wt[i];

//Calculating Average waiting time

wait_avg=(float)total/n;

total=0;

System.out.println("-----------------------------------------------------------------------");

System.out.println("\nProcess\t\t| Burst Time \t\t|Waiting Time\t\t|Turn Time");

System.out.println("-----------------------------------------------------------------------");

for(i=0;i<n;i++)

tat[i]=bst[i]+wt[i];

total+=tat[i];//Calculating TurnaroundTimetotal+=tat[i];

System.out.println("\np"+process[i]+"\t\t|\t"+bst[i]+"\t\t|\t"+wt[i]+"\t\t|\t"+tat[i]);

System.out.println("-----------------------------------------------------------------------");

}//Calculation of Average Turnaround Time

TAT_avg=(float)total/n;

System.out.println("\n\nAverage Waiting Time: "+wait_avg);

System.out.println("\nAverage Turnaround Time: "+TAT_avg);

**OUTPUT:-

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/c10/FCFS$ javac Fcfs.java

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/c10/FCFS$ java Fcfs

Enter number of process: 4

Enter CPU time:


Process[1]: 12

Process[2]: 13

Process[3]: 2

Process[4]: 3

**********FCFS Scheduling*********

-----------------------------------------------------------------------

Process | Burst Time |Waiting Time |Turn Time

-----------------------------------------------------------------------

p1 | 12 | 0 | 12

-----------------------------------------------------------------------

p2 | 13 | 12 | 25

-----------------------------------------------------------------------

p3 | 2 | 25 | 27

-----------------------------------------------------------------------

p4 | 3 | 27 | 30

-----------------------------------------------------------------------

Average Waiting Time: 16.0

Average Turnaround Time: 23.5


**PROGRAM 6:-

PRIORITY:-

import java.util.Scanner;

public class priority {

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

int x,n,p[],pp[],bt[],w[],t[],i;

float awt,atat;

p = new int[10];

pp = new int[10];

bt = new int[10];

w = new int[10];

t = new int[10];

//n is number of process

//p is process

//pp is process priority

//bt is process burst time

//w is wait time

// t is turnaround time

//awt is average waiting time

//atat is average turnaround time

System.out.print("Enter the number of process : ");

n = s.nextInt();

System.out.print("\n\t Enter CPU time---priority \n");

for(i=0;i<n;i++)

System.out.print("\nProcess["+(i+1)+"]:");

bt[i] = s.nextInt();
pp[i] = s.nextInt();p[i]=i+1;

//sorting on the basis of priority

for(i=0;i<n-1;i++)

for(int j=i+1;j<n;j++)

if(pp[i]<pp[j])

x=pp[i];

pp[i]=pp[j];

pp[j]=x;

x=bt[i];

bt[i]=bt[j];

bt[j]=x;

x=p[i];

p[i]=p[j];

p[j]=x;

w[0]=0;

awt=0;

t[0]=bt[0];

atat=t[0];

for(i=1;i<n;i++)

w[i]=t[i-1];

awt+=w[i];

t[i]=w[i]+bt[i];

atat+=t[i];
}

//Displaying the process

System.out.println("-----------------------------------------------------------------------");

System.out.print("\n\nProcess \t\t |Burst Time \t\t |Wait Time \t\t |Turn Time \n");

System.out.println("-----------------------------------------------------------------------");

for(i=0;i<n;i++)

System.out.print("\n"+p[i]+"\t\t| "+bt[i]+"\t\t| "+w[i]+"\t\t|"+t[i]+"\t\t| "+pp[i]+"\n");

System.out.println("-----------------------------------------------------------------------");

awt/=n;

atat/=n;

System.out.print("\n Average Wait Time : "+awt);

System.out.print("\n Average Turn Around Time : "+atat);

**OUTPUT:-

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/c10/priority$ java priority

Enter the number of process : 5

Enter CPU time---priority

Process[1]:10 3

Process[2]:1 1

Process[3]:2 3

Process[4]:1 4
Process[5]:5 2

-----------------------------------------------------------------------

Process |Burst Time |Wait Time |Turn Time

-----------------------------------------------------------------------

4 |1 |0 |1 |4

3 |2 |1 |3 |3

1 | 10 |3 |13 |3

5 |5 | 13 |18 |2

2 |1 | 18 |19 |1

-----------------------------------------------------------------------

Average Wait Time : 7

Average Turn Around Time : 10lab-a-26@laba26-Vostro-3669:~/Documents/sp


os/spos/c10/priority$
**PROGRAM 7:-

ROUND ROBIN SCHEDULING:-

//Round Robin Scheduling:

import java.util.Scanner;

public class RoundR

public static void main(String args[])

Scanner s = new Scanner(System.in);

int wt[],bt[],rt[],num,quantum,total;

wt = new int[10];

bt = new int[10];

rt = new int[10];

System.out.print("Enter number of processes: ");

num = s.nextInt();

System.out.print("Enter burst time");

for(int i=0;i<num;i++)

System.out.print("\nP["+(i+1)+"]: |\n\t");

bt[i] = s.nextInt();

rt[i] = bt[i];

wt[i]=0;

System.out.print("\n\nEnter quantum:| ");

quantum = s.nextInt();

int rp = num;

int i=0;

int time=0;

System.out.print("----------Round scheduling---------");
System.out.print("\n------------");

wt[0]=0;

while(rp!=0)

if(rt[i]>quantum)

rt[i]=rt[i]-quantum;

System.out.print("\n| P["+(i+1)+"] | ");

time+=quantum;

System.out.print(time);System.out.print(" |");

System.out.print("\n-----------");

else

if(rt[i]<=quantum && rt[i]>0)

{time+=rt[i];

rt[i]=rt[i]-rt[i];

System.out.print("\n| P["+(i+1)+"] | ");

rp--;

System.out.print(time);

System.out.print(" |");

System.out.print("\n-------------");

//System.out.print("\n---------------------------------");

i++;

if(i==num)

i=0;

}
**PROGRAM 8:-

SJF:-

import java.util.Scanner;

class SJF

public static void main(String args[])

int burst_time[],process[],waiting_time[],tat[],i,j,n,total=0,pp,temp;

float wait_avg,TAT_avg;

Scanner s = new Scanner(System.in);

System.out.print("Enter number of process: ");

n = s.nextInt();

process = new int[n];

burst_time = new int[n];

waiting_time = new int[n];

tat = new int[n];

System.out.println("\nEnter Burst time:");

for(i=0;i<n;i++)

System.out.print("\nProcess["+(i+1)+"]: ");

burst_time[i] = s.nextInt();;

process[i]=i+1; //Process Number

}System.out.println("\n \t \t*************** Shortest Job First Scheduling*********");

//Sorting

for(i=0;i<n;i++)

pp=i;

for(j=i+1;j<n;j++)
{

if(burst_time[j]<burst_time[pp])

pp=j;

temp=burst_time[i];

burst_time[i]=burst_time[pp];

burst_time[pp]=temp;

temp=process[i];

process[i]=process[pp];

process[pp]=temp;

//First process has 0 waiting time

waiting_time[0]=0;

//calculate waiting time

for(i=1;i<n;i++)

waiting_time[i]=0;

for(j=0;j<i;j++)

waiting_time[i]+=burst_time[j];

total+=waiting_time[i];

//Calculating Average waiting time

wait_avg=(float)total/n;

total=0;

System.out.println("----------------------------------------------------------");

System.out.println("\nProcess\t| Burst Time \t|Waiting Time\t|Turnaround Time |");

System.out.println("----------------------------------------------------------");

for(i=0;i<n;i++)

tat[i]=burst_time[i]+waiting_time[i];//Calculating Turnaround Time

total+=tat[i];
System.out.println("\n p"+process[i]+" \t | \t "+burst_time[i]+"\t | \t "+waiting_time[i]+"\t |
\t"+tat[i]+" t | t ");

System.out.println("----------------------------------------------------------");

//Calculation of Average Turnaround Time

TAT_avg=(float)total/n;

System.out.println("\n\nAverage Waiting Time: "+wait_avg);

System.out.println("\nAverage Turnaround Time: "+TAT_avg);

}
**PROGRAM 9:-

FIFO:-

import java.io.*;

public class FIFO {

public static void main(String[] args) throws IOException

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int fr,rl,pt = 0, hit = 0, fault = 0;

int buffer[];

int reference[];

int mem_layout[][];

System.out.println("\nENTER THE NUMBER OF FRAMES: ");

fr = Integer.parseInt(br.readLine());

System.out.println("\nENTER THE LENGTH OF REFERENCE STRING: ");

rl = Integer.parseInt(br.readLine());

reference = new int[rl];

mem_layout = new int[rl][fr];

buffer = new int[fr];

for(int j = 0; j < fr; j++)

buffer[j] = -1;

System.out.println("\nENTER THE REFERENCE STRING: ");

for(int i = 0; i < rl; i++)

reference[i] = Integer.parseInt(br.readLine());

}System.out.println();

for(int i = 0; i < rl; i++)

int search = -1;


for(int j = 0; j < fr; j++)

if(buffer[j] == reference[i])

search = j;

hit++;

break;

if(search == -1)

buffer[pt] = reference[i];

fault++;

pt++;

if(pt == fr)

pt = 0;

for(int j = 0; j < fr; j++)

mem_layout[i][j] = buffer[j];

for(int i = 0; i < fr; i++)

for(int j = 0; j < rl; j++)

System.out.printf("%5d ",mem_layout[j][i]);

System.out.println();

System.out.println("\nTOTAL NUMBER OF HITS: " + hit);

System.out.println("\nHIT RATIO IS: " + (float)((float)hit/rl));

System.out.println("\nTOTAL NUMBER OF PAGE FAULT: " + fault);

}
**OUTPUT:-

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/Untitled Folder$ java FIFO

ENTER THE NUMBER OF FRAMES:

ENTER THE LENGTH OF REFERENCE STRING:

14

ENTER THE REFERENCE STRING:

9 9 9 9 9 6 6 6 6 6 6 7 7 7

-1 1 1 1 1 1 4 4 4 4 4 4 1 1

-1 -1 3 3 3 3 3 3 8 8 8 8 8 2
TOTAL NUMBER OF HITS: 5

HIT RATIO IS: 0.35714287

TOTAL NUMBER OF PAGE FAULT: 9


**PROGRAM 10:-

LRU:-
import java.io.*;

import java.util.*;

public class LRU {

public static void main(String[] args) throws IOException

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int rl, fr, pointer = 0, hit = 0, fault = 0;

Boolean isFull = false;int buffer[];

ArrayList<Integer> stack = new ArrayList<Integer>();

int reference[];

int mem_layout[][];

System.out.println("\nENTER THE NUMBER OF FRAMES: ");

fr = Integer.parseInt(br.readLine());

System.out.println("\nENTER THE LENGTH OF REFERENCE STRING: ");

rl = Integer.parseInt(br.readLine());

reference = new int[rl];

mem_layout = new int[rl][fr];

buffer = new int[fr];

for(int j = 0; j < fr; j++)

buffer[j] = -1;

System.out.println("\nENTER THE REFERENCE STRING: ");

for(int i = 0; i < rl; i++)

reference[i] = Integer.parseInt(br.readLine());

System.out.println();

for(int i = 0; i < rl; i++)


{

if(stack.contains(reference[i]))

stack.remove(stack.indexOf(reference[i]));

stack.add(reference[i]);

int search = -1;

for(int j = 0; j < fr; j++)

if(buffer[j] == reference[i])

search = j;

hit++;

break;

if(search == -1)

if(isFull)

{int min_loc = rl;

for(int j = 0; j < fr; j++)

if(stack.contains(buffer[j]))

int temp = stack.indexOf(buffer[j]);

if(temp < min_loc)

min_loc = temp;

pointer = j;

}
}

buffer[pointer] = reference[i];

fault++;

pointer++;

if(pointer == fr)

pointer = 0;

isFull = true;

for(int j = 0; j < fr; j++)

mem_layout[i][j] = buffer[j];

for(int i = 0; i < fr; i++)

for(int j = 0; j < rl; j++)

System.out.printf("%5d ",mem_layout[j][i]);

System.out.println();

System.out.println("\nTOTAL NUMBER OF HIT: " + hit);

System.out.println("\nHIT RATIO: " + (float)((float)hit/rl));

System.out.println("\nTOTAL NUMBER OF FAULTS: " + fault);

**OUTPUT:-

ENTER THE NUMBER OF FRAMES:


3

ENTER THE LENGTH OF REFERENCE STRING:

18

ENTER THE REFERENCE STRING:

111444555222666111

-1 2 2 2 2 2 2 6 6 6 3 3 3 3 3 3 3 3

-1 -1 3 3 3 1 1 1 1 1 1 7 7 7 2 2 2 6

TOTAL NUMBER OF HIT: 4

HIT RATIO: 0.22222222

TOTAL NUMBER OF FAULTS: 14


**PROGRAM 11:-

OPT:-
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Opt {

public static void main(String[] args) throws IOException

{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int rl, fr, pt = 0, hit = 0, fault = 0;

boolean isFull = false;

int buffer[];

int reference[];

int mem_layout[][];

System.out.println("\nENTER THE NUMBER OF FRAMES: ");

fr = Integer.parseInt(br.readLine());

System.out.println("\nENTER THE LENGTH OF REFERENCE STRING: ");

rl = Integer.parseInt(br.readLine());

reference = new int[rl];

mem_layout = new int[rl][fr];

buffer = new int[fr];

for(int j = 0; j < fr; j++)

buffer[j] = -1;

System.out.println("\nENTER THE REFERENCE STRING: ");

for(int i = 0; i < rl; i++)

reference[i] = Integer.parseInt(br.readLine());

}
System.out.println();

for(int i = 0; i < rl; i++)

int search = -1;

for(int j = 0; j < fr; j++)

if(buffer[j] == reference[i])

search = j;

hit++;

break;

if(search == -1)

if(isFull)

int index[] = new int[fr];

boolean index_flag[] = new boolean[fr];

for(int j = i + 1; j < rl; j++){

for(int k = 0; k < fr; k++)

if((reference[j] == buffer[k]) && (index_flag[k] == false))

index[k] = j;

index_flag[k] = true;

break;

int max = index[0];


pt = 0;

if(max == 0)

max = 200;

for(int j = 0; j < fr; j++)

if(index[j] == 0)

index[j] = 200;

if(index[j] > max)

max = index[j];

pt = j;

buffer[pt] = reference[i];

fault++;

if(!isFull)

pt++;

if(pt == fr)

pt = 0;

isFull = true;

for(int j = 0; j < fr; j++)

mem_layout[i][j] = buffer[j];

for(int i = 0; i < fr; i++)

{
for(int j = 0; j < rl; j++)System.out.printf("%3d ",mem_layout[j][i]);

System.out.println();

System.out.println("\nTOTAL NUMBER OF HIT: " + hit);

System.out.println("\nHIT RATIO: " + (float)((float)hit/rl));

System.out.println("\nTOTAL NUMBER OF PAGE FAULT: " + fault);

**OUTPUT:-

lab-a-26@laba26-Vostro-3669:~/Documents/sp os/spos/d14/opt$ java Opt

ENTER THE NUMBER OF FRAMES:

ENTER THE LENGTH OF REFERENCE STRING:

14

ENTER THE REFERENCE STRING:

3
4

1 1 1 1 1 1 1 1 1 1 3 3 3 3

-1 2 2 2 2 2 2 2 2 2 2 7 7 7

-1 -1 3 4 4 4 5 6 6 6 6 6 6 6

TOTAL NUMBER OF HIT: 6

HIT RATIO: 0.42857143

TOTAL NUMBER OF PAGE FAULT: 8

You might also like