Syllabus
Syllabus
Syllabus
CSL331 System Software and Microprocesors Lab
Objectives
o To familiarize students with System Software , Operating System Programs and 8086
Microprcoessor and 8085 Microcontroller Kit
List of Exercises/Experiments
MASM Programs
14.BASIC ARITHMETIC OPERATIONS
15.STRING DISPLAY
16.STRING CONCATENATENATION
17.SORTING
18.SEARCHING
8086 TRAINER KIT Programs
19.BASIC ARITHEMATIC AND LOGICAL OPERATIONS
20.SORTING-ASCENDING
21.CODE CONVERSIONS
8051 TRAINER KIT Prgrams
22.BASIC ARITHEMATIC AND LOGICAL OPERATIONS
23. CODE CONVERSIONS
Department of Computer Science and Engineering
1
CSL331 System Software and Microprocesors Lab
INDEX
MASM PROGRAMS
Pgm.No.1
CPU SCHEDULING
AIM
Similate the following non pre-emptive CPU scheduling algorithms to find turnaround time and
waiting tme.
a). FCFS
b). SJF
c). Priority
d). Round Robin (Pre-emptive)
ALGORITHM
PROGRAM
CPU SCHEDULING - FIRST COME FIRST SERVE
****************************************************************************
****
#include <stdio.h>
#include <conio.h>
int main()
{
int bt[10],wt[10],tt[10],n,i;
float twt=0,ttt=0;
printf("Enter the number of process:");
scanf("%d",&n);
printf("Enter burst time of each process\n");
for(i=0;i<n;i++)
{
printf("process %d:",i+1);
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
{
if(i==0)
{
wt[i]=0;
}
else {
wt[i]=bt[i-1]+wt[i-1];
}
twt=twt+wt[i];
}
for(i=0;i<n;i++)
{
tt[i]=wt[i]+bt[i];
ttt=ttt+tt[i];
}
printf("\n\tProcess \tBurst time \tWaiting Time \tTurnaroung Time");
for(i=0;i<n;i++)
{
printf("\n\t%d \t\t%d \t\t%d \t\t%d\n",i+1,bt[i],wt[i],tt[i]);
}
printf("Total Waiting Time=%.2f\n",twt);
printf("Total Turnaround Time=%.2f\n",ttt);
printf("Average Waiting Time=%.2f\n",twt/n);
printf("Average Turnaround Time=%.2f\n",ttt/n);
getch();
return 0;
}
****************************************************************************
*******
OUTPUT
Enter the number of process:4
Enter burst time of each process
process 1:5
process 2:4
process 3:3
process 4:2
2 4 5 9
3 3 9 12
4 2 12 14
Total Waiting Time=26.00
Total Turnaround Time=40.00
Average Waiting Time=6.50
Average Turnaround Time=10.00
****************************************************************************
*******
ALGORITHM
1. As the name suggests, the shortest job first algorithm is an algorithm which executes the
process whose burst time is least and has arrived before the current time. Therefore, in order to
find the process which needs to be executed, sort all the processes from the given set of
processes according to their arrival time. This ensures that the process with the shortest burst
time which has arrived first is executed first.
2. Instead of finding the minimum burst time process among all the arrived processes by iterating
the
whole struct array, the range minimum of the burst time of all the arrived processes upto the
current time is calculated using segment tree.
3. After selecting a process which needs to be executed, the completion time, turn around time
and waiting time is calculated by using arrival time and burst time of the process. The formulae
to calculate the respective times are:
o Completion Time: Time at which process completes its execution.
● Turn Around Time: Time Difference between completion time and arrival time.
● Waiting Time(W.T): Time Difference between turn around time and burst time.
1. After calculating, the respective times are updated in the array and the burst time of the
executed process is set to infinity in the segment tree base array so that it is not considered as
the minimum burst time in the further queries.
PROGRAM
CPU SCHEDULING - SHORTEST JOB FIRST
****************************************************************************
*******
#include <stdio.h>
#include <stdio.h>
int main()
{
int bt[10],wt[10],tt[10],n,i,j,p[10],temp;
float twt=0,ttt=0;
printf("Enter the number of process:");
scanf("%d",&n);
printf("Enter burst time of the process:\n");
for(i=0;i<n;i++)
{
printf("process %d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
wt[i]=0;
}
else {
wt[i]=bt[i-1]+wt[i-1];
}
twt=twt+wt[i];
}
for(i=0;i<n;i++)
{
tt[i]=wt[i]+bt[i];
ttt=ttt+tt[i];
}
printf("\n\tProcess \tBurst time \tWaiting Time \tTurnaroung Time");
for(i=0;i<n;i++)
{
printf("\n\t%d \t\t%d \t\t%d \t\t%d\n",p[i],bt[i],wt[i],tt[i]);
}
printf("Total Waiting Time=%.2f\n",twt);
printf("Total Turnaround Time=%.2f\n",ttt);
printf("Average Waiting Time=%.2f\n",twt/n);
printf("Average Turnaround Time=%.2f\n",ttt/n);
getch();
return 0;
}
****************************************************************************
*******
OUTPUT
Enter the number of process:4
Enter burst time of the process:
process 1:5
process 2:4
process 3:3
process 4:2
3 3 2 5
2 4 5 9
1 5 9 14
Total Waiting Time=16.00
Total Turnaround Time=30.00
Average Waiting Time=4.00
Average Turnaround Time=7.50
****************************************************************************
*******
ALGORITHM
PROGRAM
{
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
wt[i]=0;
}
else {
wt[i]=bt[i-1]+wt[i-1];
}
twt=twt+wt[i];
}
for(i=0;i<n;i++)
{
tt[i]=wt[i]+bt[i];
ttt=ttt+tt[i];
}
printf("\n\tProcess \tBurst time \tWaiting Time \tTurnaroung Time");
for(i=0;i<n;i++)
{
printf("\n\t%d \t\t%d \t\t%d \t\t%d\n",p[i],bt[i],wt[i],tt[i]);
}
printf("Total Waiting Time=%.2f\n",twt);
printf("Total Turnaround Time=%.2f\n",ttt);
printf("Average Waiting Time=%.2f\n",twt/n);
printf("Average Turnaround Time=%.2f\n",ttt/n);
getch();
return 0;
}
****************************************************************************
*******
OUTPUT
Enter the number of process:4
Enter burst time and priority of the process:
process 1:5 4
process 2:4 3
process 3:3 2
process 4:2 1
3 3 2 5
2 4 5 9
1 5 9 14
Total Waiting Time=16.00
Total Turnaround Time=30.00
Average Waiting Time=4.00
Average Turnaround Time=7.50
ALGORITHM
****************************************************************************
*******
#include <stdio.h>
#include <conio.h>
int main()
{
int bt[10],wt[10],tat[10],temp[10],sum=0,count=0,q,tn,n,i;
float twt=0,ttt=0;
printf("Enter the number of process:");
scanf("%d",&n);
tn=n;
printf("Enter burst time of the process:\n");
for(i=0;i<n;i++)
{
printf("process %d:",i+1);
scanf("%d",&bt[i]);
temp[i]=bt[i];
}
printf("Enter the time slice or time quantum:");
scanf("%d",&q);
printf("\nProcess No\t\tBurst Time \tWaiting Time \tTurnaround Time");
for(i=0;tn!=0;)
{
if(temp[i]<=q && temp[i]>0)
{
sum=sum + temp[i];
temp[i]=0;
count=1;
}
else if(temp[i]>q)
{
temp[i]=temp[i] - q;
sum=sum + q;
}
if(temp[i]==0 && count==1)
{
tn--;
wt[i]=sum-bt[i];
tat[i]=sum;
printf("\nProcess %d\t\t%d\t\t%d\t\t%d",i+1,bt[i],sum-bt[i],sum);
ttt=ttt + tat[i];
twt=twt + wt[i];
count=0;
}
if(i==(n-1))
{
i=0;
}
else
{
i++;
}
}
printf("\n\nTotal Waiting Time=%.2f\n",twt);
printf("\nTotal Turnaround Time=%.2f\n",ttt);
printf("\nAverage Waiting Time=%.2f\n",twt/n);
printf("\nAverage Turnaround Time=%.2f\n",ttt/n);
getch();
return 0;
}
****************************************************************************
*******
OUTPUT
Enter the number of process:4
Enter burst time of the process:
process 1:5
process 2:4
process 3:3
process 4:2
Enter the time slice or time quantum:2
Pgm.No.2
BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
AIM
Implement banker’s algorithm for deadlock avoidance
ALGORITHM
Banker’s algorithm comprises of two algorithms:
1. Safety algorithm
2. Resource request algorithm
Safety Algorithm
A safety algorithm is an algorithm used to find whether or not a system is in its safe state. The algorithm
is as follows:
3. Go to step 2.
When an unfinished process is found, then the resources are allocated and the process is
marked finished. And then, the loop is repeated to check the same for all other processes.
That means if all processes are finished, then the system is in safe state.
This algorithm may require an order of mxn² operations in order to determine whether a state is safe or
not.
It means, we need to find an unfinished process whose needs can be satisfied by the available
resources. If no such process exists, just go to step 4.
Let Requesti be the request vector for the process Pi. If Requesti[j]==k, then process Pi wants k instance
of Resource type Rj.When a request for resources is made by the process Pi, the following are the
actions that will be taken:
1. If Requesti <= Needi, then go to step 2;else raise an error condition, since the process has exceeded
its maximum claim.
2.If Requesti <= Availablei then go to step 3; else Pi must have to wait as resources are not available.
3.Now we will assume that resources are assigned to process Pi and thus perform the following steps:
Available= Available-Requesti;
Allocationi=Allocationi +Requesti;
If the resulting resource allocation state comes out to be safe, then the transaction is completed and,
process Pi is allocated its resources. But in this case, if the new state is unsafe, then Pi waits for
Requesti, and the old resource-allocation state is restored.
PROGRAM
#include<stdio.h>
struct pro{
int all[10],max[10],need[10];
int flag;
};
int i,j,pno,r,nr,id,k=0,safe=0,exec,count=0,wait=0,max_err=0;
struct pro p[10];
int aval[10],seq[10];
void safeState()
{
while(count!=pno){
safe = 0;
for(i=0;i<pno;i++){
if(p[i].flag){
exec = r;
for(j=0;j<r;j++)
{
if(p[i].need[j]>aval[j]){
exec =0;
}
}
if(exec == r){
for(j=0;j<r;j++){
aval[j]+=p[i].all[j];
}
p[i].flag = 0;
seq[k++] = i;
safe = 1;
count++;
}
}
if(!safe)
{
printf("System is in Unsafe State\n");
break;
}
}
if(safe){
printf("\n\nSystem is in safestate \n");
printf("Safe State Sequence \n");
for(i=0;i<k;i++)
printf("P[%d] ",seq[i]);
printf("\n\n");
}
}
void reqRes(){
printf("\nRequest for new Resourses");
printf("\nProcess id ? ");
scanf("%d",&id);
printf("Enter new Request details ");
for(i=0;i<r;i++){
scanf("%d",&nr);
if( nr <= p[id].need[i])
{
if( nr <= aval[i]){
aval[i] -= nr;
p[id].all[i] += nr;
p[id].need[i] -= nr;
}
else
wait = 1;
}
else
max_err = 1;
}
if(!max_err && !wait)
safeState();
else if(max_err){
printf("\nProcess has exceeded its maximum usage \n");
}
else{
printf("\nProcess need to wait\n");
}
}
void main()
{
printf("\n\n---Resourse Details---");
for(i=0;i<pno;i++){
}
// Calcualting need
for(i=0;i<pno;i++){
for(j=0;j<r;j++){
p[i].need[j] = p[i].max[j] - p[i].all[j];
}
}
printf("%d\t\t",i);
for(j=0;j<r;j++){
printf("%d ",p[i].all[j]);
}
printf("\t\t");
for(j=0;j<r;j++){
printf("%d ",p[i].max[j]);
}
printf("\t\t");
for(j=0;j<r;j++){
printf("%d ",p[i].need[j]);
}
printf("\n");
//end:printf("\n");
OUTPUT
Enter no of process 5
Enter no. of resourses 3
Enter Available Resourse of each type 3
3
2
---Resourse Details---
Resourses for process 0
Allocation Matrix
010
Allocation Matrix
302
Maximum Resourse Request
322
Allocation Matrix
302
Maximum Resourse Request
902
Allocation Matrix
211
Maximum Resourse Request
222
Allocation Matrix
002
Maximum Resource Request
433
Process Details
Pid Allocation Max Need
0 0 1 0 7 5 3 7 4 3
1 3 0 2 3 2 2 0 2 0
2 3 0 2 9 0 2 6 0 0
3 2 1 1 2 2 2 0 1 1
4 0 0 2 4 3 3 4 3 1
System is in safe state
Pgm.No.3
ALGORITHM
● Seek Time:Seek time is the time taken to locate the disk arm to a specified track where the data
is to be read or write. So the disk scheduling algorithm that gives minimum average seek time is
better.
● Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to rotate
into a position so that it can access the read/write heads. So the disk scheduling algorithm that
gives minimum rotational latency is better.
● Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed
of the disk and number of bytes to be transferred.
● Disk Access Time: Disk Access Time is:
FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are addressed in
the order they arrive in the disk queue.Let us understand this with the help of an example.
PROGRAM
*****************************************************************
***************
DISK SHEDULING ALGORITHM- FIRST COME FIRST SERVE
****************************************************************************
****
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int n,q[10],i;
float total=0;
printf("Enter queue size:");
scanf("%d",&n);
printf("Enter list to be sheduled:");
for(i=1;i<=n;i++)
{
scanf("%d",&q[i]);
}
printf("Enter initial head:");
scanf("%d",&q[0]);
for(i=1;i<=n;i++)
{
total=total+abs(q[i]-q[i-1]);
}
printf("Seek Count = %.2f",total);
printf("\nAverage Seek Count = %.2f",total/n);
getch();
}
****************************************************************************
****
OUTPUT
------
Enter queue size:5
Enter list to be sheduled:90 179 28 3 150
Enter initial head:50
Seek Count = 452.00
Average Seek Count = 90.40
****************************************************************************
****
ALGORITHM
1. As the name suggests, this algorithm scans all the cylinders of the disk back and forth.
2. Head starts from one end of the disk and move towards the other end servicing all the requests
in between.
3. After reaching the other end, head reverses its direction and move towards the starting end
servicing all the requests in between.
4. The same process repeats.
PROGRAM
#include<stdio.h>
void main()
{
int n,q[10],i,j,s,d,temp,x,y;
float total=0;
printf("Enter disk size:");
scanf("%d",&s);
s--;
printf("Enter queue size:");
scanf("%d",&n);
printf("Enter list to be sheduled:");
for(i=1;i<=n;i++){ scanf("%d",&q[i]); }
printf("Enter initial head:");
scanf("%d",&q[0]);
printf("Enter move direction:");
scanf("%d",&d);
x=q[0];
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(q[j]<q[i])
{
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
}
}
for(i=0;i<=n;i++)
{
if(q[i]==x)
y=i;
}
printf("Disk Sheduling Order\n%d",q[y]);
if(d==1)
{
for(i=y+1;i<=n;i++)
{
printf("->%d",q[i]);
total=total+q[i]-q[i-1];
}
printf("->%d",s);
total=total+s-q[--i];
printf("->%d",q[y-1]);
total=total+s-q[y-1];
for(i=y-2;i>=0;i--){
printf("->%d",q[i]);
total=total+q[i+1]-q[i];
}
}
if(d==0)
{
for(i=y-1;i>=0;i--)
{
printf("->%d",q[i]);
total=total+q[i+1]-q[i];
}
printf("->0");
total=total+q[++i];
printf("->%d",q[y+1]);
total=total+q[y+1];
for(i=y+2;i<=n;i++)
{
total=total+q[i]-q[i-1];
printf("->%d",q[i]);
}
}
The circular SCAN (C-SCAN) scheduling algorithm is a modified version of the SCAN disk scheduling
algorithm that deals with the inefficiency of the SCAN algorithm by servicing the requests more
uniformly. Like SCAN (Elevator Algorithm) C-SCAN moves the head from one end servicing all the
requests to the other end. However, as soon as the head reaches the other end, it immediately returns to
the beginning of the disk without servicing any requests on the return trip (see chart below) and starts
servicing again once reaches the beginning. This is also known as the “Circular Elevator Algorithm” as
it essentially treats the cylinders as a circular list that wraps around from the final cylinder to the first
one.
PROGRAM
#include<stdio.h>
void main()
{
int n,q[10],i,j,s,d,temp,x,y;
float total=0;
printf("Enter disk size:");
scanf("%d",&s);
s--;
}
****************************************************************************
****
OUTPUT
------
Enter disk size:200
Enter queue size:8
Enter list to be sheduled:176 79 34 60 92 11 41 114
Enter initial head:50
Disk Sheduling Order
50->60->79->92->114->176->199->0->11->34->41
Seek time is:190.00
Pgm No . 4
PAGE REPLACEMENT ALGORITHMS
AIM
ALGORITHM
Page Fault: The page fault takes place when the main program accesses the memory page which is
mapped into a virtual address space but is not loaded in physical memory.
When the Physical Memory is much smaller than the Virtual Memory in such a situation Page Fault
happens.
FIFO which is also called First In First Out is one of the types of Replacement Algorithms. This
algorithm is used in a situation where an Operating system replaces an existing page with the help of
memory by bringing a new page from the secondary memory.
FIFO is the simplest among all algorithms which are responsible for maintaining all the pages in a
queue for an operating system and also keeping track of all the pages in a queue.
The older pages are kept in the front and the newer ones are kept at the end of the queue. Pages that are
in the front are removed first and the pages which are demanded are added.
PROGRAM
****************************************************************************
****
PAGE REPLACEMENT - FIRST IN FIRST OUT
****************************************************************************
****
#include<stdio.h>
#include<conio.h>
void main()
{
int p,f,a[20],frame[10],i,j,c=0,k=0,flag;
printf("Enter number of pages:");
scanf("%d",&p);
printf("Enter number of frames:");
scanf("%d",&f);
1: 1 -1 -1
2: 1 2 -1
4: 1 2 4
3: 3 2 4
2: 3 2 4
****************************************************************************
****
PAGE REPLACEMENT - LEAST RECENTLY USED
ALGORITHM
Page replacement is a widely used approach in memory management. The idea is simple: the main
memory cannot hold all the referred pages in memory. So whenever a new page is referenced, an
existing page is replaced by the page that was newly called. The goal of any page replacement
mechanism is to minimize the number of page faults.
Least Recently Used (LRU) algorithm is a page replacement technique used for memory management.
According to this method, the page which is least recently used is replaced. Therefore, in memory, any
page that has been unused for a longer period of time than the others is replaced.
This idea is somewhat based on locality of reference, that any page that has been unused for a great
amount of time is more likely to remain unused. Therefore, it is better to replace that page.
Given a reference string: 0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4 and given that there are a total of 3 memory
locations available.
1. The first three pages, 0, 1, 2, will give rise to 3-page faults (look at the image above).
2. Now that the table is filled and the next page, 3, is not present in the table, a page must be
replaced. Since 0 is the least recently used page, page 3 will replace 0. This will result in a 1
page fault.
3. This process is repeated for the entire reference string.
4. When a number from the reference string already exists in the table, there is no page fault and
no effort is required to replace the page. In that case, the reference page will become the
recently used page (like when 0 is referenced when the page frames contain 4, 0 and 1.)
5. Make sure you look at whether or not a given page is recently used from the reference string.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int p,f,page[20],frame[10],time[10],i,j,k,x,c=0,flag,count=1;
printf("Enter number of page:");
scanf("%d",&p);
printf("Enter number of frames:");
scanf("%d",&f);
printf("Enter page numbers:");
for(i=0;i<p;i++)
{
scanf("%d",&page[i]);
}
for(i=0;i<f;i++)
{
frame[i]=-1;
time[i]=0;
}
printf("\n");
for(i=0;i<p;i++)
{
flag=0;
for(j=0;j<f;j++)
{
if(frame[j]==page[i])
{
flag=1;
time[j]=count;
}
}
if(flag==0)
{
c++;
for(j=0,k=0;j<f;j++)
{
if(time[j]<time[k])
k=j;
}
frame[k]=page[i];
time[k]=count;
}
count++;
printf("%d:\t",page[i]);
for(j=0;j<f;j++)
printf("%d\t",frame[j]);
printf("\n");
}
printf("Number of page faults:%d",c);
getch();
}
****************************************************************************
****
OUTPUT
4: 4 -1 -1
2: 4 2 -1
3: 4 2 3
2: 4 2 3
2: 4 2 3
3: 4 2 3
4: 4 2 3
1: 4 1 3
Number of page faults:4
****************************************************************************
****
****************************************************************************
****
PAGE REPLACEMENT - LEAST FREQUENTLY USED
ALGORITHM
In this, it is using the concept of paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when the new page comes in. Whenever a new page
is referred to and is not present in memory, the page fault occurs and the Operating System replaces one
of the existing pages with a newly needed page. LFU is one such page replacement policy in which the
least frequently used pages are replaced. If the frequency of pages is the same, then the page that has
arrived first is replaced first.
Example :
Given a sequence of pages in an array of pages[] of length N and memory capacity C, find the
number of page faults using the Least Frequently Used (LFU) Algorithm.
Example-1 :
Input : N = 7, C = 3
pages = { 1, 2, 3, 4, 2, 1, 5 }
Output :
Page Faults = 6
Page Hits = 1
Explanation :
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f,frame[50],page[50],count=1,fre[50],fault=0,time[50],flag,j,k,i;
printf("Enter the no pages:");
scanf("%d",&n);
printf("Enter the no of frames:");
scanf("%d",&f);
printf("Enter the ref string:");
for(i=0;i<n;i++)
{
scanf("%d",&page[i]);
}
for(i=0;i<f;i++)
{
frame[i]=-1;
time[i]=0;
fre[i]=0;
}
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<f;j++)
{
if(frame[j]==page[i])
{
flag=1;
fre[j]++;
}
}
if(flag==0)
{
fault++;
for(j=0,k=0;j<f;j++)
{
if(fre[j]<fre[k])
k=j;
else if(fre[j]==fre[k])
{
if(time[j]<time[k])
k=j;
}
}
frame[k]=page[i];
time[k]=count;
count++;
fre[k]=1;
}
printf("%d",page[i]);
for(j=0;j<f;j++)
{
printf("\t%d",frame[j]);
}
printf("\n");
}
printf("Page fault is %d",fault);
getch();
}
****************************************************************************
****
OUTPUT
Enter the no pages:12
Enter the no of frames:3
Enter the ref string:1 2 3 4 1 2 5 1 2 3 4 5
1 1 -1 -1
2 1 2 -1
3 1 2 3
4 4 2 3
1 4 1 3
2 4 1 2
5 5 1 2
1 5 1 2
2 5 1 2
3 3 1 2
4 4 1 2
5 5 1 2
Page fault is 10
Pgm.No.5
AIM
Simulate the following file organization techniques
a). Single level
b). Two level
c). Hierarchical
ALGORITHM
The single-level directory is the simplest directory structure. In it, all files are contained in the same
directory which makes it easy to support and understand. A single level directory has a significant
limitation, however, when the number of files increases or when the system has more than one user.
PROGRAM
#include<stdio.h>
#include<string.h>
struct dirct{
char dir[20],file[20][10];
int findex;
};
void main(){
int i, ch=1;
struct dirct d;
char ser[20];
d.findex=0;
printf("Enter the directory name ");
scanf("%s",d.dir);
do{
switch(ch){
break;
}while(ch);
printf("\n");
}
OUTPUT
Search completed
File found at 2 position
Search completed
No such file or directory
ALGORITHM
The directory structure is the organization of files into a hierarchy of folders. In a single-level directory
system, all the files are placed in one directory. There is a root directory which has all files. It has a
simple architectureand there are no sub directories. Advantage of single level directory system is that it
is easy to find a file in thedirectory. In the two-level directory system, each user has own user file
directory (UFD). The system maintains a master block that has one entry for each user. This master
block contains the addresses of the directory of the users. When a user job starts or a user logs in, the
system's master file directory (MFD) is searched. When a user refers to a particular file, only his own
UFD is searched. This effectively solves the name collision problem and isolates users from one
another. Hierarchical directory structure allows users to create their own subdirectories and to organize
their files accordingly. A tree is the most common directory structure. The tree has a root directory, and
every file in the system has a unique path name. A directory (or subdirectory) contains a set of files or
subdirectories.
PROGRAM
#include<stdio.h>
#include<string.h>
struct dirct{
char dir[20],file[20][10];
int findex;
};
void main(){
int i,j,ch=1,dindex=0,found=0;
struct dirct d[10];
char ser[20];
for(i=0;i<10;i++)
d[i].findex=0;
do{
switch(ch){
scanf("%s",d[dindex].dir);
dindex++;
printf("Directoiry Created created\n");
break;
case 2: printf("\nEnter the directory name ");
scanf("%s",ser);
found = 0;
for(i=0;i<dindex;i++){
if(!strcmp(ser,d[i].dir))
{
printf("\nEnter the file name ");
scanf("%s",d[i].file[d[i].findex++]);
printf("File created\n");
break;
}
}
if(i==dindex){
printf("\nSearch completed\n");
printf("No such file or directory\n");
}
break;
if(!strcmp(ser,d[i].file[j]))
{
printf("%s is removed\n", d[i].file[j]);
strcpy(d[i].file[j],d[i].file[d[i].findex-1]);
d[i].findex--;
found=1;
break;
}
}
if(!found){
printf("\nSearch completed\n");
printf("No such file or directory\n");
}
break;
case 4: printf("\nEnter the file name ");
scanf("%s",ser);
found = 0;
for(i=0;i<dindex;i++){
for(j=0;j<d[i].findex;j++){
if(!strcmp(ser,d[i].file[j]))
{
printf("%s is removed\n", d[i].file[j]);
found=1;
break;
}
}
if(!found){
printf("\nSearch completed\n");
printf("No such file or directory\n");
}
break;
case 5: for(i=0;i<dindex;i++){
printf("\nThe files in the directory %s are;\n",d[i].dir);
for(j=0;j<d[i].findex;j++)
printf("%s\n", d[i].file[j]);
}
break;
}while(ch);
printf("\n");
}
OUTPUT
Search completed
No such file or directory
csaa
Pgm.No.6
AIM
ALGORITHM
The mapping from virtual to physical address is done by the memory management unit (MMU)
which is a hardware device and this mapping is known as paging technique.
● The Physical Address Space is conceptually divided into a number of fixed-size blocks,
called frames.
● The Logical address Space is also splitted into fixed-size blocks, called pages.
● Page Size = Frame Size
● Page number(p): Number of bits required to represent the pages in Logical Address
Space or Page number
● Page offset(d): Number of bits required to represent particular word in a page or page
size of Logical Address Space or word number of a page or page offset.
● Frame number(f): Number of bits required to represent the frame of Physical Address
Space or Frame number.
● Frame offset(d): Number of bits required to represent particular word in a frame or
frame size of Physical Address Space or word number of a frame or frame offset.
PROGRAM
#include<stdio.h>
void main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour memsize is %d ",memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}
OUTPUT:
Your memsize is 15
Enter page size:5
Pgm.No.7
AIM
ALGORITHM
Sequntial File allocation
STEP 8: Print the file number, length, and the block allocated.
PROGRAM
Sequencial
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
//exit();
return 0
//getch();
}
OUTPUT
Files Allocated are :
Enter starting block and length of files: 14 3
14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)0
Indexed
ALGORITHM
STEP 6: If the location is allocated set the flag = 1, and if free set flag = 0.
STEP 7: Print the file number, length, and the block allocated.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
Department of Computer Science and Engineering
50
CSL331 System Software and Microprocesors Lab
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
OUTPUT
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
Linked
ALGORITHM
STEP 5: If the location is free set the flag=0 a location is allocated set the flag = 1.
STEP 6: Print the file number, length, and the block allocated.
PROGRAM
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
OUTPUT
Pgm.No.8
AIM
ALGORITHM
Begin
end( if START)
Else
initialize LOCCTR to 0
While OPCODE != ‘END’ do
Begin
if this is not a comment line then
Begin
if there is a symbol in the LABEL field then
Begin
search SYMTAB for LABEL
if found then
set error flag (duplicate symbol)
Else
(if symbol)
search OPTAB for OPCODE
if found then
add 3 (instr length) to LOCCTR
else if OPCODE = ‘WORD’ then
add 3 to LOCCTR
else if OPCODE = ‘RESW’ then
add 3 * #[OPERAND] to LOCCTR
else if OPCODE = ‘RESB’ then
begin
end
else
End {pass 1}
PROGRAM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
char opcode[10],operand[10],label[10],ch,t1[20],t2[20],t3[10];
int locctr,start,len,s=-1,o=-1,i,j=0,flag,size=0,opd;
FILE *fp1,*fp2,*fp3,*fp4,*fp5;
struct SYMTAB
{
char label[10];
int addr;
}ST[30];
struct OPTAB
{
char opcode[10],hexcode[10];
}OT[30];
void read_OPTAB()
{
while(1)
{
o++;
fscanf(fp2,"%s%s",OT[o].opcode,OT[o].hexcode);
if(getc(fp2)==EOF) break;
}
}
void read_line()
{
strcpy(t1,"");strcpy(t2,"");strcpy(t3,"");
fscanf(fp1,"%s",t1);
if(getc(fp1)!='\n')
{
fscanf(fp1,"%s",t2);
if(getc(fp1)!='\n')
{
fscanf(fp1,"%s",t3);
}
}
if(strcmp(t1,"")!=0 && strcmp(t2,"")!=0 && strcmp(t3,"")!=0 )
{
strcpy(label,t1);strcpy(opcode,t2); strcpy(operand,t3);
}
else if(strcmp(t1,"")!=0 && strcmp(t2,"")!=0 && strcmp(t3,"")==0 )
{
strcpy(label,"");strcpy(opcode,t1); strcpy(operand,t2);
}
else if(strcmp(t1,"")!=0 && strcmp(t2,"")==0 && strcmp(t3,"")==0 )
{
strcpy(label,"");strcpy(opcode,t1);strcpy(operand,"");
}
}
int main()
{
fp1=fopen("z:\\input.txt","r");
fp2=fopen("z:\\optab.txt","r");
fp3=fopen("z:\\symtab.txt","w");
fp4=fopen("z:\\intermed.txt","w");
fp5=fopen("z:\\length.txt","w");
// do until END
while(strcmp(opcode,"END")!=0)
{
fprintf(fp4,"%x\t%s\t%s\t%s\n",locctr,label,opcode,operand); //Write this line
into intermediate file
if(strcmp(label,"")!=0) //if label present
{
for(i=0;i<=s;i++)
{
if(strcmp(ST[i].label,label)==0) //if SYMTAB contains the present label
{
printf("Error");
exit(0);
}
}
s++;
strcpy(ST[s].label,label); ST[s].addr=locctr; //if SYMTAB doesnot contains the
present label
}
// Search OPTAB for opcode
flag=0;
for(i=0;i<=o;i++)
{
if(strcmp(opcode,OT[i].opcode)==0) //if opcode is in OPTAB
{
locctr+=0x3;
size=size+3;
flag=1;
break;
}
}
if(flag==0)
{
if(strcmp(opcode,"WORD")==0) //if opcode = "WORD"
{
locctr+=0x3;
size=size+3;
}
else if(strcmp(opcode,"RESW")==0) //if opcode = "RESW"
locctr+=(0x3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0) //if opcode = "RESB"
locctr+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0) //if opcode = "BYTE"
{
locctr+=0x1;
size=size+1;
}
}
Pgm.No.9
AIM
begin
end
begin
begin
end
end
End {Pass 2}
PROGRAM
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
char
t1[20],t2[20],t3[10],t4[10],address[10],label[10],opcode[10],operand[10],length[10],size[10],a[
10],ad[10],symbol[10],add[10],st_addr[10]; //ch;
int s=-1,o=-1,i,j;
FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6;
struct SYMTAB
{
char label[10];
char addr[10];
}ST[30];
struct OPTAB
{
char opcode[10],hexcode[10];
}OT[30];
void read_OPTAB()
{
while(1)
{
o++;
fscanf(fp3,"%s%s",OT[o].opcode,OT[o].hexcode);
if(getc(fp3)==EOF) break;
}
}
void read_SYMTAB()
{
while(1)
{
s++;
fscanf(fp2,"%s%s",ST[s].label,ST[s].addr);
if(getc(fp2)==EOF) break;
}
}
void read_line()
{
strcpy(t1,"");strcpy(t2,"");strcpy(t3,"");strcpy(t4,"");
fscanf(fp1,"%s",t1);
if(getc(fp1)!='\n')
{
fscanf(fp1,"%s",t2);
if(getc(fp1)!='\n')
{
fscanf(fp1,"%s",t3);
if(getc(fp1)!='\n')
fscanf(fp1,"%s",t4);
}
}
if(strcmp(t1,"")!=0 && strcmp(t2,"")!=0 && strcmp(t3,"")!=0 && strcmp(t4,"")!=0 )
{
strcpy(address,t1);strcpy(label,t2);strcpy(opcode,t3); strcpy(operand,t4);
}
else if(strcmp(t1,"")!=0 && strcmp(t2,"")!=0 && strcmp(t3,"")!=0 && strcmp(t4,"")==0 )
{
strcpy(address,t1);strcpy(label,"");strcpy(opcode,t2); strcpy(operand,t3);
}
else if(strcmp(t1,"")!=0 && strcmp(t2,"")!=0 && strcmp(t3,"")==0 && strcmp(t4,"")==0 )
{
if(strcmp(t1,"END")==0)
{
strcpy(address,"");strcpy(label,"");strcpy(opcode,t1); strcpy(operand,t2);
}
else
{
strcpy(address,t1);strcpy(label,"");strcpy(opcode,t2); strcpy(operand,"");
}
}
}
int main()
{
fp1=fopen("intermed.txt","r");
fp2=fopen("symtab.txt","r");
fp3=fopen("optab.txt","r");
fp4=fopen("length.txt","r");
fp5=fopen("assmlist.txt","w");
fp6=fopen("objcode.txt","w");
fscanf(fp4,"%s%s",length,size);
read_OPTAB();
read_SYMTAB();
fscanf(fp1,"%s%s%s",label,opcode,operand);
strcpy(st_addr,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp5,"\t%s\t%s\t%s\n",label,opcode,operand);
fprintf(fp6,"H%s 00%s0000%s\n",label,operand,length);
fprintf(fp6,"T00%s%x",operand,atoi(size));
read_line();
}
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"BYTE")==0)
{
fprintf(fp5,"%s\t%s\t%s\t%s\t",address,label,opcode,operand);
sprintf(ad,"%x",operand[2]);
fprintf(fp5,"%s\n",ad);
fprintf(fp6,"%s",ad);
}
else if(strcmp(opcode,"WORD")==0)
{
sprintf(a,"%x",atoi(operand));
fprintf(fp5,"%s\t%s\t%s\t%s\t00000%s\
n",address,label,opcode,operand,a);
fprintf(fp6,"00000%s",a);
}
else if(strcmp(opcode,"RESB")==0)
{
fprintf(fp5,"%s\t%s\t%s\t%s\n",address,label,opcode,operand);
}
else if(strcmp(opcode,"RESW")==0)
{
fprintf(fp5,"%s\t%s\t%s\t%s\n",address,label,opcode,operand);
}
else
{ j=0;
while(strcmp(opcode,OT[j].opcode)!=0)
j++;
i=0;
while(strcmp(operand,ST[i].label)!=0)
i++;
fprintf(fp5,"%s\t%s\t%s\t%s\t%s%s\
n",address,label,opcode,operand,OT[j].hexcode,ST[i].addr);
fprintf(fp6,"%s%s",OT[j].hexcode,ST[i].addr);
}
read_line();
}
fprintf(fp5,"\t%s\t%s\t%s",label,opcode,operand);
fprintf(fp6,"\nE^00%s",st_addr);
getch();
}
Pgm.No.10
ABSOLUTE LOADER
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
FILE *fp;
int i,addr1,l,j,staddr1;
char name[10],line[50],name1[10],addr[10],rec[10],ch,staddr[10],exeaddr[10];
printf("enter program name:");
scanf("%s",&name);
fp=fopen("Z:\\KRISH\\absolue\\OBJECTCODE.txt","r");
fscanf(fp,"%s",line);
for(i=2,j=0;i<8,j<6;i++,j++)
{
name1[j]=line[i];
}
name1[j]='\0';
printf("name from obj.%s\n",name1);
if(strcmp(name,name1)==0)
{
while(!feof(fp))
{
fscanf(fp,"%s",line);
while(line[0]=='T')
{
for(i=2,j=0;i<8,j<6;i++,j++)
{
staddr[j]=line[i];
}
staddr[j]='\0';
staddr1=atoi(staddr);
i=12;
while(line[i]!='$')
{
if(line[i]!='^')
{
printf("00%d\t%c%c\n",staddr1,line[i],line[i+1]);
staddr1++;
i=i+2;
}
else
i++;
}
fscanf(fp,"%s",line);
if(line[0]='E')
{
for(i=2,j=0;i<8,j<6;i++,j++)
{
exeaddr[j]=line[i];
}
exeaddr[j]='\0';
}
}
}
fclose(fp);
}
objectcode.txt
H^SAMPLE^001000^0035
T^001000^0C^001003^071009$
T^002000^03^111111$
H^SAMPLE^001000^0035
T^001000^0C^001003^071009$
T^002000^03^111111$
E^001000
OUTPUT
Pgm.No.11
RELOCATING LOADER
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void convert(char h[12]);
char bitmask[12];
char bit[12]={0};
void main()
{char add[6],length[10],input[10],binary[12],relocbit,ch,pn[5];
int start,inp,len,i,address,opcode,addr,actualadd,tlen;
FILE *fp1,*fp2;
clrscr();
printf("\n\n Enter the actual starting address : ");
scanf("%x",&start);
fp1=fopen("RLIN.txt","r");
fp2=fopen("RLOUT.txt","w");
fscanf(fp1,"%s",input);
fprintf(fp2," ----------------------------\n");
fprintf(fp2," ADDRESS\tCONTENT\n");
fprintf(fp2," ----------------------------\n");
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",pn);
fscanf(fp1,"%x",add);
fscanf(fp1,"%x",length);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%x",&address);
fscanf(fp1,"%x",&tlen);
fscanf(fp1,"%s",bitmask);
address+=start;
convert(bitmask);
len=strlen(bit);
if(len>=11)
len=10;
for(i=0;i<len;i++)
{
fscanf(fp1,"%x",&opcode);
fscanf(fp1,"%x",&addr);
relocbit=bit[i];
if(relocbit=='0')
actualadd=addr;
else
actualadd=addr+start;
fprintf(fp2,"\n %x\t\t%x%x\n",address,opcode,actualadd);
address+=3;
}
fscanf(fp1,"%s",input);
}
}
fprintf(fp2," ----------------------------\n");
fcloseall();
printf("\n\n The contents of output file(RLOUT.TXT n\n");
fp2=fopen("RLOUT.txt","r");
ch=fgetc(fp2);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp2);
}
fclose(fp2);
getch();
}
void convert(char h[12])
{
int i,l;
strcpy(bit,"");
l=strlen(h);
for(i=0;i<l;i++)
{
switch(h[i])
{
case '0':
strcat(bit,"0");
break;
case '1':
strcat(bit,"1");
break;
case '2':
strcat(bit,"10");
break;
case '3':
strcat(bit,"11");
break;
case '4':
strcat(bit,"100");
break;
case '5':
strcat(bit,"101");
break;
case '6':
strcat(bit,"110");
break;
case '7':
strcat(bit,"111");
break;
case '8':
strcat(bit,"1000");
break;
case '9':
strcat(bit,"1001");
break;
case 'A':
strcat(bit,"1010");
break;
case 'B':
strcat(bit,"1011");
break;
case 'C':
strcat(bit,"1100");
break;
case 'D':
strcat(bit,"1101");
break;
case 'E':
strcat(bit,"1110");
break;
case 'F':
strcat(bit,"1111");
break;
}
}
}
INPUT file:
RLIN.TXT
OUTPUT
400f 485061
4012 3c4003
4015 20402a
4018 1c4039
401b 30402d
6503 1d4036
6506 184033
6509 4c1000
650c 801000
650f 601003
Pgm.No.12
PROGRAM
Pass one of two pass macro processor
#include<stdio.h>
#include<string.h>
void main()
{
fp1=fopen("inputm.txt","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
if(!strcmp(opcode,"MACRO")){
fp[m]=fopen(operand,"w");
m++;
fscanf(fp1,"%s%s%s",label,opcode,operand);
while(strcmp(opcode,"MEND")!=0){
fprintf(fp[m-1],"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
}
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
}
INPUT FILES
inputm.txt
** MACRO m1
** LDA ALPHA
** STA BETA
** MEND **
** MACRO m2
** MOV a,b
** MEND **
** START 1000
** LDA a
** CALL m1
** CALL m2
** END **
OUTPUT FILES
m1.txt
** LDA ALPHA
** STA BETA
m2.txt
** MOV a,b
PROGRAM
#include<stdio.h>
#include<string.h>
void main()
{
fp1=fopen("inputm.txt","r");
fp2=fopen("macro_out.txt","w");
fscanf(fp1,"%s%s%s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
if(!strcmp(opcode,"CALL"))
{
fp[m]=fopen(operand,"r");
m++;
fscanf(fp[m-1],"%s%s%s",label,opcode,operand);
while(!feof(fp[m-1]))
{
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp[m-1],"%s%s%s",label,opcode,operand);
}
}
else
{
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
}
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
}
INPUT FILES
inputm.txt
** MACRO m1
** LDA ALPHA
** STA BETA
** MEND **
** MACRO m2
** MOV a,b
** MEND **
** START 1000
** LDA a
** CALL m1
** CALL m2
** END **
OUTPUT FILES
m1.txt
** LDA ALPHA
** STA BETA
m2.txt
** MOV a,b
output file
** MACRO m1
** LDA ALPHA
** STA BETA
** MEND**
** MACRO m2
** MOV a,b
** MEND**
** START 1000
** LDA a
** END **
Pgm.No.13
AIM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
int m=0,i,j,flag=0;
char c,*s1,*s2,*s3,*s4,str[50]=" ",str1[50]=" ";
char mac[10][10];
void main()
{
FILE *fpm=fopen("macro.txt","r");
FILE *fpi=fopen("minput.txt","r");
FILE *fpo=fopen("moutput.txt","w");
clrscr();
while(!feof(fpm))
{
fgets(str,50,fpm);
s1=strtok(str," ");
s2=strtok(NULL," ");
if(strcmp(s1,"MACRO")==0)
{
strcpy(mac[m],s2);
m++;
}
s1=s2=NULL;
}
fgets(str,50,fpi);
while(!feof(fpi))
{
flag=0;
strcpy(str1,str);
for(i=0;i<m;i++)
{
if(strcmp(str1,mac[i])==0)
{
rewind(fpm);
while(!feof(fpm))
{
fgets(str,50,fpm);
s2=strtok(str," ");
s3=strtok(NULL," ");
if(strcmp(s2,"MACRO")==0&&strcmp(s3,str1)==0)
{
fgets(str,50,fpm);
strncpy(s4,str,4);
s4[4]='\0';
while(strcmp(s4,"MEND")!=0)
{
fprintf(fpo,"%s",str);
printf("\n####%s",str);
fgets(str,50,fpm);
strncpy(s4,str,4);
s4[4]='\0';
}
}
}
flag=1;
break;
}
}
if(flag==0)
{
fprintf(fpo,"%s",str);
printf("%s",str);
}
fgets(str,50,fpi);
}
fclose(fpm);
fclose(fpi);
fclose(fpo);
}
INPUT FILES
Macro.txt
MACRO ADD1
MOV A,B
ADD C
MEND
MACRO SUB1
STORE C
MEND
MInput.txt
MOV B,10
MOV C,20
ADD1
MUL C
SUB1
END
OUTPUT
MOutput.txt
MOV B,10
MOV C,20
MOV A,B
ADD C
MUL C
STORE C
END
5. What is Throughput?
The amount of work is being done by the CPU. One unit of work is the number of processes that are
completed per unit time, called throughput
6. What is Turnaround time.
The interval from the time of submission of a process to the time of completion is the turnaround
time
A dead lock avoidance algorithm dynamically examines there source-allocation state to ensure that a
circular wait condition can never exist. The resource allocation state is defined by the number of
available and allocated resources, and the maximum demand of the process. There are two algorithms:
Resource allocation graph algorithm
a. Banker’s algorithm
b. Safety algorithm
c. Resource request algorithm
17. What is Bankers Algorithm.
It is an algorithm which used in a banking system to ensure that the bank never allocated its available
cash in such a way that it could no longer satisfy the needs of all its customers.
18. What is a Safe State and what is its use in deadlock avoidance?
When a process requests an available resource, system must decide if immediate allocation leaves the
system in a safe state. System is in safe state if there exists a safe sequence of all processes. Deadlock
Avoidance: ensure that a system will never enter an unsafe state.
process to be non-contiguous, thus allowing a process to be allocating physical memory wherever the
latter is available.
26. Which page replacement algorithm will have less page fault rate?
Optimal Page Replacement
27. What is thrashing?
It is situation that CPU spends more time on paging than executing.
35. What is the best page size when designing an operating system?
The best paging size varies from system to system, so there is no single best when it comes to page
size. There are different factors to consider in order to come up with a suitable page size, such as
page table, paging time, and its effect on the overall efficiency of the operating system.
37. What is Throughput, Turnaround time, waiting time and Response time?
Throughput – number of processes that complete their execution per time unit. Turnaround time –
amount of time to execute a particular process. Waiting time – amount of time a process has been
waiting in the ready queue. Response time – amount of time it takes from when a request was submitted
until the first response is produced, not output (for time-sharing environment).
39. Under what circumstances do page faults occur? Describe the actions taken by the operating system
when a page fault occurs?
A page fault occurs when an access to a page that has not been brought into main memory takes
place. The operating system verifies the memory access, aborting the program if it is invalid. If it is
valid, a free frame is located and I/O is requested to read the needed page into the free frame. Upon
completion of I/O, the process table and page table are updated and the instruction is restarted
40 . Define the basic functions of assembler.
* Translating mnemonic operation codes to their machine language
equivalents.
* Assigning machine addresses to symbolic labels used by the
programmer.
The first instruction contains a forward reference RETADR. If we attempt to translate the program line by line,
we will unable to process the statement in line10 because we do not know the address that will be assigned to
RETADR .The address is assigned later(in line 80) in the program.
43. What are the three different records used in object program?
The header record, text record and the end record are the three different records used in object program.
The header record contains the program name, starting address and
length of the program.
Text record contains the translated instructions and data of the program.
End record marks the end of the object program and specifies the address in the program where execution is to
begin.
46. Write the steps required to translate the source program to object program.
• Convert mnemonic operation codes to their machine language
equivalents.
• Convert symbolic operands to their equivalent machine addresses
• Build the machine instruction in the proper format.
• Convert the data constants specified in the source program into their internal machine representation
• Write the object program and assembly listing.
47. What is the use of the variable LOCCTR (location counter) in assembler?
This variable is used to assign addresses to the symbols. LOCCTR is
initialized to the beginning address specified in the START statement. After each source statement is
processed the length of the assembled instruction or data area to be generated is added to LOCCTR and hence
whenever we reach a label in the source program the current value of
LOCCTR gives the address associated with the label.
48 . Define load and go assembler.
One pass assembler that generates their object code in memory for
immediate execution is known as load and go assembler. Here no object programmer is written out and hence
no need for loader.
49. What are the two different types of jump statements used in MASM assembler?
• Near jump
A near jump is a jump to a target in the same segment and it is
assembled by using a current
code segment CS.
• Far jump
A far jump is a jump to a target in a different code segment and it is
assembled by using different segment registers .
51. Write down the pass numbers (PASS 1/ PASS 2) of the following activities that occur in a two pass
assembler:
a. Object code generation
b. Literals added to literal table
c. Listing printed
d. Address location of local symbols
Answer:
a. Object code generation - PASS 2
b. Literals added to literal table – PASS 1
c. Listing printed – PASS2
d. Address location of local symbols – PASS1
52. What is meant by machine independent assembler features?
The assembler features that do not depend upon the machine
architecture are known as machine independent assembler features.
Eg: program blocks, Literals.
62. What are the statements used for conditional macro expansion?
IF-ELSE-ENDIF statement
WHILE-ENDW statement
70. What are the important factors considered while designing general purpose macro processors?
• comments
• grouping of statements
• tokens
• syntax used for macro definitions
PROGRAMS ON MASM
Commands to be followed in DOS box to run MASM programs
mount c c:\masm
edit pgmname.asm
masm pgmname.asm
link pgmname.obj
debug pgmname.exe
Pgm.No.18
16 BIT ADDITION
ALGORITHM
PROGRAM
DATA SEGMENT
N1 DW 1731H
N2 DW 9212H
N3 DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS :CODE;DS:DATA
START:
MOV AX,DATA
Department of Computer Science and Engineering
87
CSL331 System Software and Microprocesors Lab
MOV DS,AX
XOR AX,AX
MOV BX,AX
MOV AX,N1
ADD AX,N2
MOV N3,AX
JNC STOP
INC BX
STOP:
MOV CX,AX
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT
32 BIT ADDITION
● ALGORITHM
Step II : Load the LSB of first number into AX register.
● Step III : Load the MSB of first number into BX register.
● Step IV : Load the LSB of the second number into CX register.
● Step V : Load the MSB of the second number into DX register.
● Step VI : Add the LSBs of two number.
PROGRAM
DATA SEGMENT
LIST DD 12121212H,12121212H
N3 DW ?
N4 DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS :CODE;DS:DATA
START:
MOV AX,DATA
MOV DS,AX
XOR AX,AX
MOV CL,AL
MOV AX,[SI]
ADD AX,[SI+4]
MOV BX,AX
MOV N3,BX
MOV AX,[SI+2]
ADD AX,[SI+6]
MOV DX,AX
MOV N4,DX
JNC STOP
INC CL
STOP:
MOV AX,4CH
INT 21H
CODE ENDS
END START
OUTPUT
16 BIT SUBTRACTION
ALGORITHM
PROGRAM
DATA SEGMENT
N1 DW 8888H
N2 DW 4444H
N3 DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS :CODE;DS:DATA
START:
MOV AX,DATA
MOV DS,AX
XOR AX,AX
MOV BX,AX
MOV AX,N1
SUB AX,N2
MOV N3,AX
JNC STOP
INC BX
STOP:
MOV CX,AX
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT
32 BIT SUBTRACTION
ALGORITHM
PROGRAM
DATA SEGMENT
LIST DD 12121212H,12121212H
N3 DW ?
N4 DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS :CODE;DS:DATA
START:
MOV AX,DATA
MOV DS,AX
XOR AX,AX
MOV CL,AL
MOV AX,[SI]
ADD AX,[SI+4]
MOV BX,AX
MOV N3,BX
MOV AX,[SI+2]
ADD AX,[SI+6]
MOV DX,AX
MOV N4,DX
JNC STOP
INC CL
STOP:
MOV AX,4CH
INT 21H
CODE ENDS
END START
OUTPUT
16 BIT MULTIPLICATION
ALGORITHM
PROGRAM
DATA SEGMENT
N1 DW 8888H
N2 DW 4444H
N3 DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS :CODE;DS:DATA
START:
MOV AX,4343
MOV BX,1111
INT 3
CODE ENDS
END START
OUTPUT
Pgm.No.19
STRING DISPLAY
AIM
FLOWCHART
PROGRAM
DATA SEGMENT
DATA ENDS
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AH,09H
INT 21H
MOV AH,4CH
MOV AL,00H
INT 21H
CODE ENDS
END START
OUTPUT
Pgm.No.20
STRING CONCATENATENATION
AIM
FLOWCHART
PROGRAM
DATA SEGMENT
MSG1 DB "HELLO$"
MSG2 DB "WORLD$"
DATA ENDS
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AH,09H
INT 21H
MOV AH,09H
INT 21H
CODE ENDS
END START
OUTPUT
Pgm.No . 21
SORTING
AIM
FLOWCHART
PROGRAM
DATA SEGMENT
STRING1 DB 99H,12H,56H,45H,36H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MOV DS,AX
MOV CH,04H
LEA SI,STRING1
UP1:MOV AL,[SI]
MOV BL,[SI+1]
CMP AL,BL
JNC DOWN
MOV DL,[SI+1]
XCHG [SI],DL
MOV [SI+1],DL
DOWN: INC SI
DEC CL
JNZ UP1
DEC CH
JNZ UP2
INT 3
CODE ENDS
END START
OUTPUT
Pgm.No . 22
SEARCHING
AIM
ALGORITHM
PROGRAM
DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1 DB "FOUND$"
SE DB 33H
DATA ENDS
INT 21H
INT 3
ENDM
CODE SEGMENT
START:
MOV DS, AX
MOV AL, SE
UP:
MOV BL,[SI]
CMP AL, BL
JZ FO
INC SI
DEC CX
JNZ UP
PRINT MSG2
JMP END1
FO:
PRINT MSG1
END1:
INT 3
CODE ENDS
END START
OUTPUT
ADDITION-16 BIT
AIM
ALGORITHM
PROGRAM
ADDRESS MNEMONICS
INPUT
ADDRESS VALUE
0500 B5
0501 7A
0550 2A
0551 E6
OUTPUT
ADDRESS VALUE
0600 DF
0601 5F
0602 01
SUBTRACTION-16 BIT
AIM
ALGORITHM
PROGRAM
ADDRESS MNEMONICS
0400 CLC
0401 MOV BX,0900
0404 MOV SI,0700
0407 MOV DI,0800
040A MOV AX,[SI]
040C SSB AX,[DI]
040E MOV [BX],AX
0410 HLT
INPUT
ADDRESS VALUE
0700 18
0701 08
0800 40
0801 10
OUTPUT
ADDRESS VALUE
0900 D8
0901 F7
MULTIPLICATION-16 BIT
AIM
ALGORITHM
PROGRAM
ADDRESS MNEMONICS
2008 MUL BX
2000 HLT
INPUT
ADDRESS VALUE
3003 07
3002 08
3001 04
3000 03
OUTPUT
ADDRESS VALUE
3007 00
3006 1C
3005 35
3004 18
Pgm.No.24
SORTING-ASCENDING
AIM
ALGORITHM
2. Travel from starting memory location to last and compare two numbers if first number is greater
than second number then swap them.
5. Again travel from starting memory location to (last-1, by help of count) and compare two
numbers if first number is greater than second number then swap them.
7. Repeated.
PROGRAM
ADDRESS MNEMONICS
400 MOV SI, 500
403 MOV CL, [SI]
405 DEC CL
407 MOV SI, 500
40A MOV CH, [SI]
40C DEC CH
40E INC SI
40F MOV AL, [SI]
411 INC SI
412 CMP AL, [SI]
414 JC 41C
416 XCHG AL, [SI]
418 DEC SI
INPUT
ADDRESS VALUE
500 04
501 F9
502 F2
503 39
504 05
OUTPUT
ADDRESS VALUE
501 05
502 39
503 F2
504 F9
CODE CONVERSIONS
Pgm.No. 25
BCD TO HEXADECIMAL
AIM
Write a program to perform conversion of 8 bit BCD number into hexadecimal number
FLOWCHART
PROGRAM
ADDRES MNEMONICS
S
400 MOV SI, 500
403 MOV DI, 600
406 MOV BL, [SI]
408 AND BL, 0F
040A MOV AL, [SI]
040C AND AL, F0
040E MOV CL, 04
410 ROR AL, CL
412 MOV DL, 0A
414 MUL DL
416 ADD AL, BL
418 MOV [DI], AL
041A HLT
INPUT
ADDRESS VALUE
500 25
OUTPUT
ADDRESS VALUE
600 19
Pgm.No.
ASCII TO BCD
AIM
Write a program to perform conversion of ASCII(in hex) value of number to its BCD(decimal)
number
ALORITHM
BCD 00 01 02 03 04 05 06 07 08 09
PROGRAM
ADDRESS MNEMONICS
400 MOV AL,[2050]
INPUT
ADDRESS VALUE
2050 39
OUTPUT
ADDRESS VALUE
3050 09
BCD TO ASCII
AIM
ALGORITHM
PROGRAM
ADDRESS MNEMONICS
413 HLT
INPUT
ADDRESS VALUE
2000 98
OUTPUT
ADDRESS VALUE
3000 38
3001 39
Pgm.No. 25
BASIC ARITHEMATIC AND LOGICAL OPERATIONS
AIM
ALGORITHM
PROGRAM
Addition
ADDRESS MNEMONICS
MOV R0,#20H;set source address 20H to R0
3000
MOV A,@R0; take the value from source to register
3002 A
Output
Val
Address
ue
D8
21H
H
30H 00H
31H 00H
Subtraction
ALGORITHM
1. set source address 20H to R0
2. set destination address 30H to R1
3. MOV R1, #30H
4 take the value from source to register A
5 Move the value from A to R5
6 Move the value from A to R5
7 Clear register R4 to store borrow
8 Point to the next location
9 take the value from source to register A
10 store second byte
11 get back the first operand
12 Subtract R3 from A
13 If no carry save result
14 Increment R4 to get borrow
15 Store the borrow first
16 Increase R1 to point to the next address
17 Store the result
18 Stop the program
PROGRAM
Department of Computer Science and Engineering
117
CSL331 System Software and Microprocesors Lab
ADDRESS MNEMONICS
Output
Address Value
20H 73H
21H BDH
30H 01H
31H B6H
Multiplication
ALGORITHM
1. set source address 20H to R0
2. set destination address 30H to R1
3. take the first operand from source to register A
4. Point to the next location
5. take the second operand from source to register B
6. Multiply A and B
7. ; Store higher order byte to 30H
8. Increase R1 to point to the next location
9. Store lower order byte to 31H
10. Stop the program
PROGRAM
ADDRESS MNEMONICS
Output
Address Value
20H FFH
21H FFH
30H FEH
31H 01H
CODE CONVERSION
HEX to BCD
ALGORITHM
1. Initialize the address of the data
2. Get the data from an address, which is stored in R0
3. Store the content of A into R2
4. Clear the Carry Flag
5. Subtract 0AH from A
6. When a carry is present, A is numeric
7. Add41H for Alphabet
8. Jump to store the value
9. Copy R2 to A
10. Add 30H with A to get ASCII
11. Point the destination location
12. Store A content to the memory location pointed by R0
13. Stop program
PROGRAM
ADDRESS MNEMONICS
HALT: SJMPHALT
3017
Output
20H 0EH
21H
HEX to ASCII
ALGORITHM
1. move the numbers to be converted
2. compare the number with A
3. If the number is greater than A jump to label2
4. If the number is lesser than A add with 30H
5. STOP
6. END
PROGRAM
ADDRESS MNEMONICS
MAIN: MOV R0,#01H; move the numbers to be
3000 converted
SJMP STOP
300A
LABEL2:ADD A,#37H ;If the number is greater
3011 than A add with 37H
3013 STOP:
3015 END
1.What is a Microprocessor?
Microprocessor is a CPU fabricated on a single chip, program-controlled device, which fetches the
instructions from memory, decodes and executes the instructions.
3. What is Bandwidth ?
The number of bits processed by the processor in a single instruction.
· The 8086 can read a 16-bit word at an even address in one operation and at an odd address in two
operations. The 8088 needs two operations in either case.
· The least significant byte of a word on an 8086 family microprocessor is at the lower address.
<2> Direct:-A 16-bit memory address(offset) is directly specified in the instruction as a part of it.
<3> Register:-Data is stored in a register and it is referred using the particular register (except IP).
<4> Register Indirect:-The address of the memory location which contains data or operand is determined in
an indirect way.
<6> Register Relative:-The data is available at an eefective address formed by adding an 8-bit or 16-bit
displacement with the content of any one of the registers BX,BP,SI and DI in the default (either DS or ES)
segment.
<7> Based Indexed:-The effective address of the data is formed,in this addressing mode,by adding content of
a base register to the content of an index register.
<8> Relative Based Indexed:- The effective address is formed by adding an 8 or 16-bit displacement with the
sum of contents of any one of the base registers and any one of the index registers,in the default segment.
<9> Intrasegment Direct Mode:-In this mode,the address to which the control is to bve transferred lies in the
segment in which the control transfer instruction lies and appears directly in the instruction as an immediate
displacement value.
<10> Intrasegment Indirect Mode:-In this mode,the displacement to which the control is to be transferred,is
in the same segment in whgich the control transfer instruction lies,but it is passed to the instruction
indirectly.
<11> Intersegment Direct:-In this mode,the address to which the control is to be transferred is in a different
segment.
<12> Intersegment Indirect:-In this mode,the address to which the control is to be transferred lies in a
different segment and it is passed to the instruction indirectly sequentially.
A:- Cld
Std
Cli
Sti
35) What are String Instructions?
A:- Rep
MovSB/MovSW
Cmps
Scas
Lods
Stos
37.What is an Interrupts
Def:- An interrupt operation suspends execution of a program so that the system can take special action.The
interrupt routine executes and normally returns control to the interrupted procedure, which then resumes
execution.BIOS handles Int 00H-1FH, whereas DOS handles INT 20H-3FH.
39.What is an Operand?
A:-The data on which the operation is to be performed is called as an Operand.
43.What is an Instruction?
A:-An instruction is a binary pattern enetered through an input device to command the microprocessor to
perform that specific function.
45.What is Assembler?
A:-The assembler translates the assembly language program text which is given as input to the assembler to
their binary equivalents known as object code.
The time required to translate the assembly code to object code is called access time.The assembler checks
for syntax errors&displays them before giving the object code.
49.What is the use of HLDA?
A:-HLDA is the acknowledgment signal for HOLD. It indicates whether the HOLD signal is received or not.
HOLD and HLDA are used as the control signals for DMA operations.
An equivalent operation to LEA is MOV with the OFFSET operator, which generates slightly shorter
machine code.
The 8086 does not have on-chip clock generation circuit. Hence the clock generator chip, 8284 is connected
to the CLK pin of8086. The clock signal supplied by 8284 is divided by three for internal use. The maximum
internal clock frequency of8086 is 5MHz.
62. What is pipelined architecture?
In pipelined architecture the processor will have number of functional units and the execution time of
functional units are overlapped. Each functional unit works independently most of the time.
The bus interface unit and execution unit are the two functional units available in 8086 architecture.
The segment registers of 8086 are Code segment, Data segment, Stack segment and Extra segment registers.
65. Define machine cycle.
Machine cycle is defined as the time required to complete one operation of accessing memory, I/O, or
acknowledging an external request. This cycle may consist of three to six T-states.
T-State is defined as one subdivision of the operation performed in one clock period. These subdivisions are
internal states synchronized with the system clock, and each T-State is precisely equal to one clock period.
67. List the components of microprocessor (single board microcomputer) based system
The microprocessor based system consist of microprocessor as CPU, semiconductor memories like EPROM
and RAM, input device, output device and interfacing devices.
Generally I/O devices are slow devices. Therefore the speed of I/O devices does not match with the speed of
microprocessor. And so an interface is provided between system bus and I/O devices.
69. What is the difference between CPU bus and system bus?
The CPU bus has multiplexed lines but the system bus has separate lines for each signal. (The multiplexed
CPU lines are demultiplexed by the CPU interface circuit to form system bus).
70.What is a port?
The port is a buffered I/O, which is used to hold the data transmitted from the microprocessor to I/O device
or vice-versa.
71.Give some examples of port devices used in 8085 microprocessor based system?
The various INTEL I/O port devices used in 8085 microprocessor based system are 8212, 8155, 8156, 8255,
8355 and 8755.