Spos Programss
Spos Programss
import java.io.*;
class P1
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]);
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 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.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:-
/*
*/
import java.util.*;
import java.io.*;
class Tuple {
int length;
Tuple() {}
mnemonic = s1;
bin_opcode = s2;
length = Integer.parseInt(s3);
type = s4;
class SymTuple {
symbol = s1;
value = i1;
length = i2;
ra = s2;
class LitTuple {
LitTuple() {}
literal = s1;
value = i1;
length = i2;
ra = s2;
class TwoPassAssembler {
pass1();
pass2();
String s;
s_arr[i] = st.nextToken();
if(searchPot1(s_arr) == false) {
searchMot1(s_arr);
out_pass1.println(s);
lclist.add(lc);
int j;
System.out.println("Symbol Table:");
for(SymTuple i : symtable) {
output = i.symbol;
output += i.value;
System.out.println(output);
out_symtable.println(output);
System.out.println("\nLiteral Table:");
for(LitTuple i : littable) {
output = i.literal;
output += i.value;
System.out.println(output);
out_littable.println(output);
line_no = 0;
out_pass2 = new PrintWriter(new FileWriter("output_pass2.txt"), true);
String s;
System.out.println("Pass 2 input:");
System.out.println(s);
s_arr[i] = st.nextToken();
if(searchPot2(s_arr) == false) {
searchMot2(s_arr);
line_no++;
System.out.println("\nPass 2 output:");
System.out.println(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];
if(i == 1) {
if(index != 0) {
// Ends with F
l = Integer.parseInt(x.substring(0, x.length()-1));
l *= 4;
} else {
// Starts with F
l += 4;
lc += l;
return true;
case 2:
// EQU statement
if(!s[2].equals("*")) {
} else {
return true;
case 3:
// START statement
return true;
case 4:
// LTORG statement
ltorg(false);
return true;
case 5:
// END statement
ltorg(true);
return true;
return false;
int i = 0;
if(s.length == 3) {
i = 1;
s = tokenizeOperands(s);
if(s[j].startsWith("=")) {
for(Tuple x : mot) {
if(s[i].equals(x.mnemonic)) {
t = x;
break;
lc += t.length;
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;
int i = 0;
if(s.length == 3) {
i = 1;
if(s[i].equalsIgnoreCase("USING")) {
s = tokenizeOperands(s);
if(s[i+1].equals("*")) {
} else {
if(value != -1) {
return true;
return false;
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;
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";
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];
if(value != -1) {
output += s[i+1];
} else {
output = s[i];
if(value != -1) {
s[j] = createOffset(s[j]);
output += s[i+1];
}
}
out_pass2.println(output);
String original = s;
int index = 0;
int index_reg = 0;
if(s.startsWith("=")) {
value = getLiteralValue(s);
} else {
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 = new_offset;
index = i;
}
}
return result;
for(SymTuple st : symtable) {
if(s.equalsIgnoreCase(st.symbol)) {
return st.value;
return -1;
s = s.substring(1, s.length());
for(LitTuple lt : littable) {
if(s.equalsIgnoreCase(lt.literal)) {
return lt.value;
return -1;
temp.add(s[j]);
while(st.hasMoreTokens()) {
temp.add(st.nextToken());
s = temp.toArray(new String[0]);
return s;
String s;
BufferedReader br;
pot.add(s);
Collections.sort(pot);
}
**OUTPUT:-
Symbol Table:
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:
A(DATA1) 48 4 R
F'5' 52 4 R
F'4' 56 4 R
F'8000' 60 4 R
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:
SR 4, 4
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
import java.util.*;
import java.io.*;
class MACRO
pass1();
System.out.println("\n*********PASS-1 MACROPROCESSOR***********\n");
display(mnt,mntc,3);
System.out.println("\n");
display(ala,alac,2);
System.out.println("\n");
display(mdt,mdtc,1);
System.out.println("\n");
int index=0,i;
String s,prev="",substring;
try
{
BufferedReader inp = new BufferedReader(new FileReader("input.txt"));
if (!op.exists())
op.createNewFile();
while((s=inp.readLine())!=null)
if(s.equalsIgnoreCase("MACRO"))
prev=s;
for(;!(s=inp.readLine()).equalsIgnoreCase("MEND");mdtc++,prev=s)
if(prev.equalsIgnoreCase("MACRO"))
for(i=0;i<str.length;i++)
str[i]=st.nextToken();
mnt[mntc][1]=str[0];
mnt[mntc++][2]=(++mdtc)+"";
for(i=0;i<string.length;i++)
string[i]=st.nextToken();
index=string[i].indexOf("=");
if(index!=-1)
ala[alac++][1]=string[i].substring(0,index);
else
ala[alac++][1]=string[i];
{ //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)
catch(IOException e)
e.printStackTrace();
}
}
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:-
*********PASS-1 MACROPROCESSOR***********
i macro loc
1 INCR1 1
2 INCR2 5
0 &FIRST
1 &SECOND
2 &ARG1
3 &ARG2
INCR1 &FIRST,&SECOND=DATA9
A 1,#0
L 2,#1
MEND
INCR2 &ARG1,&ARG2=DATA5
L 3,#2
ST 4,#3
MEND
**PROGRAM 4:-
/*
*/
import java.util.*;
import java.io.*;
class MntTuple {
String name;
int index;
MntTuple(String s, int i) {
name = s;
index = i;
class MacroProcessor {
initializeTables();
pass1();
pass2();
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();
mntc++;
pass1Ala(s);
String x = st.nextToken();
x += " ";
int index;
token = st.nextToken();
x += token;
while(st.hasMoreTokens()) {
token = st.nextToken();
x += "," + token;
mdt.add(x);
mdtc++;
addIntoMdt(ala.size()-1);
int index;
while(st.hasMoreTokens()) {
String x = st.nextToken();
l.add(x);
ala.add(l);
ala_macro_binding.put(macro_name, ala_macro_binding.size());
List l = ala.get(ala_number);
boolean isFirst;
while(!s.equalsIgnoreCase("MEND")) {
isFirst = true;
s = input.readLine();
temp = st.nextToken();
line += temp;
while(st.hasMoreTokens()) {
temp = st.nextToken();
if(temp.startsWith("&")) {
int x = l.indexOf(temp);
temp = ",#" + x;
isFirst = false;
} else if(!isFirst) {
line += temp;
mdt.add(line);
mdtc++;
for(List l : ala) {
System.out.println(l);
out.println(l);
for(MntTuple l : mnt) {
System.out.println(l);
out.println(l);
for(String l : mdt) {
System.out.println(l);
out.println(l);
}
}
String s;
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++;
while(!(temp =
mdt.get(mdtp)).trim().equalsIgnoreCase("MEND")) {
line += opcode;
line += st2.nextToken();
while(st2.hasMoreTokens()) {
int index;
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);
List<String> l = ala.get(ala_no);
int ctr = 0;
try {
while(st2.hasMoreTokens()) {
l.set(ctr, st2.nextToken());
ctr++;
} catch(Exception e) {
// do nothing
String s2 = mdt.get(mdtp);
int index = 0;
while(st3.hasMoreTokens()) {
token = st3.nextToken();
try {
l.set(ctr++, token.substring(index+1,
token.length()));
} catch(Exception e) {
// do nothing
ala.set(ala_no, l);
return l;
mntc = 0;
mdtc = 0;
**OUTPUT:-
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
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
int bst[],process[],wt[],tat[],i,j,n,total=0,pos,temp;
n = s.nextInt();
wt = new int[n];
for(i=0;i<n;i++)
System.out.print("\nProcess["+(i+1)+"]: ");
bst[i] = s.nextInt();;
System.out.println("\t\t\t**********FCFS Scheduling*********");
for(i=1;i<n;i++)
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bst[j];
total+=wt[i];
wait_avg=(float)total/n;
total=0;
System.out.println("-----------------------------------------------------------------------");
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("-----------------------------------------------------------------------");
TAT_avg=(float)total/n;
**OUTPUT:-
Process[2]: 13
Process[3]: 2
Process[4]: 3
**********FCFS Scheduling*********
-----------------------------------------------------------------------
-----------------------------------------------------------------------
p1 | 12 | 0 | 12
-----------------------------------------------------------------------
p2 | 13 | 12 | 25
-----------------------------------------------------------------------
p3 | 2 | 25 | 27
-----------------------------------------------------------------------
p4 | 3 | 27 | 30
-----------------------------------------------------------------------
PRIORITY:-
import java.util.Scanner;
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];
//p is process
// t is turnaround time
n = s.nextInt();
for(i=0;i<n;i++)
System.out.print("\nProcess["+(i+1)+"]:");
bt[i] = s.nextInt();
pp[i] = s.nextInt();p[i]=i+1;
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];
}
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.println("-----------------------------------------------------------------------");
awt/=n;
atat/=n;
**OUTPUT:-
Process[1]:10 3
Process[2]:1 1
Process[3]:2 3
Process[4]:1 4
Process[5]:5 2
-----------------------------------------------------------------------
-----------------------------------------------------------------------
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
-----------------------------------------------------------------------
import java.util.Scanner;
int wt[],bt[],rt[],num,quantum,total;
wt = new int[10];
bt = new int[10];
rt = new int[10];
num = s.nextInt();
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;
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;
time+=quantum;
System.out.print(time);System.out.print(" |");
System.out.print("\n-----------");
else
{time+=rt[i];
rt[i]=rt[i]-rt[i];
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
int burst_time[],process[],waiting_time[],tat[],i,j,n,total=0,pp,temp;
float wait_avg,TAT_avg;
n = s.nextInt();
for(i=0;i<n;i++)
System.out.print("\nProcess["+(i+1)+"]: ");
burst_time[i] = s.nextInt();;
//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;
waiting_time[0]=0;
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];
wait_avg=(float)total/n;
total=0;
System.out.println("----------------------------------------------------------");
System.out.println("----------------------------------------------------------");
for(i=0;i<n;i++)
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("----------------------------------------------------------");
TAT_avg=(float)total/n;
}
**PROGRAM 9:-
FIFO:-
import java.io.*;
int buffer[];
int reference[];
int mem_layout[][];
fr = Integer.parseInt(br.readLine());
rl = Integer.parseInt(br.readLine());
buffer[j] = -1;
reference[i] = Integer.parseInt(br.readLine());
}System.out.println();
if(buffer[j] == reference[i])
search = j;
hit++;
break;
if(search == -1)
buffer[pt] = reference[i];
fault++;
pt++;
if(pt == fr)
pt = 0;
mem_layout[i][j] = buffer[j];
System.out.printf("%5d ",mem_layout[j][i]);
System.out.println();
}
**OUTPUT:-
14
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
LRU:-
import java.io.*;
import java.util.*;
int reference[];
int mem_layout[][];
fr = Integer.parseInt(br.readLine());
rl = Integer.parseInt(br.readLine());
buffer[j] = -1;
reference[i] = Integer.parseInt(br.readLine());
System.out.println();
if(stack.contains(reference[i]))
stack.remove(stack.indexOf(reference[i]));
stack.add(reference[i]);
if(buffer[j] == reference[i])
search = j;
hit++;
break;
if(search == -1)
if(isFull)
if(stack.contains(buffer[j]))
min_loc = temp;
pointer = j;
}
}
buffer[pointer] = reference[i];
fault++;
pointer++;
if(pointer == fr)
pointer = 0;
isFull = true;
mem_layout[i][j] = buffer[j];
System.out.printf("%5d ",mem_layout[j][i]);
System.out.println();
**OUTPUT:-
18
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
OPT:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
int buffer[];
int reference[];
int mem_layout[][];
fr = Integer.parseInt(br.readLine());
rl = Integer.parseInt(br.readLine());
buffer[j] = -1;
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
if(buffer[j] == reference[i])
search = j;
hit++;
break;
if(search == -1)
if(isFull)
index[k] = j;
index_flag[k] = true;
break;
if(max == 0)
max = 200;
if(index[j] == 0)
index[j] = 200;
max = index[j];
pt = j;
buffer[pt] = reference[i];
fault++;
if(!isFull)
pt++;
if(pt == fr)
pt = 0;
isFull = true;
mem_layout[i][j] = buffer[j];
{
for(int j = 0; j < rl; j++)System.out.printf("%3d ",mem_layout[j][i]);
System.out.println();
**OUTPUT:-
14
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