Spos Practicals 1 To 4-Machine
Spos Practicals 1 To 4-Machine
Program Code :
package test1;
BufferedReader br = null;
FileReader fr = null;
FileWriter fw = null;
BufferedWriter bw = null;
try {
ArrayList<Integer> pooltab=new
int symptr = 1;
int pooltabptr = 1;
sCurrentLine = br.readLine();
String s1 = "";
if(sCurrentLine!=null) s1 =
sCurrentLine.split(" ")[1]; if
(s1.equals("START")) {
bw.write("AD \t 01 \t");
}
while ((sCurrentLine = br.readLine()) != null)
{ int mind_the_LC = 0; String type = null;
int flag2 = 0;
m.setValue(locptr);
flag2 = 1;
}
}
}
}
if (s.equals("LTORG")) {
pooltab.add(pooltabptr); for
(Map.Entry m : littab.entrySet()) {
if (m.getValue() == "") {
m.setValue(locptr);
locptr++;
pooltabptr++;
mind_the_LC =
1; isOpcode = 1;
if (s.equals("END")) {
pooltab.add(pooltabptr); for
(Map.Entry m : littab.entrySet()) { if
(m.getValue() == "") {
m.setValue(locptr);
locptr++;
mind_the_LC = 1;
if(s.equals("EQU")){
symtab.put("equ", String.valueOf(locptr));
s = sCurrentLine.split(" |\\,")[2];
if (s.equals("AREG"))
{ bw.write("1\t");
isOpcode = 1;
} else if (s.equals("BREG"))
{ bw.write("2\t");
isOpcode = 1;
} else if (s.equals("CREG"))
{ bw.write("3\t");
isOpcode = 1;
} else if (s.equals("DREG"))
{ bw.write("4\t");
isOpcode = 1;
} else {
symtab.put(s, "");
s = sCurrentLine.split(" |\\,")[3];
if (s.contains("=")) { littab.put(s,
""); bw.write("L\t" + litptr +
"\t"); isOpcode = 1;
litptr++;
} else {
symtab.put(s, "");
//Overwrite? bw.write("S\t" +
symptr + "\t"); symptr++;
bw.write("\n");
if (mind_the_LC == 0)
locptr++;
}
String f1 = "SYMTAB.txt";
String f2 = "LITTAB.txt";
String f3 = "POOLTAB.txt";
System.out.println(item);
bw.close();
bw1.close();
bw2.close();
bw3.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output :
PRACTICAL 1(A)
INPUT FILES
MACHINE :
CODE
IS 04 1 L 1
IS 05 1 S 1
IS 04 2 L 2
IS 04 3 S 3
AD 05
IS 01 3 L
IS 00
DL 02 C 1
DL 02 C 1S
AD 02
OUTPUT FILES :
SYMBOL TABLE
8
LOOP 3
LITERAL TABLE
=’4’ 4
=’6’ 10
=’1’ 5
POOL TABLE
1
3
PRACTICAL 1(B) [Pass–2 Implementation]
Program Code : -
try {
String f = "/home/linuxpc-
3/Documents/SPOS_Programs/pass11/i
c.txt";
String
="/home/linuxpc3/Documents/SPOS_P
rograms/pass11/SYMTAB.txt";
="/home/linuxpc3/Documents/SPOS_P
rograms/pass11/LITTAB.txt";
String f3 =
"/home/linuxpc-
3/Documents/SPOS_Programs/pass11/
POOLTAB.txt";
String f4 = "MACHINE_CODE.txt";
BufferedWriter machine_code_file =
new BufferedWriter(fw4); String
sCurrentLine; // will hold each line of
the file containing the IC sCurrentLine
= IC_file.readLine(); int locctr=0;
locctr=Integer.parseInt(sCurrentLine.sp
lit("\t")[4]); while ((sCurrentLine =
IC_file.readLine()) != null) {
machine_code_file.write(locctr+"\t");
String s0 = sCurrentLine.split("\t")[0];
String s1 = sCurrentLine.split("\t")[1];
if (s0.equals("IS")) {
machine_code_file.write(s1 + "\t"); if
(sCurrentLine.split("\t").length == 5) {
machine_code_file.write(sCurrentLine.
split("\t")[2] + "\t");
if (sCurrentLine.split("\t")
[3].equals("L"))
{
int position =
Integer.parseInt(sCurrentLine.split("\t")
[4]);
machine_code_file.write(littab[position
-1][1]);
if (sCurrentLine.split("\t")
[3].equals("S"))
{
int add1 =
Integer.parseInt(sCurrentLine.split("\t")
[4]);
if (i == add1) {
machine_code_file.write((String)
m.getValue());
} i+
+;
else { machine_code_file.write("0\
t000");
if (s0.equals("AD")) {
littab_file.reset();
if (s1.equals("05")) {
int j = 1;
littab_file.readLine();
machine_code_file.write("00\t0\t00"+li
ttab_file.readLine().split("'")[1]);
if(temp1<(temp2-1)){
locctr++;
machine_code_file.write("\n");
machine_code_file.write(locctr+"\t");
}
temp1++;
temp2 = pooltab.get(pooltabptr);
int j = 1;
if (s1.equals("02")) { String s;
machine_code_file.write("00\t0\t00" +
s.split("'")[1]);
j++;
if(s0.equals("DL")&&s1.equals("01")){
machine_code_file.write("00\t0\t00"+s
CurrentLine.split("'")[1]);
machine_code_file.write("\n");
}
IC_file.close(); symtab_file.close();
littab_file.close(); pooltab_file.close();
machine_code_file.close();
System.out.print("Executed"); }
catch (IOException e) {
e.printStackTrace();
Output :-
PRACTICAL 1(B)
OUTPUT FILES :
MACHINE CODE
1 05 1 8
2 04 2 10
3 04 3 9
4 00 0 004
5 00 0 006
6 01 3 5
7 00 0 000
9
10 00 0 001
PRACTICAL 2(A) [Macro Pass–1 Implementation]
Program Code :
package passs1; import
java.io.BufferedReader; import
java.io.BufferedWriter; import
java.io.FileReader; import
java.io.FileWriter; import
java.io.IOException; import
java.util.ArrayList; import
java.util.Hashtable;
import java.util.Map;
FileReader fr = null;
FileWriter fw = null;
try {
fw = new FileWriter(filename1);
String f1 = "/home/linuxpc-
5/3175/PNTAB.txt";
String f2 = "/home/linuxpc-
5/3175/EVNTAB.txt";
String f3 = "/home/linuxpc-
5/3175/SSNTAB.txt";
String f5 = "/home/linuxpc-
5/3175/KPDTAB.txt";
String f6 = "/home/linuxpc-
5/3175/SSTAB.txt";
String s;
s = input_file.readLine(); s =
input_file.readLine();
mnt[6][0] = "#SSTP";
mnt[1][1] = Integer.toString(0);
mnt[2][1] = Integer.toString(0);
mnt[3][1] = Integer.toString(0);
mnt[4][1] = Integer.toString(0);
mnt[5][1] = Integer.toString(0);
mnt[6][1] = Integer.toString(0);
int mdt = 1;
mnt[4][1] = Integer.toString(mdt); .
{ String s1;
if ((s1 = s.split("&|\\,")
[j]).contains("="))
{ kp++; PNTAB.add(s1.split("=")[0]);
KPDTAB.put(s1.split("=")[0],
s1.split("=")[1]);
} else { pp++;
PNTAB.add(s.split("&|\\,")[j]);
mnt[1][1] = Integer.toString(pp);
mnt[2][1] = Integer.toString(kp);
mdt_file.write(mdt + "\t");
if (s.equals("MEND"))
mdt_file.write("MEND");
} else {
String s2;
if ((s.split(" ")[1]).equals("LCL"))
ev++;
s2 = s.split(" ")[2];
EVNTAB.add(s2.split("&")[1]);
mnt[3][1]=Integer.toString(Integer.pars
eInt(mnt[3][1])+1);
if ((s.split(" ")[1]).equals("SET"))
s2 = s.split(" ")[0];
mdt_file.write("E\t" +
(EVNTAB.indexOf(s2.split("&")[1]) +
1) + "\t" + s.split(" ")[1] +
"\t");
for (int z = 2; z < s.split(" ").length;
z++)
if (a.contains("&"))
if (EVNTAB.contains(a.split("&")[1]))
mdt_file.write("E\t" +
(EVNTAB.indexOf(a.split("&")[1]) +
1) + "\t"); }
else {
else {
mdt_file.write(a + "\t");
}
else if ((s.split(" ")[1]).equals("AIF") ||
(s.split(" ")[1]).equals("AGO"))
if (EVNTAB.contains(s3.split("&|\\)")
[1])
)
mdt_file.write("E\t" +
(EVNTAB.indexOf(s3.split("&|\\)")[1])
+ 1) + "\t"); }
if (PNTAB.contains(s3.split("&|\\)")
[1]))
{
mdt_file.write("P\t" +
(PNTAB.indexOf(s3.split("&|\\)")[1]) +
1) + "\t"); }
else if (s3.contains("."))
if (!(SSNTAB.contains(s3.split("
|\\.")[1])))
SSNTAB.add(s3.split(" |\\.")[1]);
mdt_file.write("S\t" +
((SSNTAB.indexOf(s3.split(" |\\.")[1])
+ 1) + "\t"));
else {
mdt_file.write(s3 + "\t");
s4 = s.split(" |\\,")[q];
if (s4.contains("."))
if (!(SSNTAB.contains(s4.split("
|\\.")[1])))
SSNTAB.add(s4.split(" |\\.")[1]);
else if (s4.contains("&"))
{ if (PNTAB.contains(s4.split("&")
[1]))
mdt_file.write("P\t" +
(PNTAB.indexOf(s4.split("&")[1]) + 1)
+ "\t"); }
else if
(EVNTAB.contains(s4.split("&")[1]))
mdt_file.write("E\t" +
(EVNTAB.indexOf(s4.split("&")[1]) +
1) + "\t"); }
}
else if (s4.length() > 0)
if((s.split(" ")[0].contains("."))){
int index=SSNTAB.indexOf(l.split("
|\\.")[1]);
SSTAB.add(index,mdt);
mdt++; mdt_file.write("\n");
mnt_file.write(mnt[z][0] + "\t" +
mnt[z][1]+"\n");
for (Map.Entry m :
KPDTAB.entrySet()) {
kpd_file.write(m.getKey() + "\t" +
m.getValue()+"\n");
}
mnt[3][1] = Integer.toString(ev);
input_file.close(); mdt_file.close();
pn_file.close(); evn_file.close();
ssN_file.close(); mnt_file.close();
kpd_file.close(); ss_file.close();
} catch ( IOException e) {
e.printStackTrace(); } } }
OUTPUT :
PRACTICAL 2(A)
INPUT FILES :
MACRO
LCL &M
LCL&N
&M SET O
MOVER AREG,&X
SUB AREG,&Y
AGO OVER
OVER MEND
SSNTAB
X
PNTAB
X
XZ
MMNTTA
Name EAVL
#PP 3
PRACTICAL 2(A)
OUTPUT FILE :
#KP 1
#EV 2
#MDTP 1
#KPDTP 0
#SSTP 0
MDTTAB
1 LCL E 1
2 LCL E 2
3 E 1 SET 0
4 E 2 SET &NEW
5 AIF P 2 EQ P 1 S 1
6 MOVER AREG P 1
9 AGO S 2
10 MOVER AREG P 3
11 MEND
KPDTAB
P 123
EVNTAB
M
SSTAB
10 11
PRACTICAL 2(B) [Macro Pass–2 Implementation]
Program Code :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
}
void passtwo() throws IOException{
BufferedReader br = new
BufferedReader(new
FileReader("input1.asm"));
String line = "";
int flag=0;
while((line=br.readLine())!=null){
String[] split = line.split("\\s+");
if(split[1].equals("START")){
flag=1;
}
if(flag==1){
if(pntab.containsKey(split[1])
){
ArrayList<String> apt =
new ArrayList<>();
int index =
getIndexMnt(split[1]);
int kpdtp =
mnt.get(index).getKpdtp();
//fill default values
int pp =
mnt.get(index).getPp();
int kp =
mnt.get(index).getKp();
int i=0;
for(i=0;i<pp;i++){
apt.add("0");
}
int j=0;
for(i=pp;i<pp+kp;i++){
String kpara =
kptab.get(kpdtp+j);
kpara=kpara.substring(kpara.indexOf("
=")+1,kpara.length()).replace(",","");
if(kpara.length()>1){
apt.add(kpara);
}else{
apt.add("0");
} j+
+;
}
int k=2,l=0;
while(k<split.length){
if(l<pp){
apt.remove(l);
apt.add(l,split[k].repla
ce(",", ""));
}else{ Strin
g para
= split[k].substring(split[k].indexOf(" &")
+1,split[k].indexOf("="));
String value =
split[k].substring(split[k].indexOf("=")
+1,split[k].length()).replace(",", "");
int in =
pntab.get(split[1]).indexOf(para);
apt.remove(in);
apt.add(in,value);
}
l++;
k++;
}
int mdtp =
mnt.get(index).getMdtp();
while(true){
String inst =
mdt.get(mdtp);
if(inst.contains("MEND
")){
break;
}
String[] sp =
inst.split("\\s+");
k=1;
while(k<sp.length &&
sp[k].contains("P")){
String val
= apt.get(Integer.parseInt(sp[k].substri
ng(sp[k].indexOf(",")+1,sp[k].indexOf(
")"))));
sp[k] = val;
k++;
}
System.out.println(sp[0]
+" "+sp[1]+" "+sp[2]);
mdtp++;
}
System.out.println("APTA
B:");
for(i=0;i<apt.size();i++){
System.out.println(apt.g
et(i));
}
}
}
}
}
}
OUTPUT :
PRACTICAL 2(B)
INPUT FILES :
EVNTAB:
KPDTAB:
P 123
MDTTAB
1. LCL E 1
2. LCL E 2
3. E 1 SET 0
4. E 2 SET &NEW
5. AIF P 2 EQ P 1 S 1
6. MOVER AREG P 1
7. SUB AREG P 2 8 ADD AREG P 3
8. AGO S 2
9. MOVER AREG P 3
10. MEND
MNT
name EAVL
#PP 3
#KP 1
#EV 2
#MDTP 1
#KPDTP 0
#SSTP 0
Z P OVER 11
PRACTICAL 2(B)
OUTPUT FILES :
APTAB:
11
12
13
123
EVTAB:
0
SUB AREG 12
ADD AREG 13
MOVER AREG 13
PRACTICAL 03
FIRST COME FIRST SERVE PROGRAM
import java.util.*;
OUTPUT :
SHORTEST JOB FIRST PROGRAM
import java.util.*;
for(int i=0;i<n;i++)
{
System.out.println ("enter process " + (i+1) + " arrival time:");
at[i] = sc.nextInt();
System.out.println ("enter process " + (i+1) + " brust time:");
bt[i] = sc.nextInt();
pid[i] = i+1;
f[i] = 0;
}
boolean a = true;
while(true)
{
int c=n, min=999;
if (tot == n) // total no of process = completed process loop will be terminated
break;
for (int i=0; i<n; i++)
{
/*
* If i'th process arrival time <= system time and its flag=0 and burst<min
* That process will be executed first
*/
if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))
{
min=bt[i];
c=i;
}
}
/* If c==n means c value can not updated because no process arrival time< system time so we
increase the system time */
if (c==n)
st++;
else
{
ct[c]=st+bt[c];
st+=bt[c];
ta[c]=ct[c]-at[c];
wt[c]=ta[c]-bt[c];
f[c]=1;
tot++; } }
System.out.println("\npid arrival brust complete turn waiting");
for(int i=0;i<n;i++){
avgwt+= wt[i];
avgta+= ta[i];
System.out.println(pid[i]+"\t"+at[i]+"\t"+bt[i]+"\t"+ct[i]+"\t"+ta[i]+"\t"+wt[i]): }
System.out.println ("\naverage tat is "+ (float)(avgta/n));
System.out.println ("average wt is "+ (float)(avgwt/n));
sc.close();
}}
OUTPUT :
PRIORITY SCHEDULING ALGORITHM PROGRAM
import java.util.Arrays;
import java.util.Scanner;
int p = 1;
for (int i = 0; i < numberOfProcess; i++) {
process[i] = "P" + p;
p++;
}
System.out.println(Arrays.toString(process));
System.out.print("Enter Burst Time for " + numberOfProcess + " process: ");
int burstTime[] = new int[numberOfProcess];
for (int i = 0; i < numberOfProcess; i++) {
burstTime[i] = sc.nextInt();
}
System.out.println(Arrays.toString(burstTime));
System.out.print("Enter Priority for " + numberOfProcess + " process: ");
int priority[] = new int[numberOfProcess];
for (int i = 0; i < numberOfProcess; i++) {
priority[i] = sc.nextInt();
}
System.out.println(Arrays.toString(priority));
// Sorting process & burst time by priority
int temp;
String temp2;
for (int i = 0; i < numberOfProcess - 1; i++) {
for (int j = 0; j < numberOfProcess - 1; j++) {
if (priority[j] > priority[j + 1]) {
temp = priority[j];
priority[j] = priority[j + 1];
priority[j + 1] = temp;
temp = burstTime[j];
burstTime[j] = burstTime[j + 1];
burstTime[j + 1] = temp;
temp2 = process[j];
process[j] = process[j + 1];
process[j + 1] = temp2; } } }
int TAT[] = new int[numberOfProcess + 1];
int waitingTime[] = new int[numberOfProcess + 1];
// Calculating Waiting Time & Turn Around Time
for (int i = 0; i < numberOfProcess; i++) {
TAT[i] = burstTime[i] + waitingTime[i];
waitingTime[i + 1] = TAT[i];
}
int totalWT = 0;
int totalTAT = 0;
double avgWT;
double avgTAT;
System.out.println("Process BT WT TAT");
for (int i = 0; i < numberOfProcess; i++) {
System.out.println(process[i] + " " + burstTime[i] + " " + waitingTime[i] + " "+
(TAT[i]));
totalTAT += (waitingTime[i] + burstTime[i]);
totalWT += waitingTime[i]; }
avgWT = totalWT / (double) numberOfProcess;
avgTAT = totalTAT / (double) numberOfProcess;
System.out.println("\n Average Wating Time: " + avgWT);
System.out.println(" Average Turn Around Time: " + avgTAT);
}}
OUTPUT :
ROUND ROBIN ALGORITHM PROGRAM
import java.util.Scanner;
public class roundrobin {
public static void main(String args[])
{
int n,i,qt,count=0,temp,sq=0,bt[],wt[],tat[],rem_bt[];
float awt=0,atat=0;
bt = new int[10];
wt = new int[10];
tat = new int[10];
rem_bt = new int[10];
Scanner s=new Scanner(System.in);
System.out.print("Enter the number of process (maximum 10) = ");
n = s.nextInt();
System.out.print("Enter the burst time of the process\n");
for (i=0;i<n;i++)
{
System.out.print("P"+i+" = ");
bt[i] = s.nextInt();
rem_bt[i] = bt[i];
}
System.out.print("Enter the quantum time: ");
qt = s.nextInt();
while(true) {
for (i=0,count=0;i<n;i++) {
temp = qt;
if(rem_bt[i] == 0) {
count++;
continue; }
if(rem_bt[i]>qt)
rem_bt[i]= rem_bt[i] - qt;
else
if(rem_bt[i]>=0) {
temp = rem_bt[i];
rem_bt[i] = 0; }
sq = sq + temp;
tat[i] = sq; }
if(n == count)
break: }
System.out.print(" ");
System.out.print("\nProcess\t Burst Time\t Turnaround Time\t Waiting Time\n");
System.out.print(" ");
for(i=0;i<n;i++) {
wt[i]=tat[i]-bt[i];
awt=awt+wt[i];
atat=atat+tat[i];
System.out.print("\n "+(i+1)+"\t "+bt[i]+"\t\t "+tat[i]+"\t\t "+wt[i]+"\n"); }
awt=awt/n;
atat=atat/n;
System.out.println("\nAverage waiting Time = "+awt+"\n");
System.out.println("Average turnaround time = "+atat); }}
OUTPUT :
PRACTICAL 04
FIFO PAGE REPLACEMENT PROGRAM
import java.io.*;
class fifopage
{
int n;
int f;
float rat;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of FRAMES :");
f=Integer.parseInt(br.readLine());
int fifo[]=new int[f];
System.out.println("Enter the number of INPUTS :");
n=Integer.parseInt(br.readLine());
int inp[]=new int[n];
System.out.println("Enter INPUT:");
for(int i=0;i<n;i++)
inp[i]=Integer.parseInt(br.readLine());
System.out.println("---------------------");
for(int i=0;i<f;i++)
fifo[i]=-1;
int Hit=0;
int Fault=0;
int j=0;
boolean check;
for(int i=0;i<n;i++)
{
check=false;
for(int k=0;k<f;k++)
if(fifo[k]==inp[i])
{
check=true;
Hit=Hit+1;
}
if(check==false)
{
fifo[j]=inp[i];
j++;
if(j>=f)
j=0;
Fault=Fault+1;
}
}
rat = (float)Hit/(float)n;
System.out.println("HIT:"+Hit+" FAULT:"+Fault+" HIT RATIO:"+rat);
}
}
OUTPUT :
OPTIMAL PAGE REPLACEMENT PROGRAM
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
}
OUTPUT :
LEAST RECENTLY USED PAGE REPLACEMENT PROGRAM
import java.io.*;
import java.util.*;
}
OUTPUT :