[go: up one dir, main page]

0% found this document useful (0 votes)
27 views57 pages

SystemSoftware Lab ManualProf - Malik

The document is a laboratory manual for a System Software lab course. It contains descriptions of 6 programming assignments involving CPU scheduling algorithms like FCFS and SJF, file allocation strategies like sequential and linked allocation, and disk scheduling algorithms like FCFS and SCAN. For each program, it provides the aim, algorithm, sample code, input/output and result.

Uploaded by

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

SystemSoftware Lab ManualProf - Malik

The document is a laboratory manual for a System Software lab course. It contains descriptions of 6 programming assignments involving CPU scheduling algorithms like FCFS and SJF, file allocation strategies like sequential and linked allocation, and disk scheduling algorithms like FCFS and SCAN. For each program, it provides the aim, algorithm, sample code, input/output and result.

Uploaded by

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

CS331 SYSTEM SOFTWARE LAB Department of CSE

CS331 - SYSTEM SOFTWARE LABORATORY


LABORATORY MANUAL
FOR FIFTH SEMESTER
B.TECH-CSE ACADEMIC YEAR
2019-2020 (ODD)

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 1


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:1
FCFS- CPU SCHEDULING ALGORITHM
Aim:
Simulate FCFS non-preemptive CPU scheduling algorithm to find turnaround time and
waiting time.

Note:
It is the simplest of all the scheduling Algorithms. Key concept of this Algorithm is
“allocate the CPU in the order in which the processes arrive”. It assumes that ready queue
is managed as FIFO (First in first out).This Algorithm is non-preemptive.

Algorithm
1. Start
2. Declare the array size
3. Read the number of processes to be inserted
4. Read the Burst times of processes
5. Calculate the waiting time of each process wt[i+1]=bt[i]+wt[i]
6. Calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1]
7. Calculate the average waiting time and average turnaround time.
8. Display the values
9. Stop

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 2


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample Output:

Input
Enter no of processes 3
Enter burst time 12
8
20

Output
bt wt tt
12 0 12
8 12 20
20 20 40

aw=10.666670
at=24.00000

Result:
Thus the process FCFS was executed and verified successfully.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 3


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:2

SJF NON-PREEMPTIVE CPU SCHEDULING ALGORITHM


Aim:
Simulate SJF non-preemptive CPU scheduling algorithm to find turnaround time and
waiting time.

Note:
The key concept of this Algorithm is: “CPU is allocated to the process with least CPU
burst time.” Amongst the processes in the ready queue, CPU is always assigned to the
process with the least CPU burst requirement. If there are two processes with the CPU
burst, the one which arrived first, will be taken up first by the CPU. This SJF Algorithm
can be either preemptive or non-preemptive. Preemptive SJF scheduling is sometimes
called Shortest-Remaining-Time-First (SRTF) scheduling. SJF is an optimal Algorithm,
as it gives the minimum average waiting time.

Algorithm
1. Start
2. Declare the array size
3. Read the number of processes to be inserted
4. Read the Burst times of processes
5. Sort Burst times in ascending order and process with shortest burst time is first
executed.
6. Calculate the waiting time of each process wt[i+1]=bt[i]+wt[i]
7. Calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1]

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 4


CS331 SYSTEM SOFTWARE LAB Department of CSE

8. Calculate the average waiting time and average turnaround time.


9. Display the values
10. Stop

Source code:

Sample Output

Input:
Enter no of processes 3
Enter burst time 12
8

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 5


CS331 SYSTEM SOFTWARE LAB Department of CSE

20

Output:
bt wt tt
12 8 20
8 0 8
20 20 40

aw=9.33
at=22.64

Result:
Thus the process SJF was executed and verified successfully.

Program No:3

SEQUENTIAL FILE ALLOCATION

Aim:
To simulate Sequential File Allocation strategy.

Algorithm:
1. Start
2. Read the number of files
3. For each file, read the number of blocks required and the starting block of the file.
4. Allocate the blocks sequentially to the file from the starting block.
5. Display the file name, starting block, and the blocks occupied by the file.
6. Stop

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 6


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample output:

Enter no.of files: 2


Enter no. of blocks occupied by file: 1 4
Enter the starting block of file: 1 2
Enter no. of blocks occupied by file: 2 10
Enter the starting block of file: 2 5

Filename Startblock length

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 7


CS331 SYSTEM SOFTWARE LAB Department of CSE

1 2 4
2 5 10

Result:
Thus the process sequential file allocation strategy was executed and verified
successfully.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 8


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:4

LINKED FILE ALLOCATION


Aim:
To simulate the linked file allocation strategy.

Note:
Linked allocation of disk space overcomes all the problems of contiguous allocation. In
linked allocation each file is a linked list of disk blocks where the disk blocks may be
scattered anywhere on the disk. The directory contains a pointer to the first and last
blocks of the file.
Disadvantages: Space required maintaining pointers.

Algorithm
1. Start
2. Read the number of files
3. For each file, read file name, starting block, number of blocks and block numbers
of file.
4. Start from the starting block and link each block of the file to the next block in a
linked list fashion.
5. Display the file name, starting block, size of the file, and the blocks occupied by
the file.
6. Stop

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 9


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample Output:

Enter no. of files:2


Enter file name:KMCT
Enter starting block:20
Enter no.of blocks:6
Enter block numbers: 4 12 15 45 32 25
Enter file name:CSE
Enter starting block:12
Enter no.of blocks:5
Enter block numbers:6 5 4 3 2

File start size block


KMCT 20 6 20--->4--->12--->15--->45--->32--->25
CSE 12 5 12--->6--->5--->4--->3--->2

Result:
Thus the process linked file allocation strategy was executed and verified successfully.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 10


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:5

FCFS DISK SCHEDULING ALGORITHM


Aim:
To write a program to simulate the FCFS disk scheduling Algorithm

Theory:
It is the simplest form of disk scheduling algorithms. The I/O requests are
served or processes according to their arrival. The request arrives first will be
accessed and served first. Since it follows the order of arrival, it causes the
wild swings from the innermost to the outermost tracks of the disk and vice
versa . The farther the location of the request being serviced by the read/write
head from its current location, the higher the seek time will be.

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 11


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample output:
Enter number of location 8
Enter position of head 53
Enter elements of disk queue 98
183
37
122
14
124
65
67
Movement of total cylinders 640

Result:
Thus the program was successfully executed.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 12


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:6

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 13


CS331 SYSTEM SOFTWARE LAB Department of CSE

SCAN DISK SCHEDULING ALGORITHM

Aim:
To write a program to simulate the SCAN disk Scheduling Algorithm

Note:
Here the disk arm starts at one end of the disk and moves towards the other end,
servicing requests as it reaches each cylinder until it gets to the other end of the disk. At
the other end, the direction of head movement is reversed and servicing continues.

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 14


CS331 SYSTEM SOFTWARE LAB Department of CSE

Result:
Thus the program was successfully executed.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 15


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:7

C-SCAN DISK SCHEDULING ALGORITHM

Aim:
To write a program to simulate the C-SCAN disk scheduling Algorithm

Theory:
Circular SCAN (C-SCAN) scheduling is a variant of SCAN designed to provide a more
uniform wait time. Like SCAN, C-SCAN moves the head from one end of the disk to the
other, servicing requests along the way. When the head reaches the other end, however, it
immediately returns to the beginning of the disk without servicing any requests on the
return trip (Figure 10.7). The C-SCAN scheduling Algorithm essentially treats the
cylinders as a circular list that wraps around from the final cylinder to the first one.

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 16


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 17


CS331 SYSTEM SOFTWARE LAB Department of CSE

Result: Thus the program was successfully executed

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 18


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:8

PRODUCER/CONSUMER PROBLEM USING SEMAPHORE

Aim:
To implement producer/consumer problem using semaphore.

Note:
A producer process produces information that is consumed by a consumer process. For
example, a compiler may produce assembly code that is consumed by an assembler.To
allow producer and consumer processes to run concurrently, we must have available a
buffer of items that can be filled by the producer and emptied by the consumer. This
buffer will reside in a region of memory that is shared by the producer and consumer
processes. A producer can produce one item while the consumer is consuming another
item. The producer and consumer must be synchronized, so that the consumer does not
try to consume an item that has not yet been produced.

Algorithm:
1. Declare variable for producer & consumer as pthread-t-tid produce tid consume.
2. Declare a structure to add items, semaphore variable set as struct.
3. Read number the items to be produced and consumed.
4. Declare and define semaphore function for creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer and consumer.
8. Stop the execution.

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 19


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample output:

1. Produce 2. Consume 3. Exit


Enter your choice: 2
Buffer is Empty

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 100

1. Produce 2. Consume 3. Exit


Enter your choice: 2
The consumed value is 100

1. Produce 2. Consume 3. Exit


Enter your choice: 3

Result:
Thus the producer consumer program was executed and verified successfully.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 20


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:9
DINING PHILOSOPHER’S PROBLEM

Aim:
To simulate the working of the dining philosopher’s problem

Note:
There are some Philosophers whose work is just thinking and eating. Let there are 5 (for
example) philosophers. They sat at a round table for dinner. To complete dinner each must
need two Forks (spoons). But there are only 5 Forks available (Forks always equal to no. of

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 21


CS331 SYSTEM SOFTWARE LAB Department of CSE

Philosophers) on table. They take in such a manner that, first take left Fork and next right
Fork. But problem is they try to take at same time. Since they are trying at same time, Fork 1,
2, 3, 4, 5 taken by Philosopher 1, 2, 3, 4, 5 respectively (since they are left side of each). And
each one tries to ta ke right side Fork. But no one found available Fork. And also that each
one thinks that someone will release the Fork and then I can eat. This continuous waiting
leads to Dead Locksituation.

Algorithm:
1. Define the number ofphilosophers
2. Declare one thread perphilosopher
3. Declare one semaphore (represent chopsticks) perphilosopher
4. When a philosopher ishungry
1. See if chopsticks on both sides arefree
2. Acquire both chopsticksor
3. Eat
4. restore thechopsticks
5. If chopsticks aren’tfree
5. Wait till they areavailable

Source code

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>

#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N

sem_tmutex;
sem_t S[N];

void * philospher(void *num);


void take_fork(int);
void put_fork(int);
void test(int);

int state[N];
intphil_num[N]={0,1,2,3,4};

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 22


CS331 SYSTEM SOFTWARE LAB Department of CSE

int main()
{
int i;
pthread_tthread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinkingn",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}

void *philospher(void *num)


{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}

void take_fork(intph_num)
{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungryn",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}

void test(intph_num)
{
if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] !=
EATING)
{
state[ph_num] = EATING;
sleep(2);

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 23


CS331 SYSTEM SOFTWARE LAB Department of CSE

printf("Philosopher %d takes fork %d and %dn",ph_num+1,LEFT+1,ph_num+1);


printf("Philosopher %d is Eatingn",ph_num+1);
sem_post(&S[ph_num]);
}
}

void put_fork(intph_num)
{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d
downn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinkingn",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 24


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:10
BANKERS ALGORITHM
AIM
Program to implement Banker`s algorithm.

IMPLEMENTATION DETAILS
The Banker&#39;s algorithm is run by the operating system whenever a process requests
resources. The algorithm avoids deadlock by denying or postponing the request if it
determines
that accepting the request could put the system in an unsafe state (one where deadlock
could
occur). When a new process enters a system, it must declare the maximum number of
instances
of each resource type that it may ever claim; clearly, that number may not exceed the
total
number of resources in the system. Also, when a process gets all its requested resources it
must
return them in a finite amount of time.

ALGORITHM
Step 1: Start
Step 2: Read the number of available resource to r
Step 3: for i=1 to r
Step 4: Read the available resource to rsrc[i]
Step 5: Read the number of process to p
Step 6: for i=0 to p
Step 7: for j=1 to r do step 8
Step 8: Read maximum claim and allocation details to claim [i][j] and alloc[i][j]
respectively
Step 9: End for loop

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 25


CS331 SYSTEM SOFTWARE LAB Department of CSE

Step 10: for i=0 to p


Step 11: for j=1 to r do step 12
Step 12: print claim[i][j] and alloc[i][j]
Step 13: End for loop
Step 14: for j=1 to r do step 15
Step 15: Set avail[j] =0
Step 16: for j=1 to r
Step 17: for i=0 to p do step 18
Step 18: avail[j] = avail[j] + alloc[i][j]
Step 19: avail[j] = rsrc[j] – avail[j]
Step 20: End for loop
Step 21: for i=0 to p
Step 22: for j=1 to r do step 23
Step 23: req[i][j] = claim[i][j] – alloc[i][j]
Step 24: End for loop
Step 25: for i=0 to p
Step 26: set flag[i] = 0
Step 27: for i=0 to p
Step 28: for j=1 for r do step 29 to 36
Step 29: if claim[i][j] &gt; rsrc[j] || alloc[i][j] &gt; rsrc[j] do step 30 to 36
Step 30: for l=1 for do step 31
Step 31: print avail[l]
Step 32: print process exited due to mismatch in available and claimed resources
Step 33: flag[i]++
Step 34: for j=1 to r do step 35
Step 35: avail[j] = avail[j] + alloc[i][j]
Step 36: set sf =0
Step 37: end for loop
Step 38: while full ≠ 1,repeat step 39 to60
Step 39: for i =0 to p do step 54
Step 40: while(flag[i]=0) repeat step 41 to 53
Step 41: for j=1 to r do steps up to 43
Step 42: if req[i][j] &gt; avail[j]
Step 43: i++ break
Step 44: if(j=r+1) do step 45 to 52
Step 45: for j=1 to r
Step 46: print avail[j]
Step 47: print process i is executed
Step 48: flag [i]++
Step 49: set safe[k++]=1
Step 50: for j=1 to r
Step 51: avail[j] = avail[j] + alloc[i][j]
Step 52: endif
Step 53: end while loop
Step 54: end for loop
Step 55: for i=0 to p

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 26


CS331 SYSTEM SOFTWARE LAB Department of CSE

Step 56: if flag[i]=0


Step 57: break
Step 58: if i==p
Step 59: set full=1
Step 60: end while loop
Step 61: for j=1 to r do step 62
Step 62: print avail[j]
Step 63: if sf=0
Step 64: print system to unsafe state
Step 65: else
Step 66: print system in safe state
Step 67: for i=0 to k
Step 68: print process[i]
Step 69: endif
Step 70: Stop

PROGRAM
#include<stdio.h>
void main()
{
intpr,re,i,j,k=0,count1=0,count2=0,insta;
printf("\n \t enter no of process\n");
printf("\t\t");
scanf("%d",&pr);
printf("\n enter the no of resources");
printf("\t\t");
scanf("%d",&re);
int avail[re],max[pr][re],allot[pr][re],need[pr][re],completed[pr];
for(i=0;i<pr;i++)
completed[i]=0;
printf("\n enter no of instances\n");
for(i=0;i<re;i++)
{
printf("\t\t");
scanf("%d",&insta);
avail[i]=insta;
}
printf("\n\t enter max no of instances of resources thet a process need:\n");
for(i=0;i<pr;i++)
{
printf("\n \t for p[%d]",i);
for(j=0;j<re;j++)
{
printf("\t");
scanf("%d",&insta);

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 27


CS331 SYSTEM SOFTWARE LAB Department of CSE

max[i][j]=insta;
}
}
printf("\n \t enter no of instances already located to process of a resource \n");
for(i=0;i<pr;i++)
{
printf("\n \t for p[%d] \t",i);
for(j=0;j<re;j++)
{
printf("\t\t");
scanf("%d",&insta);
allot[i][j]=insta;
need[i][j]=max[i][j]-allot[i][j];
}
}
printf("\n \t state sequence is:\t");
while(count1!=pr)
{
count2=count1;
for(i=0;i<pr;i++)
{
for(j=0;j<re;j++)
{
if(need[i][j]<=avail[j])
{
k++;
}
}
if(k==re && completed[i]==0)
{
printf("p[%d]\t",i);
completed[i]=1;
for(j=0;j<re;j++)
{
avail[j]+allot[i][j];
}
count1++;
}
k=0;
}
if(count1==count2)
{
printf("\t \t stop after this deadlock \n");
break;
}
}

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 28


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 29


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:11
SINGLE LEVELDIRECTORY

Aim:
To write a C program to implement File Organization concept using the
technique Single leveldirectory.

Note:
It is the simplest of all directory structures, in this the directory system having only one
directory, it consisting of the all files. Sometimes it is said to be root directory. The
following Fig. shows single level directory that contains four files (A, B, C, D). It has the
simplicity and ability to locate files quickly. It is not used in the multi-user system; it is
used on small embeddedsystem.

Algorithm:
Step 1: Start the Program
Step 2: Initialize values gd=DETECT,gm,count,i,j,mid,cir_x.
Step 3: Initialize graph function
Step 4: Set back ground color with setbkcolor();
Step 5: Read number of files in variable count.
Step 6: check i<count; mid=640/count;
Step 7: Stop the execution

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 30


CS331 SYSTEM SOFTWARE LAB Department of CSE

Result:

Thus the program was successfully executed

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 31


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:12
PASS ONE OF A TWO PASS ASSEMBLER

Aim:
To write a program to implement pass one of a two pass assembler.

Note:
An assembler is a translator, that translates an assembler program into a
conventional machine language program. Basically, the assembler goes
through the program one line at a time and generates machine code for that
instruction. Then the assembler procedes to the next instruction.In this way,
the entire machine code program is created. For most instructions this
process works fine, for example for instructions that only reference registers,
the assembler can compute the machine code easily, since the assembler
knows where the registersare.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 32


CS331 SYSTEM SOFTWARE LAB Department of CSE

Algorithm:
1 begin
2 read first inputline;
3 if OPCODE = 'START'then
begin
i. save #[OPERAND] as startingaddress
ii. initialized LOCCTR to startingaddress
iii. write line to intermediatefile
iv. r
e
a
d

n
e
x
t

i
n
p
u
t
l
i
n
e

e
n
d

{
i
f
S
T
A
R
T

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 33


CS331 SYSTEM SOFTWARE LAB Department of CSE

}
4 e
lse
initia
lized
LOC
CTR
to 0
5
whil
e
OPC
ODE
!=
'EN
D'
do
begi
n
a. i. if this is not a comment line then
ii. begin
iii. if there is a
symbol in the
LABEL fieldthen
begin
1. search SYMTAB forLABEL
2. if foundthen
3. set error flag (duplicatesymbol)
4. else
5. insert
(LABEL,
LOCCTR) into
SYMTAB end
{ifsymbol}
iv. search OPTAB forOPCODE
v. if found then add 3 {instruction length} toLOCCTR
vi. else if OPCODE = 'WORD' then add 3 toLOCCTR
vii. else if OPCODE = 'RESW' thenadd 3 * #[OPERAND] toLOCCTR
viii. else if OPCODE = 'RESB' then add #[OPERAND] toLOCCTR
ix. e
lse if
OPCOD
E=
x.

xi. '

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 34


CS331 SYSTEM SOFTWARE LAB Department of CSE

BYTE'
then
begin
1. find length of constant inbytes
2. add length toLOCCTRend {if BYTE}
xii. else set
error flag (invalid
operationcode) end {if
not acomment}

8 end {while notEND}


9 write last line to intermediatefile
10 save (LOCCTR -
starting address) as
programlength 11end

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 35


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 36


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 37


CS331 SYSTEM SOFTWARE LAB Department of CSE

ouput.txt
2003 ** STA ALPHA

2006 ** LDCH CHARZ


2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C'Z'
2019 C1 RESB1
2020 ** END**

Result:
Thus pass one of two passes assembler is implemented and the result is verified successfully

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 38


CS331 SYSTEM SOFTWARE LAB Department of CSE

PROGRAM No:13
PASS TWO OF A TWO PASS ASSEMBLER
Aim:
To write a program to implement pass two of a two pass assembler.

Note:
.An assembler is a translator, that translates an assembler program into a conventional
machine language program. Basically, the assembler goes through the program one line
at a time and generates machine code for that instruction. Then the assembler procedes to
the next instruction. In this way, the entire machine code program is created. For most
instructions this process works fine, for example for instructions that only reference
registers, the assembler can compute the machine code easily, since the assembler knows
where the registers are

Algorithm:
1 begin
2 read first input file {from intermediate file} 3 if OPCODE = 'START' then
4 begin
a) write listing line
b) read next input line 5 end {if START}
6 write header record to object program 7 initialized first Text record
8 while OPCODE != 'END' do 9 begin
a) if this is not a comment line then
i) begin
ii) search OPTAB for OPCODE
iii) if found then
iv) begin
(1) if there is a symbol in OPERAND field then
(2) begin
(a) search SYMTAB for OPERAND
(b) if found then
(c) store symbol value as operand address
(d) else
(e) begin
(f) store 0 as operand address
(g) set error flag (undefined symbol)
(h) end
(3) end {if symbol}
(4) else
(5) store 0 as operand address
(6) assemble the object code instruction
i) end {if opcode found}

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 39


CS331 SYSTEM SOFTWARE LAB Department of CSE

ii) else if OPCODE = 'BYTE' or 'WORD' then


iii) convert constant to object code
iv) if object code not fit into the current Text record then
v) begin
(7) write Text record to object program
(8) initialized new Text record
vi) end
vii) add object code to Text record
viii) end {if not comment}
b) write listing line read next input line
10 end {while not END}
11 write last Text record to object program 12 write End record to object program
13 write last listing line 14 end

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 40


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 41


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample output:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 42


CS331 SYSTEM SOFTWARE LAB Department of CSE

Result:
Thus the program for two pass assembler is implemented and the output is verified accordingly.

SINGLE PASS ASSEMBLER


a single pass assembler.
Program No:14
SINGLE PASS ASSEMBLER

Aim
To write a program to implement a single pass assembler.

Algorithm
1 Begin
2 Read first input line
3 if OPCODE=‟START‟ then
a. save #[operand] as starting address
b. initialize LOCCTr as starting address
c. read next input line end
4 else initialize LOCCTR to 0
5 while OPCODE != „END‟ do
d. if there is not a comment line then
e. if there is a symbol in the LABEL field then
i. search SYMTAB for LABEL
ii. if found then
1. if symbol value as null
2. set symbol value as LOCCTR and search the linked list with the
corresponding operand
3. PTR addresses and generate operand addresses as corresponding
symbol values
4. set symbol value as LOCCTR in symbol table and delete the linked
list
iii. end
iv. else insert (LABEL,LOCCTR) into SYMTAB
v. end
6search OPTAB for OPCODE
7 if found thensearch SYMTAB for OPERAND address
8 if found then
f. if symbol value not equal to null then
i) store symbol value as operand address

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 43


CS331 SYSTEM SOFTWARE LAB Department of CSE

else insert at the end of the linked list with a node with address as LOCCTR
9 else insert (symbol name, null) add 3 to LOCCTR.
10 elseif OPCODE=‟WORD‟ then
add 3 to LOCCTR & convert comment to object code
11 elseif OPCODE = „RESW‟ then add 3 #[OPERND] to LOCCTR
12 elseif OPCODE = „RESB‟ then add #[OPERND] to LOCCTR
13 elseif OPCODE = „BYTE‟ then

g. find length of the constant in bytes


h. add length to LOCCTR convert constant to object code 14 if object code will not
fit into current text record then
i. write text record to object program
j. initialize new text record
o. add object code to text record 15 write listing line
16 read next input line
17 write last text record to object program 18 write end record to object program
19 write last listing line
20 End

Source Code

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 44


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 45


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 46


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 47


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 48


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample output:

Result:
Thus the program for single pass assembler is implemented and the output is verified
accordingly.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 49


CS331 SYSTEM SOFTWARE LAB Department of CSE

Program No:15
TWO PASS MACRO PROCESSOR
Aim:
Write a program to implement two pass macro processor

Theory:
Ageneral-purpose macro processor or general purpose preprocessor is a macro processor that is not
tied to or integrated with a particular language or piece of software. A macro processor is a program
that copies a stream of text from one place to another, making a systematic set of replacements as it
does so. Macro processors are often embedded in other programs, such as assemblers and
compilers. Sometimes they are standalone programs that can be used to process any kind of text.
Macro processors have been used for language expansion (defining new language constructs that can
be expressed in terms of existing language components), for systematic text replacements that
require decision making, and for text reformatting (e.g. conditional extraction of material from an
HTML file).

Algorithm:
1 Start the macro processorprogram.
2 Include the necessary
header files and variable. 3
Open the threefiles
a.f1=macin.dat with readprivilege
b. f2=macout.dat with writeprivilege
c. f3= deftab.dat with writeprivilege
4 Get the variable form f1 file macin.dat for
label,opcode,operand 5 Read the variable until
the opcode is not is equal to zero
6 Then check if the opcode is equal to Macro if Macro. Then
Copymacroname=label
a. Get the variable label, opcode,operand
b. In these if condition perform the while loop until opcode is not equal toMEND
c. Copy thevariable
d. close while loop and ifcondition
e. else if opcode is
equal to macro name
Perform the for loop
from 0 tolength
7 Finally terminate theprogram.

Source code:

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 50


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 51


CS331 SYSTEM SOFTWARE LAB Department of CSE

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 52


CS331 SYSTEM SOFTWARE LAB Department of CSE

Sample output:
INPUT FILE:
macin.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** call m1
** call m2

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 53


CS331 SYSTEM SOFTWARE LAB Department of CSE

** add a,b

OUPUT FILE:
No. of macros=2
Enter the text filename outmac
macin
outmac.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** move a,b
** lda b
** add a,b

Results:
Thus the macro processor is implemented and the result is verified successfully.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 54


CS331 SYSTEM SOFTWARE
ARE LAB Department of CSE

Program No:16
ABSOLUTE LOADER

Aim
To write a C program to implement an absolute loader.
Algorithm:
1. Start theprogram
2. Assign the requiredvariable
3. Open the files
fp1=fopen("input2.dat","r")
;
fp2=fopen("out2.dat","w");
4. Read the content. Using while loop perform the loop until character is not equal toE.
5. Then compare whether the character is equal toH
6. If H then get the starting address, length andinput
7. Else if the character is T then store the string as the three address in the output file with
input[0],inuput[1] foraddress
input[2],inuput[3] for address+1
input[4],inuput[5] for address+2
8. Else if it is not H or T then perform the following fprintf
fprintfin fp2 for output file for ,
input[0],inuput[1] foraddress
input[2],inuput[3] for address+1
input[4],inuput[5]
inuput[5] for address+2
9. Increment the address value by3.
10. Read the next input string and repeat from step4.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 55


CS331 SYSTEM SOFTWARE LAB Department of CSE

11. Finally terminate theprogram

Input & Out


Input File: INPUT2.DAT
H 1000 232
T 1000 142033 483039 102036
T 2000 298300 230000 282030 302015
E
Output File: OUT2.DAT
1000 14
1001 20
1002 33
1003 48
1004 30

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 56


CS331 SYSTEM SOFTWARE LAB Department of CSE

1005 39
1006 10
1007 20
1008 36
2000 29
2001 83
2002 00
2003 23
2004 00
2005 00
2006 28
2007 20
2008 30
2009 30
2010 20
2011 15

Result:
Thus the absolute loader is implemented and the result is verified successfully.

MUDRA INSTITUTE :MOHAMMED MALIK CK Page 57

You might also like