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

SPOS Codes

The document contains code for three Java programs: 1) Pass1 reads in an assembly code file, splits each line into tokens, and stores the tokens in symbol, opcode, literal, and pool tables which are then printed out. 2) Pass2 reads the tables from Pass1 and the intermediate code file, translates symbols to addresses, and writes the translated assembly code to a new file. 3) The first programs implement First Come First Serve and Shortest Job First scheduling algorithms to calculate waiting times, turnaround times, and averages for a given set of processes.

Uploaded by

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

SPOS Codes

The document contains code for three Java programs: 1) Pass1 reads in an assembly code file, splits each line into tokens, and stores the tokens in symbol, opcode, literal, and pool tables which are then printed out. 2) Pass2 reads the tables from Pass1 and the intermediate code file, translates symbols to addresses, and writes the translated assembly code to a new file. 3) The first programs implement First Come First Serve and Shortest Job First scheduling algorithms to calculate waiting times, turnaround times, and averages for a given set of processes.

Uploaded by

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

Pass1

import java.io.*;

class pass1 {
public static void main(String args[]) throws Exception {
FileReader fr = new FileReader(args[0]);
BufferedReader br = new BufferedReader(fr);
String line = null;

int line_count = 0, lc = 0, symtabline = 0, optabline = 0, pooltabline


= 0, littabline = 0;

final int max = 100;


String symboltable[][] = new String[max][3];
String optable[][] = new String[max][3];
String littable[][] = new String[max][2];
int pooltable[] = new int[max];

System.out.println("---------------------------------------------");
while((line = br.readLine())!=null) {
String[] tokens = line.split("\t");

if(line_count==0) {
lc = Integer.parseInt(tokens[2]);
for(int i=0; i<tokens.length; i++) {
System.out.println(tokens[i] + "\t");
}
System.out.println("");
}
else {
for(int i=0; i<tokens.length; i++) {
System.out.println(tokens[i] + "\t");
}
System.out.println("");

if(!tokens[0].equals("")) {
symboltable[symtabline][0] = tokens[0];
symboltable[symtabline][1] = Integer.toString(lc);
symboltable[symtabline][2] = Integer.toString(1);
symtabline++;
}
else if (tokens[1].equalsIgnoreCase("DS") ||
tokens[1].equalsIgnoreCase("DC")) {
symboltable[symtabline][0] = tokens[0];
symboltable[symtabline][1] = Integer.toString(lc);
symboltable[symtabline][2] = Integer.toString(1);
symtabline++;
}
if(tokens.length==3 && tokens[2].charAt(0)=='=') {
littable[littabline][0] = tokens[2];
littable[littabline][1] = Integer.toString(lc);
littabline++;
}
else if(tokens[1]!=null) {
optable[optabline][0] = tokens[1];
if(tokens[1].equalsIgnoreCase("START")||
tokens[1].equalsIgnoreCase("END")||tokens[1].equalsIgnoreCase("ORIGIN")||
tokens[1].equalsIgnoreCase("EQU")||tokens[1].equalsIgnoreCase("LTORG")) {
optable[optabline][1] = "AD";
optable[optabline][2] = "R11";
}
else if(tokens[1].equalsIgnoreCase("DS")||
tokens[1].equalsIgnoreCase("DC")) {
optable[optabline][1] = "DL";
optable[optabline][2] = "R7";
}
else {
optable[optabline][1] = "IS";
optable[optabline][2] = "(04, 1)";
}
optabline++;
}
}
line_count++;
lc++;
}
System.out.println("---------------------------------------------");
System.out.println("SYMBOL TABLE");
System.out.println("---------------------------------------------");
System.out.println("SYMBOL \t ADDRESS \t LENGTH");
for(int i=0; i<symtabline; i++) {
System.out.println(symboltable[i][0] + "\t" + symboltable[i][1] +
"\t" + symboltable[i][2]);
}
System.out.println("---------------------------------------------");
System.out.println("OPCODE TABLE");
System.out.println("---------------------------------------------");
System.out.println("MNEMONIC \t CLASS \t INFO");
System.out.println("---------------------------------------------");
for(int i=0; i<optabline; i++) {
System.out.println(optable[i][0] + "\t" + optable[i][1] + "\t" +
optable[i][2]);
}
System.out.println("---------------------------------------------");

System.out.println("LITERAL TABLE");
System.out.println("---------------------------------------------");
System.out.println("LITERAL \t ADDRESS");
System.out.println("---------------------------------------------");
for(int i=0; i<littabline; i++) {
if(littable[i][0]!=null && littable[i+1][0]!=null) {
if(i==0) {
pooltable[pooltabline] = i+1;
pooltabline++;
}
else if(Integer.parseInt(littable[i][1]) <
(Integer.parseInt(littable[i+1][1]))-1) {
pooltable[pooltabline] = i+2;
pooltabline++;
}
}
}
System.out.println("POOL TABLE");
System.out.println("---------------------------------------------");
System.out.println("LITERAL NUMBER");
System.out.println("---------------------------------------------");
for(int i=0; i<pooltabline; i++) {
System.out.println(pooltable[i]);
}
System.out.println("---------------------------------------------");

br.close();
}
}
Pass 2
import java.io.*;
import java.util.HashMap;

public class pass2 {


public static void main(String args[]) throws IOException {
BufferedReader b1 = new BufferedReader(new
FileReader("intermediate.txt"));
BufferedReader b2 = new BufferedReader(new
FileReader("symboltable.txt"));
BufferedReader b3 = new BufferedReader(new
FileReader("literaltable.txt"));

FileWriter f1 = new FileWriter("Pass2.txt");

HashMap<Integer, String> symSymbol = new HashMap<Integer, String>();


HashMap<Integer, String> litSymbol = new HashMap<Integer, String>();
HashMap<Integer, String> litAddress = new HashMap<Integer, String>();
String s;

int symtabPointer = 1, littabPointer = 1, offset;

while((s=b2.readLine())!=null) {
String word[] = s.split("\t\t\t");
symSymbol.put(symtabPointer++, word[1]);
}

while ((s=b3.readLine())!=null) {
String word[] = s.split("\t\t");
litSymbol.put(littabPointer, word[0]);
litAddress.put(littabPointer++, word[1]);
}

while ((s=b1.readLine())!=null) {
if(s.substring(1, 6).compareToIgnoreCase("IS,00")==0) {
f1.write("+ 00 0 000 \n");
}
else if(s.substring(1, 3).compareToIgnoreCase("IS")==0) {
f1.write("+ "+s.substring(4, 6)+" ");
if(s.charAt(9)==')') {
f1.write(s.charAt(8)+" ");
offset=3;
}
else {
f1.write("0 ");
offset=0;
}
if(s.charAt(8+offset)=='S') {

f1.write(symSymbol.get(Integer.parseInt(s.substring(10+offset, s.length()-1)))
+"\n");
}
else {

f1.write(litAddress.get(Integer.parseInt(s.substring(10+offset, s.length()-
1)))+"\n");
}
}
else if(s.substring(1,6).compareToIgnoreCase("DL, 01")==0) {
String s1=s.substring(10, s.length()-1),s2="";
for(int i=0; i<3-s1.length(); i++) {
s2+="0";
s2+=s1;
f1.write("+ 00 0 " + s2 + "\n");
}
}
else {
f1.write("\n");
}
}
f1.close();
b1.close();
b2.close();
b3.close();
}
}
First come First Server
import java.util.*;

public class FCFS {


public static void main(String[] args) {
int id[] = new int[20];
int etime[] = new int[20];//Execution Time
int stime[] = new int[20];//Start Time
int wtime[] = new int[20]; //Waiting Time
int tat[] = new int[20]; //Turn Around Time

int total = 0, total1 = 0;


float avg, avg1;

Scanner sc = new Scanner(System.in);


System.out.println("Enter the number of Processes: ");
int n = sc.nextInt();
for(int i=0; i<n; i++) {
System.out.println();

System.out.println("Enter the process ID for process " + (i+1) +


": ");
id[i] = sc.nextInt();
System.out.println("Enter the execution time for process " + (i+1)
+ ": ");
etime[i] = sc.nextInt();
}

stime[0] = 0;
for(int i=1; i<n; i++) {
stime[i] = stime[i-1] + etime[i-1];
}
wtime[0] = 0;
for(int i=1; i<n; i++) {
wtime[i] = stime[i-1] + id[i];
total = total+wtime[i];
}
for(int i=1; i<n; i++) {
tat[i] = wtime[i] + etime[i];
total1 = total1+tat[i];
}
avg = (float)total/n;
avg1 = (float)total1/n;

System.out.println("Arrial Time\tExecution Time\tStart Time\tWaiting


Time\tTurnAround Time");
for(int i=0; i<n; i++) {
System.out.println(id[i] + "\t\t" + etime[i] + "\t\t" + stime[i]
+ "\t\t" + wtime[i] + "\t\t" + tat[i]);
}
System.out.println("Average Turn Around Time: "+avg1 + "\n Average
Waiting Time: "+avg);

sc.close();
}
}
Shortest Job First

import java.util.*;

public class SJF {


static int[][] mat = new int[10][6];

static void arrangeArrival(int num, int[][] mat) {


for (int i=0; i<num; i++) {
for (int j=0; j<num-i-1; j++) {
if(mat[j][1] > mat[j+1][1]) {
for(int k =0; k<5; k++) {
int temp = mat[j][k];
mat[j][k] = mat[j+1][k];
mat[j+1][k] = temp;
}
}
}
}
}

static void completionTime(int num, int[][] mat) {


int temp, val = -1;
mat[0][3] = mat[0][1] + mat[0][2];
mat[0][5] = mat[0][3] - mat[0][1];
mat[0][4] = mat[0][5] - mat[0][2];

for(int i=1; i<num; i++) {


temp = mat[i-1][3];
int low = mat[i][2];

for(int j=i; j<num; j++) {


if(temp>=mat[j][1] && low>=mat[j][2]) {
low = mat[j][2];
val = j;
}
}
mat[val][3] = temp + mat[val][2];
mat[val][5] = temp + mat[val][3] - mat[val][1];
mat[val][4] = temp + mat[val][5] - mat[val][2];
for(int k=0; k<6; k++) {
int tem = mat[val][k];
mat[val][k] = mat[i][k];
mat[i][k] = tem;
}
}
}

public static void main(String[] args) {


int num;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of Processes: ");


num = sc.nextInt();

System.out.println("...Enter the process ID...");


for(int i=0; i<num; i++) {
System.out.println("...Process" + (i+1) + "...");
System.out.println("Enter Process Id: ");
mat[i][0] = sc.nextInt();
System.out.println("Enter Arrival Time: ");
mat[i][1] = sc.nextInt();
System.out.println("Enter Burst Time: ");
mat[i][2] = sc.nextInt();
}

System.out.println("Before Arrange: ");


System.out.println("Process ID \t Arrival Time \t Burst Time");
for(int i=0; i<num; i++) {
System.out.printf("%d \t\t %d \t\t %d \n",
mat[i][0], mat[i][1], mat[i][2]);
}

arrangeArrival(num, mat);
completionTime(num, mat);
System.out.println("Final Result...");
System.out.println("Process ID \t Arrival Time \t Burst Time \t
Waiting Time \t Turnarount Time");
for(int i=0; i<num; i++) {
System.out.printf("%d \t\t %d \t\t %d \t\t %d \t\t %d \n",
mat[i][0], mat[i][1], mat[i][2], mat[i][3], mat[i][4]);
}
sc.close();
}
}
Round Robin Algorithm
import java.util.Scanner;

public class Round_Robin {


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.println("Enter the number of process: ");
n = s.nextInt();
System.out.println("Enter the burst time: ");
for(i=0; i<n; i++) {
System.out.println("P"+i+"=");
bt[i] = s.nextInt();
rem_bt[i] = bt[i];
}
System.out.println("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.println("-----------------------------------------");
System.out.println("Process \t Burst Time \t TrunAround Time \t
Waiting Time \n");
System.out.println("-----------------------------------------");
for(i=0; i<n; i++) {
wt[i] = tat[i]-bt[i];
awt = awt + wt[i];
atat = atat + tat[i];
System.out.println("\n"+(i+1)+"\t"+bt[i]+"\t"+tat[i]+"\
t"+wt[i]+"\n");
}
awt = awt/n;
atat = atat/n;
}
s.close();
}
}
Least Recently Used Algorithm
import java.util.*;
public class LRU {
public static void main(String args[]) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number of Frames: ");
int nf = sc.nextInt();
System.out.println("Enter number of References: ");
int nr = sc.nextInt();

int page[] = new int[nr];


int frame[] = new int[nr];
System.out.println("Enter Refernces: ");
for(int i=0; i<nr; i++) {
page[i] = sc.nextInt();
}

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


frame[i] = -1;
}

for(int k=0; k<nf; k++) {


System.out.println(" "+frame[k]);
}
System.out.println();

int flag = 0, hit = 0, miss = 0, front = 0, rear = -1;


int age[] = new int[nf];
for(int i=0, j=0; j<nf && i<nr; j=j%nf, i++) {
flag = 0;

for(int y=0; y<nf; y++) {


if(frame[y] == page[i]) {
flag = 1;
hit++;
age[y] = 0;
}
}
if(flag == 0) {
if(frame[j] == -1) {
frame[j] = page[i];
}
miss++;
rear++;
j++;
}
else {
int max = age[0], loc = 0;
miss++;
for(int b=0; b<nf; b++) {
if(age[b] > max) {
max = age[b];
loc = b;
}
frame[loc] = page[i];
age[loc] = 0;
j++;
}
for(int k=0; k<nf; k++){
System.out.println(" "+frame[k]);
System.out.println();
for(int a=0; a<nf; a++) {
if(frame[a] == -1) {
age[a] = 0;
}

else {
age[a]++;
}
}
}
}
}
float hr = (float)hit/((float)hit + (float)miss);
System.out.println("Hit="+hit+" miss="+miss);
System.out.println("page replacement ratio = " + hr);
}
}
First In First Out
import java.io.*;

class FIFO {
public static void main(String args[]) throws IOException {
int n, 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, fault = 0, 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)
fault = fault+1;
}
}
rat = (float)hit/(float)n;
System.out.println("HIT: "+hit+"FAULT: "+fault+"HIT RATIO: "+rat);
}
}
Optimal Page Replacement
import java.io.*;
import java.util.*;

public class optimal {


static boolean search(int key, int[] fr) {
for(int i=0; i<fr.length; i++) {
if(fr[i] == key)
return true;
}
return false;
}

static int predict(int pg[], int[] fr, int pn, int index) {
int res=-1, farthest = index;
for(int i=0; i<fr.length; i++) {
int j;
for(j=index; j<fr.length; j++) {
if(fr[i] == pg[j]) {
if(j>farthest) {
farthest = j;
res = i;
}
break;
}
}
if(j == pn)
return i;
}
return (res == -1) ? 0:res;
}

static void optimalPage(int pg[], int pn, int fn) {


int[] fr = new int[fn];
int hit = 0, index = 0;
for(int i=0; i<pn; i++) {
if(search(pg[i], fr)) {
hit++;
continue;
}
if(index < fn) {
fr[index++] = pg[i];
}
else {
int j= predict(pg, fr, pn, i+1);
fr[j] = pg[i];
}
}
System.out.println("No. of hits = " + hit);
System.out.println("No. of misses = " + (pn - hit));
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the total number of Frames: ");
int fn = sc.nextInt();
System.out.println("Enter the reference string size: ");
int pn = sc.nextInt();
int pg[] = new int[10];
System.out.println("Enter the elements in array: ");
for(int i=0; i<pn; i++) {
pg[i] = sc.nextInt();
}
optimalPage(pg, pn, fn);
}
}

You might also like