CSL331 System Software and Microprocessor Lab
CSL331 System Software and Microprocessor Lab
LAB MANUAL
(Regulation:2019)
JAWAHARLAL COLLEGE OF ENGINEERING AND TECHNOLOGY
SYLLABUS:
LIST OF EXPERIMENTS
COURSE OUTCOMES:
Blooms’
Sl No DESCRIPTION Taxonomy
Level
C307.1 Develop 8086 programs and execute it using a
microprocessor kit. LEVEL 2
C307.2 Apply First Come First Served, Shortest Job First, Round Robin
and Priority based CPU Scheduling Algorithms. LEVEL 3
C307.3 Develop and execute programs to interface stepper motor, 8255, LEVEL 2
8279 and digital to analog converters with 8086 trainer kit.
C307.4 Implement and execute different scheduling and paging LEVEL 3
algorithms in OS.
Design and implement assemblers, Loaders and
C307.5 macroprocessors. LEVEL 3
PO
Subject Code PO1 PO2 PO3 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
4
C307.1 3 3 3 2 3 2 3 3 2 3 3 3 2
C307.2 3 3 3 2 3 2 3 3 2 3 3 3 2
3 3 3 2 3 2 3 3 2 3 3 3 2
C307.3
C307.4 3 3 3 2 3 2 3 3 2 3 3 3 2
3 3 3 2 3 2 3 3 2 3 3 3 2
C307.5
JAWAHARLAL COLLEGE OF ENGINEERING AND TECHNOLOGY
CO1 PO1 (High) It involves strong knowledge in Illustrating the use of systems calls
and Implement Process Creation and Inter Process Communication
in Operating Systems
PO4 (Medium) It involves only fair knowledge to provide valid conclusions for
investigating complex problems in process creation and
implementation
PO5 (High) It involves Strong idea in applying appropriate Techniques for creating
processes developing software systems
PO8 (Medium) It involves providing fair idea in applying ethics ethical principles and
commit to professional ethics and responsibilities while handling
processes in operating systems
PO11(Medium)
It involves Fair knowledge in Applying the Engineering and
Management principles to one’s own work, as a member and leader
in a team, to manage projects in Multidisciplinary Teams involving
process management concepts.
PO12 (High) This course outcome provides Strong chance of lifelong learning to
cope up with the emerging Technologies in area of process
management in operating systems
PO4 (Medium) It involves only fair knowledge to provide valid conclusions for
investigating complex problems in CPU scheduling algorithms using
research-based knowledge and research methods
PO5 (High) It involves Strong idea in applying appropriate Techniques for CPU
scheduling
PO8 (Medium) It involves providing fair idea in applying ethics ethical principles
and commit to professional ethics and responsibilities while
scheduling processes in operating systems
JAWAHARLAL COLLEGE OF ENGINEERING AND TECHNOLOGY
PO12 (High) This course outcome provides Strong chance of lifelong learning to
cope up with the emerging Technologies in area of process
scheduling
CO3 PO1 (High) It involves strong knowledge in computer science to solve problems
related to Memory allocation methods and Page Replacement
Algorithms.
PO4 (Medium) It involves only fair knowledge to provide valid conclusions for
investigating complex problems in memory allocation
PO8 (Medium) It involves providing fair idea in applying ethics ethical principles
and commit to professional ethics and responsibilities while in
operating systems
PO12 (High) This course outcome provides Strong chance of lifelong learning to
cope up with the emerging Technologies in area of memory
management
CO4 PO1 (High) It involves strong knowledge in computer science to solve problems
related to Deadlock Detection and Deadlock Avoidance in Operating
Systems.
PO4 (Medium) It involves only fair knowledge to provide valid conclusions for
investigating complex problems in Deadlock Detection and
Deadlock Avoidance in Operating Systems using research-based
knowledge and research methods
PO8 (Medium) It involves providing fair idea in applying ethics ethical principles
and commit to professional ethics and responsibilities in Deadlock
Detection and Deadlock Avoidance in Operating Systems
PO12 (High) This course outcome provides Strong chance of lifelong learning to
cope up with the emerging Technologies in area of handling
deadlocks
PO4 (Medium) It involves only fair knowledge to provide valid conclusions for
investigating complex problems in Storage Management and Disk
Scheduling in Operating Systems using research-based knowledge
and research methods
PO5 (High) It involves strong knowledge in Functioning effectively as an
individual, and as a member or leader in multi-disciplinary teams,
and strive to achieve common goals to study concepts of storage
management
PO8 (Medium) It involves providing fair idea in applying ethics ethical principles
and commit to professional ethics and responsibilities in storage
management Operating Systems
PO12 (High) This course outcome provides Strong chance of lifelong learning to
cope up with the emerging Technologies in area of storage
management in operating systems
PSO3 It involves fair Ability to Apply Knowledge for developing Codes and
(Medium) integrating hardware/software products in the domains of Big Data
Analytics, Web Applications and Mobile Apps incorporating the
concepts of inter process communication
PSO3 It involves fair Ability to Apply Knowledge for developing Codes and
(Medium) integrating hardware/software products in the domains of Big Data
Analytics, Web Applications and Mobile Apps incorporating the
concepts of inter process scheduling techniques
JAWAHARLAL COLLEGE OF ENGINEERING AND TECHNOLOGY
PSO3 It involves fair Ability to Apply Knowledge for developing Codes and
(Medium) integrating hardware/software products in the domains of Big Data
Analytics, Web Applications and Mobile Apps incorporating the
concepts of page replacement techniques
PSO3 It involves fair Ability to Apply Knowledge for developing Codes and
(Medium) integrating hardware/software products in the domains of Big Data
Analytics, Web Applications and Mobile Apps incorporating the
concepts of deadlocks
PSO3 It involves fair Ability to Apply Knowledge for developing Codes and
(Medium) integrating hardware/software products in the domains of Big Data
Analytics, Web Applications and Mobile Apps incorporating the
concepts of storge management
ASSESSMENT METHODOLOGIES-INDIRECT
☑ ASSESSMENT OF COURSE OUTCOMES (BY ☑ STUDENT FEEDBACK ON FACULTY
FEEDBACK, ONCE) (TWICE)
☑ ASSESSMENT OF MINI/MAJOR PROJECTS ☐ OTHERS
BY EXT.
EXPERTS
Prepared by Approved by
An Operating System is a program that manages the Computer hardware. It controls and
coordinates the use of the hardware among the various application programs for the various users.
Apart from the program code, it includes the current activity represented by
Program Counter,
Contents of Processor registers,
Process Stack which contains temporary data like function parameters, return
addresses and local variables
Data section which contains global variables
Heap for dynamic memory allocation
A Multi-programmed system can have many processes running simultaneously with the CPU
multiplexed among them. By switching the CPU between the processes, the OS can make the
computer more productive. There is Process Scheduler which selects the process among many
processes that are ready, for program execution on the CPU. Switching the CPU to another process
requires performing a state save of the current process and a state restore of new process, this is
Context Switch.
Scheduling Algorithms
CPU Scheduler can select processes from ready queue based on various scheduling
algorithms. Different scheduling algorithms have different properties, and the choice of a particular
algorithm may favour one class of processes over another. The scheduling criteria include
CPU utilization:
Throughput: The number of processes that are completed per unit time.
Waiting time: The sum of periods spent waiting in ready queue.
Turnaround time: The interval between the time of submission of process to the time
of completion.
Response time: The time from submission of a request until the first response is produced.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
14
The job with the shortest burst time will get the CPU first.
The lesser the burst time, the sooner will the process get the CPU.
It is the non-pre-emptive type of scheduling.
However, it is very difficult to predict the burst time needed for a process hence this
algorithm is very difficult to implement in the system.
In the following example, there are five jobs named as P1, P2, P3, P4 and P5. Their arrival
time and burst time are given in the table below.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
15
1 1 7 8 7 0
2 3 3 13 10 7
3 6 2 10 4 2
4 7 10 31 24 14
Since, 5 9 8 21 12 4 No Process
arrives at time 0
hence; there will be an empty slot in the Gantt chart from time 0 to 1 (the time at which the first
process arrives)
.
According to the algorithm, the OS schedules the process which is having the lowest burst
time among the available processes in the ready queue.
Till now, we have only one process in the ready queue hence the scheduler will schedule this
to the processor no matter what is its burst time.
This will be executed till 8 units of time.
Till then we have three more processes arrived in the ready queue hence the scheduler will
choose the process with the lowest burst time.
Among the processes given in the table, P3 will be executed next since it is having the lowest
burst time among all the available processes.
It is the pre-emptive form of SJF. In this algorithm, the OS schedules the Job according to
the remaining time of the execution
4. Priority Scheduling
In the Round Robin scheduling algorithm, the OS defines a time quantum (slice).
All the processes will get executed in the cyclic way.
Each of the process will get the CPU for a small amount of time (called time quantum) and
then get back to the ready queue to wait for its next turn. It is a pre-emptive type of
scheduling.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
16
A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues.
The processes are permanently assigned to one queue, generally based on some property of
the process, such as memory size, process priority, or process type.
Each queue has its own scheduling algorithm.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
17
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)
PROGRAM
#include<stdio.h>
void main()
{
int i=0,j=0,b[i],g[20],p[20],w[20],t[20],a[20],n=0,m;
float avgw=0,avgt=0;
printf("Enter the number of process : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process ID : ");
scanf("%d",&p[i]);
int temp=0;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
18
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
g[0]=0;
for(i=0;i<=n;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=g[i+1]-a[i];
w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i];
}
avgw=avgw/n;
avgt=avgt/n;
printf("pid\tarrivalT\tBrustT\tCompletionT\tWaitingtime\tTurnaroundTi\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\t%d\t\t%d\t\t\t%d\n",p[i],a[i],b[i],g[i+1],w[i],t[i]);
}
printf("\nAverage waiting time %f",avgw);
printf("\nAverage turnarround time %f",avgt);
}
OUTPUT 1
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
19
Burst Time : 3
Arrival Time: 1
Process ID : 3
Burst Time : 1
Arrival Time: 2
Process ID : 4
Burst Time : 2
Arrival Time: 3
Process ID : 5
Burst Time : 5
Arrival Time: 4
1 0 4 4 0 4
2 1 3 7 3 6
3 2 1 8 5 6
4 3 2 10 5 7
5 4 5 15 6 11
OUTPUT 2
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
20
OUTPUT 3
PROGRAM
#include<stdio.h>
void main()
{
int i=0,j=0,p[i],b[i],g[20],w[20],t[20],a[20],n=0,m;
int k=1,min=0,btime=0;
float avgw=0,avgt=0;
printf("Enter the number of process : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nProcess id : ");
scanf("%d",&p[i]);
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
21
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
btime=btime+b[i];
min=b[k];
for(j=k;j<n;j++)
{
if(btime >= a[j] && b[j]<min)
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
temp=b[j];
b[j]=b[j-1];
b[j-1]=temp;
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
22
temp=p[j];
p[j]=p[j-1];
p[j-1]=temp;
}
}
k++;
}
g[0]=a[0];
for(i=0;i<n;i++)
{
g[i+1]=g[i]+b[i];
if(g[i]<a[i])
g[i]=a[i];
}
for(i=0;i<n;i++)
{
t[i]=g[i+1]-a[i];
w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i];
}
avgw=avgw/n;
avgt=avgt/n;
printf("pid\tBrustTime\tGantChart\tWaiting time\t\tTurnarround Time\n");
for(i=0;i<n;i++)
{
printf(" %d\t %d\t\t%d-%d\t\t%d\t\t\t%d\n",p[i],b[i],g[i],g[i+1],w[i],t[i]);
}
printf("\nAverage waiting time %f",avgw);
printf("\nAverage turnarround time %f\n",avgt);
OUTPUT 1
Process id : 1
Burst Time : 7
Arrival Time: 0
Process id : 2
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
23
Burst Time : 5
Arrival Time: 1
Process id : 3
Burst Time : 1
Arrival Time: 2
Process id : 4
Burst Time : 2
Arrival Time: 3
Process id : 5
Burst Time : 8
Arrival Time: 4
OUTPUT 2
Enter the number of process : 4
Process id : 1
Burst Time : 7
Arrival Time: 0
Process id : 2
Burst Time : 4
Arrival Time: 2
Process id : 3
Burst Time : 1
Arrival Time: 4
Process id : 4
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
24
Burst Time : 4
Arrival Time: 5
Priority Scheduling
#include<stdio.h>
int main()
{
int burst_time[20], process[20], waiting_time[20], turnaround_time[20], priority[20];
int i, j, limit, sum = 0, position, temp;
float average_wait_time, average_turnaround_time;
printf("Enter Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Burst Time and Priority For %d Processes\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nProcess[%d]\n", i + 1);
printf("Process Burst Time:\t");
scanf("%d", &burst_time[i]);
printf("Process Priority:\t");
scanf("%d", &priority[i]);
process[i] = i + 1;
}
for(i = 0; i < limit; i++)
{
position = i;
for(j = i + 1; j < limit; j++)
{
if(priority[j] < priority[position])
{
position = j;
}
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
25
temp = priority[i];
priority[i] = priority[position];
priority[position] = temp;
temp = burst_time[i];
burst_time[i] = burst_time[position];
burst_time[position] = temp;
temp = process[i];
process[i] = process[position];
process[position] = temp;
}
waiting_time[0] = 0;
for(i = 1; i < limit; i++)
{
waiting_time[i] = 0;
for(j = 0; j < i; j++)
{
waiting_time[i] = waiting_time[i] + burst_time[j];
}
sum = sum + waiting_time[i];
}
average_wait_time = sum / limit;
sum = 0;
printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n");
for(i = 0; i < limit; i++)
{
turnaround_time[i] = burst_time[i] + waiting_time[i];
sum = sum + turnaround_time[i];
printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i], waiting_time[i],
turnaround_time[i]);
}
average_turnaround_time = sum / limit;
printf("\nAverage Waiting Time:\t%f", average_wait_time);
printf("\nAverage Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}
OUTPUT
Process id : 1
Burst Time : 15
Priority: 3
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
26
Process id : 2
Burst Time : 10
Priority: 2
Process id : 3
Burst Time : 90
Priority: 1
#include<stdio.h>
int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);
printf("Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
printf("\nEnter Time Quantum:\t");
scanf("%d", &time_quantum);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
27
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
28
Process[2] 5 9 4
Process[3] 3 11 8
Process[4] 4 14 10
Process[1] 9 21 12
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
29
Viva Questions
8. What is Throughput?
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
30
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
9. What is Turnaround time.
The interval from the time of submission of a process to the time of completion is the
turnaround time
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
31
DEADLOCK
Deadlock :
A set of processes is deadlocked if each process in the set is waiting for an event that only another
process in the set can cause (including itself).
Deadlock Avoidance
This approach to the deadlock problem anticipates deadlock before it actually occurs.
This approach employs an algorithm to access the possibility that deadlock could occur and acting
accordingly.
This method differs from deadlock prevention, which guarantees that deadlock cannot occur by
denying one of the necessary conditions of deadlock.
If the necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by
being careful when resources are allocated.
Perhaps the most famous deadlock avoidance algorithm, due to Dijkstra [1965], is the Banker’s
algorithm.
Safe State
To avoid deadlocks, we try to make only those transitions that will take you from one safe state to
another.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
32
We avoid transitions to unsafe state (a state that is not deadlocked, and is not safe).
Banker's algorithm is a deadlock avoidance algorithm.
It is named so because this algorithm is used in banking systems to determine whether a loan can
be granted or not.
Consider there are n account holders in a bank and the sum of the money in all of their accounts
is S.
Every time a loan has to be granted by the bank, it subtracts the loan amount from the total
money the bank has.
Then it checks if that difference is greater than S.
It is done because, only then, the bank would have enough money even if all the n account holders
draw all their money at once.
Banker's algorithm works in a similar way in computers.
The Banker's algorithm is run by the operating system whenever a process requests resources.
The algorithm prevents 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 may not exceed the total number of resources in the system.
For the Banker's algorithm to work, it needs to know three things:
How much of each resource each process could possibly request
How much of each resource each process is currently holding
How much of each resource the system has available
Some of the resources that are tracked in real systems are memory, semaphores and interface access.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
33
Pgm.No.2
BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
AIM
Implement banker’s algorithm for deadlock avoidance
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");
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
34
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()
{
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
35
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];
}
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
36
scanf("%d",&ch);
if(ch)
reqRes();
}while(ch!=0);
//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
Maximum Resourse Request
753
Allocation Matrix
302
Maximum Resourse Request
322
Allocation Matrix
302
Maximum Resourse Request
902
Allocation Matrix
211
Maximum Resourse Request
222
Allocation Matrix
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
37
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 Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
38
Viva questions
1. What is deadlock?
Deadlock is a situation that when two or more process waiting for each other and holding the
resource which is required by another process.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
39
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
40
DISK SCHEDULING
Disk scheduling is is done by operating systems to schedule I/O requests arriving for disk. It is also
known as I/O scheduling.
Disk scheduling is important because:
Multiple I/O requests may arrive by different processes and only one I/O request can be
served at a time by disk controller. Thus other I/O requests need to wait in waiting queue
and need to be scheduled.
Two or more request may be far from each other so can result in greater disk arm
movement.
Hard drives are one of the slowest parts of computer system and thus need to be accessed in
an efficient manner.
There are many Disk Scheduling Algorithms but before discussing them let’s have a quick look at
some of the important terms:
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:
Disk Response Time: Response Time is the average of time spent by a request waiting to
perform its I/O operation. Average Response time is the response time of the all
requests. Variance Response Time is measure of how individual request are serviced with
respect to average response time. So the disk scheduling algorithm that gives minimum
variance response time is better.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
41
1. 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.
Advantages:
Every request gets a fair chance
No indefinite postponement
Disadvantages:
Does not try to optimize seek time
May not provide the best possible service
3. SCAN: In SCAN algorithm the disk arm moves into a particular direction and services the
requests coming in its path and after reaching the end of disk, it reverses its direction and
again services the request arriving in its path. So, this algorithm works like an elevator and
hence also known as elevator algorithm. As a result, the requests at the midrange are
serviced more and those arriving behind the disk arm will have to wait.
Advantages:
High throughput
Low variance of response time
Average response time
Disadvantages:
Long waiting time for requests for locations just visited by disk arm. These situations
are avoided in CSAN algorithm in which the disk arm instead of reversing its direction
goes to the other end of the disk and starts servicing the requests from there. So, the disk
arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm
and hence it is known as C-SCAN (Circular SCAN).
Advantages:
Provides more uniform wait time compared to SCAN
4. CSCAN: In SCAN algorithm, the disk arm again scans the path that has been scanned, after
reversing its direction. So, it may be possible that too many requests are waiting at the other
end or there may be zero or few requests pending at the scanned area.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
42
Pgm.No.4
PROGRAM
#include<stdio.h>
void main(){
int ioq[20],i,n,ihead,tot;
float seek=0,avgs;
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
43
avgs = seek/(n);
OUTPUT 2
Enter the number of requests :5
Enter the initial head position :100
Enter the I/O queue requests
23
89
132
42
187
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
44
SCAN
PROGRAM
#include<stdio.h>
void main()
{
int ioq[20],i,n,j,ihead,temp,scan,tot;
float seek=0,avgs;
for(i=0;i<n-1;i++){
for(j=0;j<n-1;j++)
{
temp = ioq[j];
ioq[j] = ioq[j+1];
ioq[j+1] = temp;
}
}
ioq[n]=ioq[n-1];
for(i=0;i<n;i++){
if(ihead == ioq[i]){
scan = i;
break;
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
45
for(i=scan+1;i<n;i++){
tot = ioq[i+1] - ioq[i];
if(tot < 0)
tot = tot * -1;
//seek += tot;
printf("%d\t%d\n",ioq[i],tot);
}
seek = ihead + ioq[n-1];
avgs = seek/(n-2);
}
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
46
124
65
67
53 16
37 23
14 14
0 65
65 2
67 31
98 24
122 2
124 59
183 0
CSCAN
PROGRAM
#include<stdio.h>
void main()
{
int ioq[20],i,n,j,ihead,itail,temp,scan,tot=0;
float seek=0,avgs;
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
47
scanf("%d",&ioq[i]);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-1;j++)
{
temp = ioq[j];
ioq[j] = ioq[j+1];
ioq[j+1] = temp;
}
}
for(i=0;i<n+1;i++){
if(ihead == ioq[i]){
scan = i;
break;
i = scan;
temp = n;
while(i != temp){
if(tot < 0)
tot = tot * -1;
seek += tot;
}
printf("%d --> ",ioq[i]);
// printf("%d\t%d\n",ioq[i],tot);
i++;
if(i == n){
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
48
i = 0;
temp = scan;
seek += itail;
avgs = seek/(n-3);
OUTPUT
50 --> 65 --> 68 --> 90 --> 120 --> 122 --> 128 --> 200 --> 0 --> 35 --> 38 -->
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
49
Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —
> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1
Page Fault.
6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1
Page Fault.
Finally when 3 come it is not avilable so it replaces 0 1 page fault
Belady’s anomaly – Belady’s anomaly proves that it is possible to have more page faults
when increasing the number of page frames while using the First in First Out (FIFO) page
replacement algorithm. For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1,
0, 4 and 3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10 page faults.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
50
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4
Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is not used for the longest duration
of time in the future.—>1 Page fault.
0 is already there so —> 0 Page fault..
4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already
available in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating
system cannot know future requests. The use of Optimal Page replacement is to set up
a benchmark so that other replacement algorithms can be analyzed against it.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
51
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4
Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page
fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already
available in the memory.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
52
Pgm.No.7
AIM
PROGRAM
#include<stdio.h>
void main()
{
int n,f,fr[20],p[50],rep=0, found,fi=0;
int i,k;
printf("Enter the number of pages ");
scanf("%d",&n);
for(i=0;i<f;i++)
fr[i] = -1;
printf("\n\nPages\t\tFrames\n\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",p[i]);
found = 1;
for(k=0;k<f;k++)
{
if(p[i] == fr[k]){
found = 0;
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
53
break;
}
}
if(found)
{
fr[fi] = p[i];
rep++;
fi = (fi+1)%f;
for(k=0;k<f;k++)
printf("%d\t",fr[k]);
}
printf("\n");
}
printf("\n\nNumber of page fault : %d\n",rep);
}
OUTPUT
Pages Frames
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3
2
1 0 1 3
2 0 1 2
0
1
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
54
7 7 1 2
0 7 0 2
1 7 0 1
#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j,
pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
55
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
return 0;
}
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
56
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
if(frame[n] == -1 || frame[n] == pages[m])
{
if(frame[n] != -1)
{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
57
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d\t", frame[n]);
}
printf("\n");
}
printf("Page Hit:\t%d\n", hit);
return 0;
}
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
58
Viva Questions
5. Which page replacement algorithm will have less page fault rate?
Optimal Page Replacement
6. What is thrashing?
It is situation that CPU spends more time on paging than executing.
7. What is swapping
A process must be in memory to be executed. A process, however, can be swapped
temporarily out of memory to a backing store and then brought back into memory for
continued execution. This process is called swapping.
8. What is fragmentation?
fragmentation is a phenomenon in which storage space is used inefficiently, reducing
capacity or performance.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
59
larger than the hole itself. The general approach to avoiding this problem is to break the
physical memory into fixed-sized blocks and allocate memory in units based on block size.
With this approach, the memory allocated to a process may be slightly larger than the
requested memory. The difference between these two numbers is internal fragmentation.
14. 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.
16. 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).
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
60
Fragmentation occurs in a dynamic memory allocation system when many of the free blocks
are too small to satisfy any request.
External Fragmentation: External Fragmentation happens when a dynamic memory
allocation algorithm allocates some memory and a small piece is left over that cannot be
effectively used. If too much external fragmentation occurs, the amount of usable
memory is drastically reduced. Total memory space exists to satisfy a request, but it is
not contiguous
Internal Fragmentation: Internal fragmentation is the space wasted inside of allocated
memory blocks because of restriction on the allowed sizes of allocated blocks. Allocated
memory may be slightly larger than requested memory; this size difference is memory
internal to a partition, but not being used Reduce external fragmentation by compaction
->Shuffle memory contents to place all free memory together in one large block.
->Compaction is possible only if relocation is dynamic, and is done at execution time.
20. 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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
61
Information about files is maintained by Directories. A directory can contain multiple files. It can
even have directories inside of them. In Windows we also call these directories as folders.
Two-Level Directory
In this separate directories for each user is maintained.
Path name: Due to two levels there is a path name for every file to locate that file.
So same file name for different user are possible. Searching is efficient in this method.
Single-Level Directory
In this a single directory is maintained for all the users.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
62
Pgm.No.8
AIM
Simulate the following file organization techniques
a). Single level
b). Two level
c). Hierarchical
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){
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
63
printf("File created\n");
break;
case 2: printf("\nEnter the file to delete ");
scanf("%s",ser);
for(i=0;i<d.findex;i++){
if(!strcmp(ser,d.file[i]))
{
printf("File removed \n");
strcpy(d.file[i],d.file[d.findex-1]);
d.findex--;
break;
}
}
if(i==d.findex)
printf("No such file or directory\n");
break;
}while(ch);
printf("\n");
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
64
OUTPUT
Search completed
File found at 2 position
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
65
Search completed
No such file or directory
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
66
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){
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
67
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]))
{
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
68
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
69
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
70
Search completed
No such file or directory
Hierarchical Directory
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
71
PAGING
Paging is a memory management scheme that eliminates the need for contiguous allocation of
physical memory. This scheme permits the physical address space of a process to be non –
contiguous.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
72
Pgm.No.9
AIM
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);
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
73
OUTPUT:
Your memsize is 15
Enter page size:5
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
74
The purpose of file allocation in operating systems is first of all the efficient use of the disk space
or efficient disk utilization.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
75
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
76
Pgm.No.10
AIM
PROGRAM
Sequencial
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
77
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
#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]);
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
78
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
79
Linked
#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();
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
80
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
81
2. Best Fit Allocate the process to the partition which is the first smallest sufficient
partition among the free available partition. It searches the entire list of holes to
find the smallest hole whose size is greater than or equal to the size of the
process.
3. Worst Fit Allocate the process to the partition which is the largest sufficient
among the freely available partitions available in the main memory. It is opposite
to the best-fit algorithm. It searches the entire list of holes to find the largest hole
and allocate it to process.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
82
Although best fit minimizes the wastage space, it consumes a lot of processor
time for searching the block which is close to the required size. Also, Best-fit may
perform poorer than other algorithms in some cases.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
83
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
84
ASSEMBLER
System software which is used to convert assembly language program to its equivalent
object code. Input to the assembler is a source code written in assembly language. Output is
the object code. Design of assembler depends upon the machine architecture as the language
used is mnemonic language.
Analysis Phase
Look at the mnemonics table and get the opcode corresponding to the mnemonic.
Obtain the address of a memory operand from the symbol table.
Synthesize the machine instruction.
TYPES OF ASSEMBLER
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
85
In the first pass it reads the entire source file, looking only the label definitions.
All labels are collected, assigned values and placed in the symbol table in this pass.
No instructions are assembled and at the end of the pass, the symbol table should contain all
the labels defined in the program.
In the second pass, the instructions are again read and are assembled using the symbol table.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
86
i. Assembler instructions.
ii. Generate data values defined by BYTE, WORD, etc.
iii. Perform processing of assembler directives not done during pass 1.
iv. Write object program and assembly listing.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
87
Pgm.No.10
AIM
PROGRAM
#include<stdio.h>
#include<string.h>
void main()
{
FILE *f1,*f2,*f3,*f4;
char s[100],lab[30],opcode[30],opa[30],opcode1[30],opa1[30];
int locctr,x=0;
f1=fopen("input.txt","r");
f2=fopen("opcode.txt","r");
f3=fopen("out1.txt","w");
f4=fopen("sym1.txt","w");
while(fscanf(f1,"%s%s%s",lab,opcode,opa)!=EOF)
{
if(strcmp(lab,"**")==0)
{
if(strcmp(opcode,"START")==0)
{
fprintf(f3,"%s %s %s",lab,opcode,opa);
locctr=(atoi(opa));
}
else
{
rewind(f2);
x=0;
while(fscanf(f2,"%s%s",opcode1,opa1)!=EOF)
{
if(strcmp(opcode,opcode1)==0)
{
x=1;
}
}
if(x==1)
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
88
{
fprintf(f3,"\n %d %s %s %s",locctr,lab,opcode,opa);
locctr=locctr+3;
}
}
}
else
{
if(strcmp(opcode,"RESW")==0)
{
fprintf(f3,"\n %d %s %s %s",locctr,lab,opcode,opa);
fprintf(f4,"\n %d %s",locctr,lab);
locctr=locctr+(3*(atoi(opa)));
}
else if(strcmp(opcode,"WORD")==0)
{
fprintf(f3,"\n %d %s %s %s",locctr,lab,opcode,opa);
fprintf(f4,"\n %d %s",locctr,lab);
locctr=locctr+3;
}
else if(strcmp(opcode,"BYTE")==0)
{
fprintf(f3,"\n %d %s %s %s",locctr,lab,opcode,opa);
fprintf(f4,"\n %d %s",locctr,lab);
locctr=locctr+1;
}
else if(strcmp(opcode,"RESB")==0)
{
fprintf(f3,"\n %d %s %s %s",locctr,lab,opcode,opa);
fprintf(f4,"\n %d %s",locctr,lab);
locctr=locctr+1;
}
else
{
fprintf(f3,"\n %d %s %s %s",locctr,lab,opcode,opa);
fprintf(f4,"\n %d %s",locctr,lab);
locctr=locctr+(atoi(opa));
}
}
}
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
89
INPUT FILES
input.txt
** START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C'Z'
C1 RESB 1
** END **
opcode.txt
START *
LDA 03
STA 0F
LDCH 53
STCH 57
END
OUTPUT FILES
out1.txt
** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C'Z'
2019 C1 RESB 1
2020 ** END **
sym1.txt
2012 ALPHA
2015 FIVE
2018 CHARZ
2019 C1
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
90
Pgm.No.11
AIM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char opcode[20],operand[20],symbol[20],label[20],code[20],mnemonic[25], character,
add[20],objectcode[20];
int flag,flag1,locctr,location,loc;
FILE *fp1,*fp2,*fp3,*fp4;
fp1=fopen("out3.txt","r"); fp2=fopen("twoout.txt","w");
fp3=fopen("opcode.txt","r"); fp4=fopen("sym1.txt","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{ fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
while(strcmp(opcode,"END")!=0)
{ flag=0;
fscanf(fp3,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{ if((strcmp(opcode,code)==0) && (strcmp(mnemonic,"*"))!=0)
{ flag=1;
break;
}
fscanf(fp3,"%s%s",code,mnemonic);
}
if(flag==1)
{ flag1=0; rewind(fp4);
while(!feof(fp4))
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
91
{
fscanf(fp4,"%s%d",symbol,&loc);
if(strcmp(symbol,operand)==0)
{
flag1=1; break;
}}
if(flag1==1)
{
sprintf(add,"%d",loc);
strcpy(objectcode,strcat(mnemonic,add));
}}
else if(strcmp(opcode,"BYTE")==0 || strcmp(opcode,"WORD")==0)
{
if((operand[0]=='C') || (operand[0]=='X'))
{
character=operand[2];
sprintf(add,"%d",character);
strcpy(objectcode,add);
}
else
{
strcpy(objectcode,add);
}}
else
strcpy(objectcode,"\0");
fprintf(fp2,"%s\t%s\t%s\t%d\t%s\n",label,opcode,operand,locctr,objectcode);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
fprintf(fp2,"%s\t%s\t%s\t%d\n",label,opcode,operand,locctr);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
92
INPUT FILES
opcode.txt
START *
LDA 03
STA 0F
LDCH 53
STCH 57
END +
out3.txt
** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C'Z'
2019 C1 RESB 1
2020 ** END **
sym1.txt
2012 ALPHA
2015 FIVE
2018 CHARZ
2019 C1
OUTPUT FILES
twoout.txt
** START 2000
** LDA FIVE 2000 032018
** STA ALPHA 2003 0F2015
** LDCH CHARZ 2006 532019
** STCH C1 2009 572019
ALPHA RESW 1 2012
FIVE WORD 5 2015 2019
CHARZ BYTE C'Z' 2018 90
C1 RESB 1 2019
** END ** 2020
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
93
10. Write the steps required to translate the source program to object program.
• Convert mnemonic operation codes to their machine language
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
94
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.
11. 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.
12. 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.
13. 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 .
17. 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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
95
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
18. 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.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
96
The source program written in assembly language or high level language will be converted
to object program, which is in the machine language form for execution.
This conversion is either from assembler or from compiler, contains translated instructions
and data values from the source program, or specific addresses in primary memory where
these items are to be loaded for execution.
This contain three processes:
1. Loading- It allocates memory location and brings the object program in to memory for
execution.
2. Linking- It combines two or more separate object programs and supplies the information
needed to allow references between them.
3. Relocation- It modifies the object program so that it can be loaded at address different
from the location originally specified.
LOADER: It is a utility of an operating system. It copies program from a storage device to a
computer’s main memory, where the program can then be executed.
1. Read executable file’s header to determine the size of text and data segments.
2. Create new address space for the program.
3. Copies instructions and add data in to address space.
4. Copies arguments passed to the program on the stack.
5. Initializes the machine registers including the stack pointer.
6. Jumps to a start-up routine that copies the program’s arguments from the stack to registers
and calls the program’s main routine.
Types of Loader
ABSOLUTE LOADER
It is also known as Bootstrap Loader.
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
97
o It simply performs input and output operation to load a program into the main
memory.
o It is coded in very few machine instructions.
o Program is stored in the library in their ready to execute form. Such a library is
called a Phase Library.
Disadvantage:
o Programmer must explicitly specify the assembler the memory where the program is
to be loaded.
o Handling multiple subroutine become difficult since the programmer must specify
the address of the routines whenever they are referenced to perform subroutine
linkage.
o When dealing with lots of subroutines the manual shuffling and re-shuffling of
memory address references in the routines become tedious and complex.
Design of Absolute Loader
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
98
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
99
Pgm.No.12
ABSOLUTE LOADER
AIM
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];
if(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]!='^')
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
100
{
printf("00%d \t %c%c\n", staddr1,line[i],line[i+1]);
staddr1++;
i=i+2;
}
else i++;
}
}
else if(line[0]='E')
printf("jump to execution address:%s",&line[2]);
fscanf(fp,"%s",line);
}while(!feof(fp) );
}
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
101
Pgm.No.13
RELOCATING LOADER
AIM
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);
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
102
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':
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
103
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;
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
104
case 'F':
strcat(bit,"1111");
break;
}
}
}
INPUT file:
RLIN.TXT
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
105
MACRO PROCESSORS
A macro instruction (macro) is a notational convenience for the programmer. It allow the
programmer to write a shorthand version of a program . A macro represents a commonly used
group of statements in the source programming language. It replaces each macro instruction with
the corresponding group of source language statements.
A macro processor Essentially involve the substitution of one group of characters or lines
for another. Normally, it performs no analysis of the text it handles. It doesn’t concern the meaning
of the involved statements during macro expansion The design of a macro processor generally is
machine independent.
label op operands
name MACRO parameters
:
body
:
MEND
Parameters: the entries in the operand field identify the parameters of the macro instruction . We
require each parameter begins with ‘&’
Body: the statements that will be generated as the expansion of the macro.
Prototype for the macro: The macro name and parameters define a pattern or prototype for the
macro instructions used by the programmer
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
106
Pgm.No.16
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
107
** 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);
}
}
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
108
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.17
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
109
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)
{
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
110
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
111
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
112
VIVA QUESTIONS
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
113
16. What are the important factors considered while designing general purpose macro processors?
• comments
• grouping of statements
• tokens
• syntax used for macro definitions
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
114
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
115
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
116
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
117
Commands to be followed
mount c c:\masm
edit pgmname.asm
masm pgmname.asm
link pgmname.obj
debug pgmname.exe
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
118
Pgm.No.18
16 BIT ADDITION
PROGRAM
DATA SEGMENT
N1 DW 1731H
N2 DW 9212H
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
ADD AX,N2
MOV N3,AX
JNC STOP
INC BX
STOP:
MOV CX,AX
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
119
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT
32 BIT ADDITION
PROGRAM
DATA SEGMENT
LIST DD 12121212H,12121212H
N3 DW ?
N4 DW ?
DATA ENDS
CODE SEGMENT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
120
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
121
16 BIT SUBTRACTION
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
122
MOV N3,AX
JNC STOP
INC BX
STOP:
MOV CX,AX
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT
32 BIT SUBTRACTION
PROGRAM
DATA SEGMENT
LIST DD 12121212H,12121212H
N3 DW ?
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
123
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
124
16 BIT MULTIPLICATION
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
125
END START
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
126
Pgm.No.19
STRING DISPLAY
AIM
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
127
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
128
Pgm.No.20
STRING CONCATENATE
AIM
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
129
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
130
Pgm.No.21
SORTING
AIM
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
131
DEC CL
JNZ UP1
DEC CH
JNZ UP2
INT 3
CODE ENDS
END START
OUTPUT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
132
Pgm.No.22
SEARCHING
AIM
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
133
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
134
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
135
8086
16 bit addition
16 bit subtraction
BCD to hexadecimal conversion
Sorting in ascending order
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
136
Pgm.No.23
ADDITION-16 BIT
AIM
PROGRAM
ADDRESS MNEMONICS
INPUT
ADDRESS VALUE
0500 B5
0501 7A
0550 2A
0551 E6
OUTPUT
ADDRESS VALUE
0600 DF
0601 5F
0602 01
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
137
Pgm.No.24
SUBTRACTION-16 BIT
AIM
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
138
Pgm.No.25
MULTIPLICATION-16 BIT
AIM
PROGRAM
ADDRESS MNEMONICS
2008 MUL BX
2014 HLT
INPUT
ADDRESS VALUE
3003 07
3002 08
3001 04
3000 03
OUTPUT
ADDRESS VALUE
3007 00
3006 1C
3005 35
3004 18
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
139
Pgm.No.26
SORTING-ASCENDING (check descending from the maual)
AIM
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
419 XCHG AL, [SI]
41B INC SI
41C DEC CH
41E JNZ 40F
420 DEC CL
422 JNZ 407
424 HLT
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
140
INPUT
ADDRESS VALUE
500 04
501 F9
502 F2
503 39
504 05
OUTPUT
ADDRESS VALUE
501 05
502 39
503 F2
504 F9
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
141
Pgm.No.26
BCD TO HEXADECIMAL
AIM
Write a program to perform conversion of 8 bit BCD number into hexadecimal number
PROGRAM
ADDRESS MNEMONICS
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
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
142
Pgm.No.27
ASCII TO BCD
AIM
Write a program to perform conversion of ASCII(in hex) value of number to its BCD(decimal)
number
BCD 00 01 02 03 04 05 06 07 08 09
PROGRAM
MOV AL,[2050]
AND AL,0F
MOV [3050],AL
HLT
INPUT
ADDRESS VALUE
2050 39
OUTPUT
ADDRESS VALUE
3050 09
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
143
Pgm.No.28
BCD TO ASCII
AIM
PROGRAM
ADDRESS MNEMONICS
413 HLT
INPUT
ADDRESS VALUE
2000 98
OUTPUT
ADDRESS VALUE
3000 38
3001 39
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET
144
Pgm.No.29
SEARCHING
AIM
PROGRAM
System Software and Microprocessors Lab CSL 331- Lab Manual, Semester 5, CSE Dept., JCET