0% found this document useful (0 votes)
162 views54 pages

Spos Practicals 1 To 4-Machine

The document describes a program that takes assembly code as input and generates machine code output by performing a two-pass assembler process. The first pass parses the assembly code and generates symbol, literal and pool tables. The second pass reads these tables and the intermediate code to generate the final machine code, resolving all symbols and literals to their appropriate addresses. Key steps include mapping opcodes and directives to codes, tracking symbol and literal locations, handling the literal pool, and writing the final machine code by replacing symbols and literals with their addresses from the tables.

Uploaded by

Rushikesh Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views54 pages

Spos Practicals 1 To 4-Machine

The document describes a program that takes assembly code as input and generates machine code output by performing a two-pass assembler process. The first pass parses the assembly code and generates symbol, literal and pool tables. The second pass reads these tables and the intermediate code to generate the final machine code, resolving all symbols and literals to their appropriate addresses. Key steps include mapping opcodes and directives to codes, tracking symbol and literal locations, handling the literal pool, and writing the final machine code by replacing symbols and literals with their addresses from the tables.

Uploaded by

Rushikesh Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

PRACTICAL 1(A) [Pass–1 Implementation]

Program Code :
package test1;

//package com.company; import


java.io.BufferedReader; import
java.io.*; import java.util.*; public class
prac1 { public static void main(String[]
args) {

BufferedReader br = null;

FileReader fr = null;

FileWriter fw = null;

BufferedWriter bw = null;

try {

String inputfilename = "/home/linuxpc-1/Desktop/input.txt";


fr = new FileReader(inputfilename); br = new
BufferedReader(fr); String OUTPUTFILENAME = "ic.txt";
fw = new FileWriter(OUTPUTFILENAME); bw = new
BufferedWriter(fw);

Hashtable<String, String> is = new Hashtable<String,


String>(); is.put("STOP", "00"); is.put("ADD", "01");
is.put("SUB", "02"); is.put("MULT", "03"); is.put("MOVER",
"04"); is.put("MOVEM", "05"); is.put("COMP", "06");
is.put("BC", "07"); is.put("DIV", "08");
is.put("READ", "09");
is.put("PRINT", "10");

Hashtable<String, String> dl = new Hashtable<String,


String>(); dl.put("DC", "01"); dl.put("DS", "02");

Hashtable<String, String> ad = new Hashtable<String,


String>(); ad.put("START", "01"); ad.put("END", "02");
ad.put("ORIGIN", "03"); ad.put("EQU", "04");
ad.put("LTORG", "05");

Hashtable<String, String> symtab = new Hashtable<String, String>();

Hashtable<String, String> littab = new Hashtable<String, String>();

ArrayList<Integer> pooltab=new

ArrayList<Integer>(); String sCurrentLine; int locptr


= 0; int litptr = 1;

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");

String s2 = sCurrentLine.split(" ")


[2]; bw.write("C \t" + s2 + "\n");
locptr = Integer.parseInt(s2);

}
while ((sCurrentLine = br.readLine()) != null)
{ int mind_the_LC = 0; String type = null;

int flag2 = 0;

String s = sCurrentLine.split(" |\\,")[0];

for (Map.Entry m : symtab.entrySet()) {


if (s.equals(m.getKey())) {

m.setValue(locptr);

flag2 = 1;

}
}

if (s.length() != 0 && flag2 == 0) {


symtab.put(s, String.valueOf(locptr));
symptr++;

int isOpcode = 0; //checks whether current word is an opcode or not s

= sCurrentLine.split(" |\\,")[1]; for (Map.Entry m :

is.entrySet()) { if (s.equals(m.getKey())) { bw.write("IS\t" +

m.getValue() + "\t"); type = "is"; isOpcode = 1;

for (Map.Entry m : ad.entrySet()) { if


(s.equals(m.getKey())) { bw.write("AD\t"
+ m.getValue() + "\t"); type = "ad";
isOpcode = 1;

}
}

for (Map.Entry m : dl.entrySet()) { if


(s.equals(m.getKey())) { bw.write("DL\t" +
m.getValue() + "\t"); type = "dl";
isOpcode = 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));

if (sCurrentLine.split(" |\\,").length > 2) {

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 if (type == "dl") {


bw.write("C\t" + s + "\t");

} else {

symtab.put(s, "");

if (sCurrentLine.split(" |\\,").length > 3) {

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";

FileWriter fw1 = new FileWriter(f1); BufferedWriter


bw1 = new BufferedWriter(fw1); for (Map.Entry m :
symtab.entrySet()) { bw1.write(m.getKey() + "\t" +
m.getValue()+"\n");

System.out.println(m.getKey() + " " + m.getValue());

String f2 = "LITTAB.txt";

FileWriter fw2 = new FileWriter(f2);

BufferedWriter bw2 = new BufferedWriter(fw2);

for (Map.Entry m : littab.entrySet()) {

bw2.write(m.getKey() + "\t" + m.getValue()+"\n");

System.out.println(m.getKey() + " " + m.getValue());

String f3 = "POOLTAB.txt";

FileWriter fw3 = new FileWriter(f3);

BufferedWriter bw3 = new BufferedWriter(fw3);


for (Integer item : pooltab) { bw3.write(item+"\n");

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 : -

package test2; import


java.io.BufferedReader; import
java.io.BufferedWriter; import
java.io.FileReader; import
java.io.FileWriter; import
java.io.IOException; import
java.lang.reflect.Array; import
java.util.ArrayList; import
java.util.Hashtable; import
java.util.Map; public class pass2 {
public static void main(String[] args)
throws NumberFormatException {

try {

String f = "/home/linuxpc-
3/Documents/SPOS_Programs/pass11/i
c.txt";

FileReader fw = new FileReader(f);

BufferedReader IC_file = new


BufferedReader(fw);

String
="/home/linuxpc3/Documents/SPOS_P
rograms/pass11/SYMTAB.txt";

FileReader fw1 = new FileReader(f1);

BufferedReader symtab_file = new


BufferedReader(fw1);
symtab_file.mark(500);
String f2

="/home/linuxpc3/Documents/SPOS_P
rograms/pass11/LITTAB.txt";

FileReader fw2 = new FileReader(f2);

BufferedReader littab_file = new


BufferedReader(fw2);
littab_file.mark(500);

String f3 =

"/home/linuxpc-
3/Documents/SPOS_Programs/pass11/
POOLTAB.txt";

FileReader fw3 = new FileReader(f3);

BufferedReader pooltab_file = new


BufferedReader(fw3);

String littab[][]=new String[10][2] ;

Hashtable<String, String> symtab =


new Hashtable<String, String>();
String str; int z=0;

while ((str = littab_file.readLine()) !=


null) { littab[z][0]=str.split("\t")[0];
littab[z][1]=str.split("\t")[1];

z++; //increment to store in the next


row

while ((str = symtab_file.readLine()) !=


null) {
symtab.put(str.split("\t")[0],
str.split("\t")[1]);

ArrayList<Integer> pooltab = new


ArrayList<Integer>(); String t; // will
hold each line of the file

while ((t = pooltab_file.readLine()) !=


null) { pooltab.add(Integer.parseInt(t));

int pooltabptr = 1; int temp1 =


pooltab.get(0); int temp2 =
pooltab.get(1);

String f4 = "MACHINE_CODE.txt";

FileWriter fw4 = new FileWriter(f4);

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]);

int i = 1; String l1; for (Map.Entry m :


symtab.entrySet()) {

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;

while (j < temp1) {

littab_file.readLine();

while (temp1 < temp2) {

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++;

temp1 = temp2; pooltabptr++; if


(pooltabptr < pooltab.size()) {

temp2 = pooltab.get(pooltabptr);

int j = 1;

if (s1.equals("02")) { String s;

while ((s = littab_file.readLine()) !=


null) { if (j >= temp1)

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;

public class pass1 { public static void


main(String[] args) {

BufferedReader input_file = null;

FileReader fr = null;

FileWriter fw = null;

BufferedWriter mdt_file = null;

try {

String filename = "/home/linuxpc-


5/3175/Input.txt"; fr = new
FileReader(filename);

input_file = new BufferedReader(fr);


String filename1 = "/home/linuxpc-
5/3175/MDT.txt";

fw = new FileWriter(filename1);

mdt_file = new BufferedWriter(fw);

String f1 = "/home/linuxpc-
5/3175/PNTAB.txt";

FileWriter fw1 = new FileWriter(f1);

BufferedWriter pn_file = new


BufferedWriter(fw1);

String f2 = "/home/linuxpc-
5/3175/EVNTAB.txt";

FileWriter fw2 = new FileWriter(f2);

BufferedWriter evn_file = new


BufferedWriter(fw2);

String f3 = "/home/linuxpc-
5/3175/SSNTAB.txt";

FileWriter fw3 = new FileWriter(f3);

BufferedWriter ssN_file = new


BufferedWriter(fw3);
String f4 = "/home/linuxpc-
5/3175/MNT.txt";

FileWriter fw4 = new FileWriter(f4);

BufferedWriter mnt_file = new


BufferedWriter(fw4);

String f5 = "/home/linuxpc-
5/3175/KPDTAB.txt";

FileWriter fw5 = new FileWriter(f5);

BufferedWriter kpd_file = new


BufferedWriter(fw5);

String f6 = "/home/linuxpc-
5/3175/SSTAB.txt";

FileWriter fw6 = new FileWriter(f6);

BufferedWriter ss_file = new


BufferedWriter(fw6);

String s;

s = input_file.readLine(); s =
input_file.readLine();

String mnt[][] = new String[7][2];


mnt[0][0] = "name"; mnt[1][0] =
"#PP"; mnt[2][0] = "#KP"; mnt[3][0] =
"#EV"; mnt[4][0] = "#MDTP";
mnt[5][0] = "#KPDTP";

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);

ArrayList<String> PNTAB = new


ArrayList<String>();

ArrayList<String> EVNTAB = new


ArrayList<String>();

ArrayList<String> SSNTAB = new


ArrayList<String>();

ArrayList<Integer> SSTAB = new


ArrayList<Integer>();

Hashtable<String, String> KPDTAB =


new Hashtable<String, String>();

int pp = 0; int kp = 0; int ev = 0;

int mdt = 1;

mnt[4][1] = Integer.toString(mdt); .

mnt[0][1] = s.split(" ")[1];


int i = s.split("&|\\,").length;

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

{ 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);

while ((s = input_file.readLine()) !=


null)

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);

mdt_file.write(s.split(" ")[1] + "\tE\t" +


(EVNTAB.indexOf(s2.split("&")[1]) +
1)); }

else if ((s2 = s.split(" ")


[1]).equals("SET") || (s2 = s.split("
")[1]).equals("AIF") || (s2 = s.split("
")[1]).equals("AGO"))

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++)

String a = s.split(" ")[z];

if (a.contains("&"))

if (EVNTAB.contains(a.split("&")[1]))

mdt_file.write("E\t" +
(EVNTAB.indexOf(a.split("&")[1]) +
1) + "\t"); }

else {

mdt_file.write(s.split(" ")[z] + "\t");

else {

mdt_file.write(a + "\t");

}
else if ((s.split(" ")[1]).equals("AIF") ||
(s.split(" ")[1]).equals("AGO"))

mdt_file.write(s.split(" ")[1] + "\t");

for (int y = 2; y < s.split(" ").length;


y++)

String s3 = s.split(" ")[y]; if


(s3.contains("&"))

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");

} } else { String s4; int len; len =


s.split(" |\\,").length; for (int q = 0; q <
len; q++)

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)

String l=s.split(" ")[0];

if((s.split(" ")[0].contains("."))){

int index=SSNTAB.indexOf(l.split("
|\\.")[1]);

SSTAB.add(index,mdt);

mdt++; mdt_file.write("\n");

} for (int p = 0; p < PNTAB.size();


p++) { pn_file.write(PNTAB.get(p) +
"\n");

} for (int p = 0; p < EVNTAB.size(); p+


+) { evn_file.write(EVNTAB.get(p)
+ "\n");

} for (int p = 0; p < SSNTAB.size(); p+


+) { ssN_file.write(SSNTAB.get(p)
+ "\n");

for (int z = 0; z < 7; z++) {

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");
}

for (int p = 0; p < SSTAB.size(); p++) {


ss_file.write(SSTAB.get(p) + "\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

EAVL &X,&Y,&2, &P-123

LCL &M

LCL&N

&M SET O

&N SET &NEW

AIF (&Y EQ &X).ONLY

MOVER AREG,&X

SUB AREG,&Y

ADD AREG, &Z

AGO OVER

ONLY MOVER AREG,&Z

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

7 SUB AREG P 2 8 ADD AREG P 3

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;

public class PassTwo {

ArrayList<mntf> mnt = new


ArrayList<>();
ArrayList<String>mdt = new
ArrayList<>();
HashMap<String,
ArrayList<String>> pntab = new
HashMap<>();
ArrayList<String>kptab = new
ArrayList<>();
PassOne p;
public PassTwo() throws Exception {
p = new PassOne();
p.PassOne();
mnt = p.mnt;
mdt = p.mdt;
pntab = p.pntab;
kptab = p.kptab;

}
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));

}
}
}
}
}

public int getIndexMnt(String mn){


int temp=0; while(!
mnt.get(temp).getName().e
quals(mn)){
temp++;
}
return temp;
}
public static void main(String[] args)
throws Exception {
PassTwo p = new PassTwo();
p.passtwo();
}

}
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

PNTAB SSNTAB SSTAB


X Y ONLY 10

Z P OVER 11
PRACTICAL 2(B)
OUTPUT FILES :
APTAB:
11

12

13

123

EVTAB:
0

PROGRAM WITH MACRO EXPANSION:


MOVER AREG 11

SUB AREG 12

ADD AREG 13

MOVER AREG 13
PRACTICAL 03
FIRST COME FIRST SERVE PROGRAM
import java.util.*;

public class fcfs {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("enter no of process: ");
int n = sc.nextInt();
int pid[] = new int[n]; // process ids
int ar[] = new int[n]; // arrival times
int bt[] = new int[n]; // burst or execution times
int ct[] = new int[n]; // completion times
int ta[] = new int[n]; // turn around times
int wt[] = new int[n]; // waiting times
int temp;
float avgwt=0,avgta=0;

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


{
System.out.println("enter process " + (i+1) + " arrival time: ");
ar[i] = sc.nextInt();
System.out.println("enter process " + (i+1) + " brust time: ");
bt[i] = sc.nextInt();
pid[i] = i+1;
}

//sorting according to arrival times


for(int i = 0 ; i <n; i++)
{
for(int j=0; j < n-(i+1) ; j++)
{
if( ar[j] > ar[j+1] ){
temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
temp = bt[j];
bt[j] = bt[j+1];
bt[j+1] = temp;
temp = pid[j];
pid[j] = pid[j+1];
pid[j+1] = temp: }
} }
// finding completion times
for(int i = 0 ; i < n; i++) {
if( i == 0)
{
ct[i] = ar[i] + bt[i];}
else {
if( ar[i] > ct[i-1])
{
ct[i] = ar[i] + bt[i]; }
else
ct[i] = ct[i-1] + bt[i]; }
ta[i] = ct[i] - ar[i] ; // turnaround time= completion time- arrival time
wt[i] = ta[i] - bt[i] ; // waiting time= turnaround time- burst time
avgwt += wt[i] ; // total waiting time
avgta += ta[i] ; // total turnaround time }
System.out.println("\npid arrival brust complete turn waiting");
for(int i = 0 ; i< n; i++)
{
System.out.println(pid[i] + " \t " + ar[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + ta[i] + "\t" + wt[i] ) ;}
sc.close();
System.out.println("\naverage waiting time: "+ (avgwt/n)); // printing average waiting time.
System.out.println("average turnaround time:"+(avgta/n)); // printing average turnaround time.
}}

OUTPUT :
SHORTEST JOB FIRST PROGRAM
import java.util.*;

public class shortestjob {


public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println ("enter no of process:");
int n = sc.nextInt();
int pid[] = new int[n];
int at[] = new int[n]; // at means arrival time
int bt[] = new int[n]; // bt means burst time
int ct[] = new int[n]; // ct means complete time
int ta[] = new int[n]; // ta means turn around time
int wt[] = new int[n]; //wt means waiting time
int f[] = new int[n]; // f means it is flag it checks process is completed or not
int st=0, tot=0;
float avgwt=0, avgta=0;

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;

public class priority {

public static void main(String[] args) {

System.out.println("*** Priority Scheduling ***");

System.out.print("Enter Number of Process: ");


Scanner sc = new Scanner(System.in);
int numberOfProcess = sc.nextInt();
String process[] = new String[numberOfProcess];

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
{

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


{

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;

public class optimalpage {

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


{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames, pointer = 0, hit = 0, fault = 0,ref_len;
boolean isFull = false;
int buffer[];
int reference[];
int mem_layout[][];

System.out.println("Please enter the number of Frames: ");


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

System.out.println("Please enter the length of the Reference string: ");


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

reference = new int[ref_len];


mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for(int j = 0; j < frames; j++)
buffer[j] = -1;

System.out.println("Please enter the reference string: ");


for(int i = 0; i < ref_len; i++)
{
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for(int i = 0; i < ref_len; i++)
{
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j] == reference[i])
{
search = j;
hit++;
break;
}
}
if(search == -1)
{
if(isFull)
{
int index[] = new int[frames];
boolean index_flag[] = new boolean[frames];
for(int j = i + 1; j < ref_len; j++)
{
for(int k = 0; k < frames; k++)
{
if((reference[j] == buffer[k]) && (index_flag[k] == false))
{
index[k] = j;
index_flag[k] = true;
break;
}
}
}
int max = index[0];
pointer = 0;
if(max == 0)
max = 200;
for(int j = 0; j < frames; j++)
{
if(index[j] == 0)
index[j] = 200;
if(index[j] > max)
{
max = index[j];
pointer = j;
}
}
}
buffer[pointer] = reference[i];
fault++;
if(!isFull)
{
pointer++;
if(pointer == frames)
{
pointer = 0;
isFull = true;
}
}
}
for(int j = 0; j < frames; j++) mem_layout[i]
[j] = buffer[j];
}

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


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

System.out.println("The number of Hits: " + hit);


System.out.println("Hit Ratio: " + (float)((float)hit/ref_len));
System.out.println("The number of Faults: " + fault);
}

}
OUTPUT :
LEAST RECENTLY USED PAGE REPLACEMENT PROGRAM

import java.io.*;
import java.util.*;

public class LRUpage {

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


{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames,pointer = 0, hit = 0, fault = 0,ref_len;
Boolean isFull = false;
int buffer[];
ArrayList<Integer> stack = new ArrayList<Integer>();
int reference[];
int mem_layout[][];

System.out.println("Please enter the number of Frames: ");


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

System.out.println("Please enter the length of the Reference string: ");


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

reference = new int[ref_len];


mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for(int j = 0; j < frames; j++)
buffer[j] = -1;

System.out.println("Please enter the reference string: ");


for(int i = 0; i < ref_len; i++)
{
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for(int i = 0; i < ref_len; i++)
{
if(stack.contains(reference[i]))
{
stack.remove(stack.indexOf(reference[i]));
}
stack.add(reference[i]);
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j] == reference[i])
{
search = j;
hit++;
break;
}
}
if(search == -1)
{
if(isFull)
{
int min_loc = ref_len;
for(int j = 0; j < frames; 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 == frames)
{
pointer = 0;
isFull = true;
}
}
for(int j = 0; j < frames; j++) mem_layout[i]
[j] = buffer[j];
}

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


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

System.out.println("The number of Hits: " + hit);


System.out.println("Hit Ratio: " + (float)((float)hit/ref_len));
System.out.println("The number of Faults: " + fault);
}

}
OUTPUT :

You might also like