vtu computer science lab manual.pdf

64

Upload: vinaayaka-vm

Post on 01-Jan-2016

175 views

Category:

Documents


3 download

DESCRIPTION

The programs prescribed by Visvesvaraya Technological University for the course of B.E. in Computer Science and Engineering are solved in this lab manual.

TRANSCRIPT

Page 1: VTU computer science Lab manual.pdf
Page 2: VTU computer science Lab manual.pdf

1

1. General 2-5

2. C++ programs 6-38

3. MASM programs 39-58

4. VH programs 59-63

Page 3: VTU computer science Lab manual.pdf

2

1.Create a document using a suitable word processing package, like MS word, with at least 3

paragraphs and perform the following operations:

*Set left margin 1” and right margin 0.75”

*Center the heading and make it bold, increase the font size.

*Under line the specified words in the document and change them to italics.

*Conduct spell check and correct them suitably.

*Demonstrate use of numbering and bullets.

*Exchange paragraphs 2 and 3 using cut paste facility.

*Put suitable headers and footers.

*Count the number of words and lines.

*Demonstrate use of drawing tools.

*Include suitable logo /emblem/symbol.

Procedure:

*To set left and right margins, go to File in the menu bar, click on page setup.

There, in the margins change left to 1” and right to 0.75”.

Click on ‘OK’ at the bottom.

*To center the heading, select the heading and goto standard tool bar and click on center align

button.

*To make it bold and increase the font size, select it and click on ‘B’ button i.e., ‘bold’ button

which is on the standard tool bar and change the font size using the font size pull down

beside the bold button.

*Select the specific word to be underlined and click on U button on the standard menu bar to

under line it and I button beside it to make the word Italic.

*Select the word and go to Tools in the menu bar click on Spelling and Grammar … Check for

correct word, click it and then click change button.

*Select a sentence, go to Format in the menu bar, click on Bullets and Numbering. Click Bulleted

if you want bullets, click on Numbered if numbering is needed. Then click on the

required style and click OK.

*Select the 2nd paragraph go to Edit in the menu bar and click Cut. Place the cursor where we

are required to put the paragraph. Go to Edit in menu bar and click paste.

*Go to view in menu bar and click Header and Footer. Type the information required in the

header and type the information required in footer, click page number button.

*Go to tools in menu bar and click Word Count… After the numbers and lines are found click

close.

*Click the auto shapes and drawing tools which are found in the Drawing tools bar at the

bottom. Draw the design wanted.

*Go to Insert in the menu bar and click Picture.

*Click on Clipart or Word art and use them.

Page 4: VTU computer science Lab manual.pdf

3

2. Create a formal letter using a suitable word processing package, like MS Word, to place a

purchase order for procurement of books, having the following information.

Example:-

Sl.No. Title of the book Details of the book No. of copies

Author Edition Publisher

1 Engg.Mathematics KCS 6th edition Khanna 1

2 Nisarga Pati 2nd edition Nisarga 1

3 Computers Vinay 1st edition SNVpublishers 1

4 Indian Agriculture VMV 5th edition VTR publishers 2

*Open MS Word, type a letter in an order.

*Click on table in menu bar.

*Click insert, click table…

*Choose number of columns and rows.

*Select the columns to be merged, click merge cells in the small tool box.

*Now select the row / column to be split. Click split the cell, enter the number of row, press OK.

*Enter the letter by clicking save in standard tool bar.

Page 5: VTU computer science Lab manual.pdf

4

3. Create and execute a DOS batch file HELPDOS.BAT which provides on-line help facility for the

following DOS commands – date, time.

*Goto start menu, select Run then type, command and press enter.

*In ‘C:\’ type,

C:\edit HELPDOS.bat(press enter)

*In editor (screen) type,

Echo off

cls

echo ***Information about date ***

echo You can modify the date or press enter

date

echo ***Information about time ***

echo You can modify the time or press enter.

time

echo on

*Go to file and save.

*Go to file and exit.

*Type the file name in command prompt to run it. i.e.,

C:\HELPDOS (press enter)

Page 6: VTU computer science Lab manual.pdf

5

4. Create and execute a DOS batch file MYMOVE.BAT with 2 parameters,

which creates a new directory (given by parameter 1)

and moves the file (given by parameter 2) from the current directory to newly

created directory.

*Click Run in start menu, type command and press enter.

*In command prompt ‘C:\’ type

C:\edit MYMOVE.bat

echo off

cls

mkdir %1

echo ***Directory %1 is created ***

move %2 %1

echo ***The file %2 is moved to %1 directory***

echo on

*Save and exit.

*Again in command prompt type

C:\edit cse.doc

*Type some text, save and exit by clicking save in the file menu and exit which is also found in

file menu.

*In command prompt type,

C:\MYMOVE jmit cse.doc(press enter)

Page 7: VTU computer science Lab manual.pdf

6

1. Given that an EMPLOYEE class contains following members:

Data members : Employee_Number, Employee Name, Basic, DA, IT, Net Salary

Member functions: to read the data, to calculate Net Salary and to print data members.

Write a C++ program to read the data of N employees and compute Net_Salary of each employee.

(Dearness Allowance (DA) = 52% of Basic and Income Tax (IT) = 30% of the gross salary. Net_Salary = Basic + DA - IT)

#include<iostream.h>

#include<conio.h>

class employee

{

int eid;

char ename[20];

float da,IT,basic,net,gross;

public:

void read();

void calculate();

void print();

};

void employee::read()

{

cout<<"enter the name of employee:";

cin>>ename;

cout<<"enter the employee number:";

cin>>eid;

cout<<"enter the basic salary:";

cin>>basic;

}

void employee::calculate()

{

da=0.67*basic;

gross=basic+da;

IT=0.3* gross;

net=gross-IT;

}

void employee::print()

{

cout<<"\n employee name="<<ename;

Page 8: VTU computer science Lab manual.pdf

7

cout<<"\n employee number="<<eid;

cout<<"\n basic="<<basic;

cout<<"\n da="<<da;

cout<<"\n gross="<<gross;

cout<<"\n income tax="<<IT;

cout<<"\n net salary="<<net;

}

void main()

{

employee emp[10];

int i,n;

clrscr();

cout<<"\n enter the number of employee:\n";

cin>>n;

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

{

cout<<"enter the detail of employee"<<i+1<<endl;

emp[i].read();

}

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

emp[i].calculate();

{

cout<<"employee:"<<i+1<<"detail\n";

cout<<"employee:"<<i+1<<"details \n";

emp[i].print();

}

getch();

}

Page 9: VTU computer science Lab manual.pdf

8

2. Define a STUDENT class with USN, Name, and Marks in 3 tests of

a subject. Declare an array of 10 STUDENT objects. Using appropriate functions, find the average of two better

marks for each student. Print the USN, Name, and the average marks of all the students.

# include<iostream.h>

# include<conio.h>

class student

{

int m1,m2,m3;

char name[10],usn[10];

float avg;

public:

void student();

void calculate();

void display();

};

void student::student()

{

cout<<"enter the name of the student " <<endl;

cin>>name;

cout<<"enter the usn of the student " <<endl;

cin>>usn;

cout<<"enter the test marks of the student " <<endl;

cin>>m1>>m2>>m3;

}

void student::calculate()

{

if((m1<=m2)&&(m1<=m3))

avg=(float)(m2+m3)/2;

else

if((m2<=m1)&&(m2<=m3))

avg=(float)(m1+m3)/2;

else

avg=(float)(m1+m2)/2;

}

void student::display()

{

cout<<"the name of the employee is "<<name<<endl;

cout<<"the usn of the employee is "<<usn<<endl;

cout<<"the averge marks of the employee is "<<avg<<endl;

}

Page 10: VTU computer science Lab manual.pdf

9

void main()

{

student stu[10];

int i,n;

clrscr();

cout<<"enter the number of students "<<endl;

cin>>n;

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

{

cout<<"enter the student"<<i+1<<"details"<<endl;

stu[i].student();

stu[i].calculate();

}

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

{

stu[i].display();

}

getch();

}

Page 11: VTU computer science Lab manual.pdf

10

3. Write a C++ program to create a class called COMPLEX and implement the following overloading functions

ADD that return a COMPLEX number.

i. ADD (a, s2) - where a is an integer (real part) and s2 is a

complex number.

ii. ADD (si, s2) - where si and s2 are complex numbers.

#include<iostream.h>

#include<conio.h>

class com

{

int ip, rp;

public:

void getdata();

com add(com c1, com c2);

com add(com c1, int n);

void show (com c);

};

void com:: getdata()

{

cout<<"Enter the value of real and imaginary part \n";

cin>>rp>>ip;

}

void com:: show(com c)

{

cout<< "The complex Number is" <<c.rp<<"+ i" << c.ip<<endl;

}

com com::add(com c1, int x)

{

com c3;

c3.rp=c1.rp+x;

c3.ip=c1.ip;

return(c3);

}

com com:: add(com c1, com c2)

{

com c3;

c3.rp=c1.rp+c2.rp;

c3.ip=c1.ip+c2.ip;

Page 12: VTU computer science Lab manual.pdf

11

return(c3);

}

void main()

{

com c1,c2,c3,c4;

clrscr();

cout<<"Enter the first complex num \n";

c1.getdata();

cout<<"Enter the second complex num \n";

c2.getdata();

cout<<"Sum of the com c1 & com c2 \n";

c3=c3.add(c1,c2);

c3.show(c3);

cout<<"Enter the value of int (real part) \n";

int x;

cin>> x;

c4=c4.add(c1,x);

cout<<"endl";

cout<<" Resultant complex num \n";

c4.show(c4);

getch();

}

Page 13: VTU computer science Lab manual.pdf

12

4. Write a C++ program to create a class called LIST (linked list) with member functions to insert an element at the

front of the list as well as to delete an element from the front of the list. Demonstrate all the functions after creating a

list object.

#include<iostream.h>

#include<conio.h>

#include<process.h>

typedef struct node

{

int info;

struct node *link;

}node;

class list

{

node * first;

public:

list()

{

first=NULL;

}

void insert();

void remove();

void disp();

};

void list::insert()

{

int ele;

node * temp;

temp=new(node);

cout<<"Enter the element\n";

cin>>ele;

temp->info=ele;

temp->link=NULL;

if(first==NULL)

first=temp;

else

{

temp->link=first;

first=temp;

}

}

void list::remove()

{

node *temp;

if(first==NULL)

cout<<"Empty list\n";

else

{

temp=first;

cout<<"Deleted element is\n "<<temp->info;

first=first->link;

delete(temp);

}

}

void list :: disp()

{

node *temp;

temp=first;

Page 14: VTU computer science Lab manual.pdf

13

if(first==NULL)

cout<<"Empty list\n";

else

while(temp!=NULL)

{

cout<<temp->info<<"-->";

temp=temp->link;

}

}

void main()

{

list l;

int ch;

clrscr();

do

{

cout<<"^^^^^ ^^^^^^^^\n";

cout<<"1:INSERT\n 2:DELETE\n 3:DISPLAY\n 4:EXIT\n";

cout<<"***** *******\n";

cout<<"Enter your choice";

cin>>ch;

switch(ch)

{

case 1:l.insert();

break;

case 2: l.remove();

break;

case 3: l.disp();

break;

case 4: exit(1);

}

}

while(ch!=4);

getch();

}

Output:-

/*1:INSERT

2:DELETE

3:DISPLAY

4:EXIT

Enter your choice3

11-->10-->

1:INSERT

2:DELETE

3:DISPLAY

4:EXIT

Enter your choice2

Deleted element is

11

1:INSERT

2:DELETE

3:DISPLAY

4:EXIT

Enter your choice3

10-->

1:INSERT

2:DELETE

Page 15: VTU computer science Lab manual.pdf

14

3:DISPLAY

4:EXIT

Enter your choice

4*/

Page 16: VTU computer science Lab manual.pdf

15

5. Write a C++ program to create a template function for Quick sort and demonstrate sorting of integers and

doubles.

#include<iostream.h>

#include<conio.h>

template<class t>

void readarr(t a[],int n)

{

cout<<"\nenter the element\n";

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

{

cin>>a[i];

}

}

template<class t1>

void printarr(t1 a[],int n)

{

cout<<"\narray element\n";

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

cout<<" "<<a[i];

}

template<class t>

void qsort(t a[],int lb,int ub)

{

if(lb<ub)

{

int flag=1,i,j;

t key;

i=lb+1;

j=ub;

key=a[lb];

while(flag)

{

while(a[i]<key && i<ub)

i++;

while(a[j]>key && j>lb)

j--;

if(i<j)

swap(a[i],a[j]);

else

flag=0;

}

swap(a[lb],a[j]);

qsort(a,lb,j-1);

qsort(a,j+1,ub);

}

}

template<class t>

void swap(t &x,t &y)

{

t temp;

temp=x;

x=y;

y=temp;

}

void main()

Page 17: VTU computer science Lab manual.pdf

16

{

int a[15],n;

double d[15];

clrscr();

cout<<"\nenter the limit: ";

cin>>n;

cout<<"\nenter the int array element\n";

readarr(a,n);

cout<<"\nenter the double array element\n";

readarr(d,n);

cout<<"\nbefore sorting:";

printarr(a,n);

printarr(d,n);

qsort(a,0,n-1);

qsort(d,0,n-1);

cout<<"\nafter sorting:";

printarr(a,n);

printarr(d,n);

getch();

}

Page 18: VTU computer science Lab manual.pdf

17

6. Write a C++ program to create a class called STACK using an

array of integers. Implement the following operations by overloading the operators + and -.

i. sl=sl + element; where si is an object of the class

STACK and element is an integer to be pushed on to top of

the stack.

ii. sl=sl-; where si is an object of the class STACK and -

operator pops the element.

Handle the STACK Empty and STACK Full conditions. Also display the contents of the stack after each operation, by

overloading the operator «.

#include<iostream.h>

#include<conio.h>

#include<process.h>

#define SIZE 10

class stack

{

int arr[SIZE],top;

public:

stack()

{

top=-1;

}

friend stack operator +(stack,int);

friend stack operator -(stack);

friend ostream &operator <<(ostream &stream,stack)

};

stack operator +(stack s,int ele)

{

if(s.top==(SIZE-1))

{

cout<<"\nstack overflow\n";

}

else

{

s.arr[++s.top]=ele;

}

return(s);

}

stack operator -(stack s)

{

if(s.top==-1)

{

cout<<"\nstack empty\n";

}

else

{

int ele=s.arr[s.top];

--s.top;

cout<<"\npopped element is "<<ele<<endl;

}

return s;

}

ostream &operator <<(ostream &stream,stack s)

{

stream<<endl;

Page 19: VTU computer science Lab manual.pdf

18

cout<<"\nelements of stack\n";

if(s.top==-1)

{

cout<<"\nstack is empty\n";

}

else

{

for(int i=s.top;i>=0;i--)

stream<<endl<<s.arr[i];

}

}

void main()

{

clrscr();

stack s;

int ele,ch;

do

{

cout<<"\nstack operation\n";

cout<<"\n1.push\n2.pop\n3.exit\n";

cout<<"\nenter your choice\n";

cin>>ch;

switch(ch)

{

case 1:cout<<"enter the element";

cin>>ele;

s=s+ele;

cout<<s;

break;

case 2:cout<<endl;

s=-s;

cout<<s;

break;

case 3:exit(1);

}

}

while(ch!=3);

getch();

}

Page 20: VTU computer science Lab manual.pdf

19

7. Write a C+-*- program to create a class called DATE. Accept two

valid dates in the form dd/mm'yy. Implement the following operations by overloading the operators * and -

. After every operation display the results by overloading the operator «.

i. no_of_days = dl - d2; where dl and d2 are DATE objects, dl >-d2 and no_of_days

is an integer.

ii. d2 = dl + no_of days; where dl is a DATE object and no_of days is an integer.

#include<iostream.h>

#include<conio.h>

#include<math.h>

const intsize=12;

class date

{

private:

int dd,mm,yy;

protected:

int mon[13];

public:

date()

{

mon[1]=31;mon[2]=28;mon[3]=31;mon[4]=30;mon[5]=31;mon[6]=30;

mon[7]=31;mon[8]=31;mon[9]=30;mon[10]=31;mon[11]=30;mon[12]=31;

}

void getdate()

{

cout<<"\nenter the year:";

cin>>yy;

if(yy%4==0)

mon[2]=29;

X:

cout<<"\nenter the month:";

cin>>mm;

if(mm>12||mm<1)

{

cout<<"\ninvalid month"<<endl;

goto X;

}

Y:

cout<<"\nenter the date:";

cin>>dd;

if(dd>mon[mm])

{

cout<<"\ninvalid date"<<endl;

goto Y;

}

}

friend int operator -(date,date);

friend date operator +(int,date);

friend ostream& operator <<(ostream &,date);

};

date operator +(int day,date d)

{

for(int i=1;i<=day;i++)

{

d.dd++;

if(d.dd>d.mon[d.mm])

Page 21: VTU computer science Lab manual.pdf

20

{

d.dd=1;

++d.mm;

}

if(d.mm>12)

{

d.mm=1;

++d.yy;

}

}

return(d);

}

int operator -(date d1,date d2)

{

int days=0;

while((d1.dd!=d2.dd)||(d1.mm!=d2.mm)||(d1.yy!=d2.yy))

{

d2.dd++;

days++;

if(d2.dd>d2.mon[d2.mm])

{

d2.dd=1;

d2.mm++;

}

if(d2.mm>12)

{

d2.mm=1;

d2.yy++;

}

}

return(abs(days));

}

ostream & operator <<(ostream & os,date d)

{

os<<"date is:"<<d.dd<<"-"<<d.mm<<"-"<<d.yy<<endl;

return os;

}

void main()

{

int d;

date d1,d2,d3;

clrscr();

cout<<"\n enter the first date:"<<endl;

d1.getdate();

cout<<d1;

cout<<"\n enter the second date:"<<endl;

d2.getdate();

cout<<d2;

cout<<"\n difference between the date is:";

int days=d1-d2;

cout<<days<<endl;

cout<<"\n enter the days to be added:";

cin>>d;

d3=d+d1;

cout<<"\n after adding the dateis:"<<d3<<endl;

getch();

}

Page 22: VTU computer science Lab manual.pdf

21

8. Write a C+ + program to create a class called MATRIX using a two-

dimensional array of integers. Implement the following operations by overloading the operator

= = which checks the compatibility of two matrices ml and m2 to be added and subtracted. Perform the

addition and subtraction by overloading the operators + and - respectively. Display the results (sum matrix m3 and

difference matrix m4) by overloading the operator «.

if(ml = = m2)

{

m3 = mi -1- m2:

m4 = ml - m2;

i i

else

display error

#include<iostream.h>

#include<conio.h>

#define SIZE 10

class matrix

{

int a[SIZE][SIZE],r,c;

public:

matrix()

{

r=c=0;

}

matrix(int p,int q)

{

r=p;

c=q;

}

void readmat();

friend matrix operator +(matrix,matrix);

friend matrix operator -(matrix,matrix);

friend ostream & operator <<(ostream &os,matrix);

int operator ==(matrix);

};

void matrix :: readmat()

{

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

for(int j=0;j<c;j++)

cin>>a[i][j];

}

ostream & operator <<(ostream & os,matrix m)

{

cout<<"\nelement of the matrix\n";

for(int i=0;i<m.r;i++)

{

for(int j=0;j<m.c;j++)

os <<m.a[i][j]<<"\t";

cout<<endl;

}

return (os);

}

matrix operator +(matrix m1,matrix m2)

{

matrix m3(m1.r,m2.c);

for(int i=0;i<m1.r;i++)

for(int j=0;j<m1.c;j++)

Page 23: VTU computer science Lab manual.pdf

22

m3.a[i][j]=m1.a[i][j]+m2.a[i][j];

return m3;

}

int matrix :: operator ==(matrix m)

{

if(m.r==r && m.c==c)

return 1;

else

return 0;

}

matrix operator -(matrix m1,matrix m2)

{

matrix m3(m1.r,m2.c);

for(int i=0;i<m1.r;i++)

for(int j=0;j<m1.c;j++)

m3.a[i][j]=m1.a[i][j]-m2.a[i][j];

return m3;

}

void main()

{

int r1,r2,c1,c2;

clrscr();

cout<<"\nenter the order of 1st matrix\n";

cin>>r1>>c1;

cout<<"\nenter the order of 2nd matrix\n";

cin>>r2>>c2;

matrix m1(r1,c1),m2(r2,c2);

cout<<"\nenter the elements of 1st matrix\n:";

m1.readmat();

cout<<"\nenter the elements of 2nd matrix\n:";

m2.readmat();

if(m1==m2)

{

matrix m3=m1+m2;

matrix m4=m1-m2;

cout<<"\nsum of 2 matrices is +"<<m3;

cout<<"\ndifference of matrices is ="<<m4;

}

else

cout<<"\ninvalid row & column......TRY AGAIN\n";

getch();

}

Page 24: VTU computer science Lab manual.pdf

23

9. Write a C++ program to create a class called OCTAL, which has the characteristics of an octal number. Implement the

following operations by writing an appropriate constructor and an overloaded operator +.

i. OCTAL h = x ; where x is an integer

ii. int y = h + k : where h is an OCTAL object and k is an

integer.

Display the OCTAL result by overloading the operator «. Also display the values of h and y.

#include<iostream.h>

#include<conio.h>

#include<math.h>

class octal

{

int num, x;

public: octal ()

{

num=0;

}

octal(int x)

{

num=convert(x);

}

int convert(int);

int operator +(int);

friend ostream & operator<< (ostream &,octal);

};

int octal::convert(int x)

{

int r,i=0,n=0;

while(x!=0)

{

r=x%8;

x=x/8;

n=n+r * pow(10,i);

i++;

}

return (n);

}

int octal::operator +(int k)

{

k=convert(k);

return(k+num);

}

ostream & operator <<(ostream & os, octal ot)

{

os<<"octal number is \n";

os<< ot.num<<endl;

return(os);

}

void main()

{

int n,k;

clrscr();

cout<<"Enter the value of n \n";

cin>>n;

octal h(n);

cout<<"Enter the number to be added\n";

Page 25: VTU computer science Lab manual.pdf

24

cout<<h;

cout<<"Enter one integer n: \n";

cin>>k;

int y=h+k;

cout<<"addition of two octal no is:\n"<<y;

getch();

}

/*********************OUTPUT****************

Enter the value of n

15

Enter the number to be added

octal number is

17

Enter one integer n:

3

addition of two octal no is:

20

*************************************************/

Page 26: VTU computer science Lab manual.pdf

25

10. Write a C++ program to create a class called QUEUE with

member functions to add an element and to delete an element from

the queue. Using these member functions, implement a queue of

integers and doubles. Demonstrate the operations by displaying the

contents of the queue after every operation:

#include<iostream.h>

#include<conio.h>

#include<process.h>

const int size =15;

template <class t>

class queue

{

t q[size],ele;

int f,r;

public: queue()

{

f=r=-1;

}

void add();

void remove();

void display();

};

template <class t>

void queue<t>::add()

{

cout<<"Enter the element\n";

cin>>ele;

if(r>=size-1)

{

cout<<"queue is full\n";

getch();

}

else

{

r++;

q[r]=ele;

if(f==-1)

f=0;

}

}

template<class t>

void queue<t>::remove()

{

if(f<0)

{

cout<<"queue is empty\n";

getch();

}

else

{

cout<<"deleted item is: "<<q[f];

if(f==r)

f=r=-1;

else

f++;

}

Page 27: VTU computer science Lab manual.pdf

26

}

template <class t>

void queue<t>::display()

{

if(f<0)

cout<<"queue is empty \n";

{

cout<<"\ncontents of the queue:\n";

for(int i=f;i<=r;i++)

cout<<" "<<q[i];

}

}

void main()

{

queue<int> q1;

queue<double> q2;

int ch;

clrscr();

do

{

cout<<"\n 1:add\n 2:delete\n 3:display\n 4:add d\n 5:delete d\n 6:display d\n 7: exit\n";

cout<<"Enter ur choice\n";

cin>>ch;

switch(ch)

{

case 1: q1.add();

q1.display();

break;

case 2: q1.remove();

q1.display();

break;

case 3: q1.display();

getch();

break;

case 4: q2.add();

q2.display();

break;

case 5: q2.remove();

q2.display();

break;

case 6: q2.display();

getch();

break;

case 7: exit(1);

}

}

while(ch!=7);

getch();

}

Page 28: VTU computer science Lab manual.pdf

27

/**************************************

OUTPUT:

3:display

4:add d

5:delete d

6:display d

7: exit

Enter ur choice

4

Enter the element

5

contents of the queue:

5

1:add

2:delete

3:display

4:add d

5:delete d

6:display d

7: exit

Enter ur choice

4

Enter the element

6

contents of the queue:

5 6

1:add

2:delete

3:display

4:add d

5:delete d

6:display d

7: exit

Enter ur choice

5

deleted item is: 5

contents of the queue:

6

1:add

2:delete

3:display

4:add d

5:delete d

6:display d

7: exit

Enter ur choice

6

contents of the queue:

6

**************************************/

Page 29: VTU computer science Lab manual.pdf

28

11. Write a C++ program to create a class called DL1ST (Doubly

Linked List) with member functions to insert a node at a specified

position and delete a node from a specified position of the list.

Demonstrate the operation by displaying the contents of the list after

every operation.

#include<iostrem.h>

#include<conio.h>

#include<process.h>

typedef struct node

{

int info;

struct node *lptr,*rptr;

}node;

class dlist

{

private : node *first;

public :dlist()

{

first=null;

}

void insert(int ele,int pos)

{

node *temp;

temp=new node();

temp->info=ele;

temp->lptr=temp->rptr=null;

if(pos==1)

{

if(first==null)

first=temp;

else

{

temp->rptr=first;

first->lptr=temp;

first=temp;

}

}

else

{

int i=1;

node *prev;

prev=first;

while(i!=(pos-1)&&(prev!=null))

{

i++;

prev=prev->rptr;

}

if(prev==null)

{

cout<<"invalid position."<<endl;

getch();

}

else

{

if((prev->rptr==null)&&(i==(pos-1)))

{

prev->rptr=temp;

Page 30: VTU computer science Lab manual.pdf

29

temp->lptr=prev;

}

else

{

temp->rptr=prev->rptr;

(prev->rptr)->lptr=temp;

prev->rptr=temp;

temp->lptr=prev;

}

}

}

}

int remov(int pos)

{

node *temp;

if(first==null)

{

cout<<"empty list."<<endl;

getch();

}

else

{

temp=first;

if(pos==1)

{

first=null;

delete(temp);

}

else

{

int i=1;

node *prev=first;

while((i!=pos)&&(prev!=null))

{

i++;

prev=prev->rptr;

}

if(prev==null)

{

cout<<"invalid position."<<endl;

getch();

}

else

{

if((prev->rptr==null)&&(i=pos))

(prev->lptr)->rptr=null;

else

{

temp=prev;

(prev->rptr)->lptr=prev->lptr;

(prev->lptr)->rptr=prev->rptr;

delete(temp);

}

}

}

}

return(pos);

}

Page 31: VTU computer science Lab manual.pdf

30

void disp()

{

node *temp=first;

if(first==null)

{

cout<<"empty list."<<endl;

getch();

}

else

{

temp=first;

while(temp!=null)

{

cout<<temp->info<<"->";

temp=temp->rptr;

}

cout<<"null"<<endl;

}

}

};

void main()

{

dlist d;

int ele,pos,ch;

clrscr();

do

{

cout<<"1.insert,2.remove,3.display,4.exit"<<endl;

cout<<"enter the choice:";

cin>>ch;

switch(ch)

{

case 1:cout<<"enter the element &position:";

cin>>ele>>pos;

d.insert(ele,pos);

break;

case 2:cout<<"enter the position:";

cin>>pos;

d.remove(pos);

break;

case 3:d.disp();

break;

case 4:exit(0);

}

}

while(ch<=4);

getch();

}

Page 32: VTU computer science Lab manual.pdf

31

12. Write a C++ program to create a class called STUDENT with

data members USN, Name and Age. Using inheritance, create the

classes UGSTUDENT and PGSTUDENT having fields as Semester,

Fees and Stipend. Enter the data for at least 5 students.

Find the semester wise average age for all UG and PG students separately.

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class student

{

protected:

char name[20];

int usn;

public:

int age;

void getdata()

{

cout<<"\nenter the name:";

cin>>name;

cout<<"\nenter the usn:";

cin>>usn;

cout<<"\nenter the age:";

cin>>age;

}

void disp()

{

cout<<"usn:"<<usn<<endl ;

cout<<"name:"<<name<<endl;

cout<<"age:"<<age<<endl;

}

};

class ugstudent : public student

{

protected:int fee,stipend;

public:int sem;

void getug()

{

getdata();

cout<<"enter the semester:";

cin>>sem;

cout<<"enter the fees:";

cin>>fee;

cout<<"enter the stipend:";

cin>>stipend;

}

void dispug()

{

disp();

cout<<"semester:"<<sem<<endl;

cout<<"fees:"<<fee<<endl;

cout<<"stipend:"<<stipend<<endl;

}

};

class pgstudent : public student

{

protected:int fee,stipend;

public:int sem;

Page 33: VTU computer science Lab manual.pdf

32

void getpg()

{

getdata();

cout<<"enter the semester:";

cin>>sem;

cout<<"enter the fees:";

cin>>fee;

cout<<"enter the stipend:";

cin>>stipend;

}

void disppg()

{

disp();

cout<<"semester:"<<sem<<endl;

cout<<"fees:"<<fee<<endl;

cout<<"stipend:"<<stipend<<endl;

}

};

void main()

{

pgstudent pg[5];

ugstudent ug[5];

int i,j,n,m,ugage[9],pgage[5],sum,count;

clrscr();

cout<<"enter the no. of ug students:";

cin>>n;

cout<<"enter the student details:"<<endl;

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

ug[i].getug();

cout<<"enter the no. of pg students:";

cin>>m;

cout<<"enter the student details:"<<endl;

for(i=0;i<m;i++)

pg[i].getpg();

cout<<"________UG STUDENT DETAILS________"<<endl;

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

ug[i].dispug();

getch();

cout<<"________PG STUDENT DETAILS________"<<endl;

for(i=0;i<m;i++)

pg[i].disppg();

getch();

for(i=1;i<=9;i++)

{

sum=count=0;

for(j=0;j<n;j++)

{

if(i==ug[j].sem)

{

sum=sum+ug[j].age;

count++;

}

}

if(count>0)

ugage[i]=sum/count;

}

for(i=1;i<=4;i++)

{

Page 34: VTU computer science Lab manual.pdf

33

sum=count=0;

for(j=0;j<n;j++)

{

if(i==pg[j].sem)

{

sum=sum+pg[j].age;

count++;

}

}

if(count>0)

pgage[i]=sum/count;

}

cout<<"\naverage age of UG students sem wise="<<endl;

for(i=1;i<=8;i++)

cout<<"\nsemester"<<i<<"="<<ugage[i]<<endl;

getch();

cout<<"\naverage age of PG students sem wise="<<endl;

for(i=1;i<=4;i++)

cout<<"\nsemester"<<i<<"="<<pgage[i]<<endl;

getch();

}

Page 35: VTU computer science Lab manual.pdf

34

13. Write a C++ program to create a class called STRING and

implement the following operations. Display the results after every

operation by overloading the operator «.

i. STRING s1 ="VTU"

ii. STRING s2 = "BELGAUM"

iii. STIRNG s3 = si + s2 ; (Use copy constructor).

#include<iostream.h>

#include<string.h>

#include<conio.h>

class string

{

char str[20];

public:

string()

{

strcpy(str,"");

}

string(char *s)

{

strcpy(str,s);

}

string operator +(string ss)

{

string temp;

strcpy(temp.str, str);

strcat(temp.str, ss.str);

return temp;

}

string operator << (string ss)

{

cout<<ss.str;

return 0;

}

};

void main()

{

clrscr();

string s1="VTU";

string s2 ="BELGAUM";

cout<<"string s1:";

s1<<s1<<"\n";

cout<<"string s2:";

s2<<s2<<"\n";

string s3=s1+s2;

s3<<s3;

getch();

}

Page 36: VTU computer science Lab manual.pdf

35

14. Write a C++ program to create a class called BIN_TREE (

Binary tree) with member functions to perform inorder, preorder and

postorder traversals. Create a B1N_TREE object and demonstrate

the traversals.

#include<iostream.h>

#include<conio.h>

typedef struct node

{

int info;

struct node *left,*right;

}node;

class bin_tree

{

private:node *root;

public:bin_tree()

{

root=NULL;

}

node* create_tree()

{

int ele,n,i;

cout<<"enter the no of elements:";

cin>>n;

for(i=1;i<=n;i++)

{

cout<<"enter the element:";

cin>>ele;

root=insert(root,ele);

}

return(root);

}

node* insert(node* root,int ele)

{

node *prev,*temp;

int flag;

temp=new node;

temp->info=ele;

temp->left=temp->right=NULL;

if(root==NULL)

root=temp;

else

{

node* next;

flag=1;

prev=root;

while(prev!=NULL)

{

next=prev;

if(ele<prev->info)

prev=prev->left;

else

if(ele>prev->info)

prev=prev->right;

else

{

flag=0;

break;

Page 37: VTU computer science Lab manual.pdf

36

}

}

if(flag==0)

cout<<"duplicate element"<<endl;

else

{

if(ele<next->info)

next->left=temp;

else

next->right=temp;

}

}

return(root);

}

void inorder(node* temp)

{

if(temp!=NULL)

{

inorder(temp->left);

cout<<"\t"<<temp->info;

inorder(temp->right);

}

}

void preorder(node* temp)

{

if(temp!=NULL)

{

cout<<"\t"<<temp->info;

preorder(temp->left);

preorder(temp->right);

}

}

void postorder(node* temp)

{

if(temp!=NULL)

{

postorder(temp->left);

postorder(temp->right);

cout<<"\t"<<temp->info;

}

}

};

void main()

{

node *rt;

bin_tree bt;

clrscr();

rt=bt.create_tree();

cout<<endl<<"inorder traversal:";

bt.inorder(rt);

cout<<endl<<"preorder traversal:";

bt.preorder(rt);

cout<<endl<<"postorder traversal:";

bt.postorder(rt);

getch();

}

Page 38: VTU computer science Lab manual.pdf

37

15. Write a C++ program to create a class called EXPRESSION.

Using appropriate member functions convert a given valid Infix

expression into Postfix form. Display the Infix and Postfix

expressions.

#include<iostream.h>

#include<conio.h>

#include<string.h>

class expression

{

int pos,top;

char infix[30],postfix[30],stack[30];

public:expression()

{

top=-1;

pos=0;

}

void getinfix()

{

cout<<"enter the infix expression:";

cin>>infix;

}

void convert();

void push(char ch)

{

stack[++top]=ch;

}

char pop()

{

return(stack[top--]);

}

void disp()

{

cout<<"infix expression is:"<<infix<<endl;

cout<<"postfix expressiion is:"<<postfix<<endl;

}

int precedence(char ch);

};

int expression:: precedence(char ch)

{

switch(ch)

{

case '#':return(-1);

case '(':

case ')':return (0);

case '+':

case '-':return(1);

case '*':

case '/':return(2);

case '^':return(3);

Page 39: VTU computer science Lab manual.pdf

38

}

return(0);

}

void expression:: convert()

{

int i=0;

push('#');

while(infix[i]!='\0')

{

switch(infix[i])

{

case '(':push(infix[i]);

break;

case ')':char temp=pop();

while(temp!='(')

{

postfix[pos++]=temp;

temp=pop();

}

break;

case '+':

case '-':

case '*':

case '/':

case '^':while(precedence(stack[top])>=precedence(infix[i]))

postfix[pos++]=pop();

push(infix[i]);

break;

default:postfix[pos++]=infix[i];

break;

}

i++;

}

while(top>0)

postfix[pos++]=pop();

postfix[pos]='\0';

}

void main()

{

char ch;

clrscr();

expression exe;

exe.getinfix();

exe.convert();

exe.disp();

getch();

Page 40: VTU computer science Lab manual.pdf

39

-:ASCII:-

.MODEL SMALL

.DATA

MSG DB"ENTER THE ALPHANUMERIC CHARACTER:$"

.CODE

MOV AX,@DATA

MOV DS,AX

LEA DX,MSG

MOV AH,09H

INT 21H

MOV AH,01H

INT 21H

PUSH AX

MOV AX,0B800H

MOV ES,AX

MOV DI,00

MOV AX,0720H

MOV CX,25*80

REP STOSW

MOV DI,2000

POP AX

MOV BL,AL

MOV CL,04

SHR AL,CL

CMP AL,0AH

JL L1

ADD AL,07H

L1:ADD AL,30H

STOSB

INC DI

MOV AL,BL

AND AL,0FH

CMP AL,0AH

JL L2

Page 41: VTU computer science Lab manual.pdf

40

ADD AL,07H

L2:ADD AL,30H

STOSB

MOV AH,4CH

INT 21H

END

Page 42: VTU computer science Lab manual.pdf

41

-:Binary search:-

.MODEL SMALL

.DATA

ARR DB 10h,11h,12h,13h,14h

LEN DB ($-ARR)

MSG1 DB "SEARCH element IS FOUND AT"

RES DB " POSITION$"

MSG2 DB "ELEMENT NOT FOUND$"

SR EQU 14H

.CODE

MOV AX,@DATA

MOV DS,AX

MOV CL,SR

MOV BL,00

MOV DL,LEN

AGAIN: CMP BL,DL

JA FAIL

MOV AL,BL

ADD AL,DL

SHR AL,01

MOV AH,00

MOV SI,AX

CMP CL,ARR[SI]

JAE BIGGER

DEC AL

MOV DL,AL

JMP AGAIN

BIGGER: JE SUCCESS

INC AL

MOV BL,AL

JMP AGAIN

SUCCESS:ADD AL,01

ADD AL,30H

MOV RES,AL

Page 43: VTU computer science Lab manual.pdf

42

LEA DX,MSG1

MOV AH,09H

INT 21H

JMP LAST

FAIL: LEA DX,MSG2

MOV AH,09H

INT 21H

LAST: MOV AH,4CH

INT 21H

END

Page 44: VTU computer science Lab manual.pdf

43

-:CREATE AND DELETE FILE:-

PRINT MACRO MSG

LEA DX,MSG

MOV AH,09H

INT 21H

ENDM

.MODEL SMALL

.DATA

MSG1 DB 0AH,0DH,"NTR FNAME FR CRTN$"

MSG2 DB 0AH,0DH,"NTR FNAME FR DLN$"

MSG3 DB 0AH,0DH,"ERROR$"

MSG4 DB 0AH,0DH,"FILE CRTD SUCC$"

MSG5 DB 0AH,0DH,"DELTD SUCC$"

FNAME DB 10 DUP(0)

.CODE

MOV AX,@DATA

MOV DS,AX

PRINT MSG1

LEA SI,FNAME

UP1:MOV AH,01H

INT 21H

CMP AL,0DH

JE GET1

MOV [SI],AL

INC SI

JMP UP1

GET1:MOV AH,3CH

LEA DX,FNAME

INT 21H

JNC L1

PRINT MSG3

JMP L2

L1:PRINT MSG4

L2:PRINT MSG2

LEA DI,FNAME

Page 45: VTU computer science Lab manual.pdf

44

UP2:MOV AH,01H

INT 21H

CMP AL,0DH

JE GET2

MOV [DI],AL

INC DI

JMP UP2

GET2:MOV AH,41H

LEA DX,FNAME

INT 21H

JC L3

PRINT MSG5

JMP LAST

L3:PRINT MSG3

LAST: MOV AH,4CH

INT 21H

END

Page 46: VTU computer science Lab manual.pdf

45

-:MOVE CURSOR:-

.MODEL SMALL

.DATA

MSG1 DB "NTR X CO $"

MSG2 DB 0AH,0DH, "NTR Y CO $"

.CODE

MOV AX,@DATA

MOV DS,AX

LEA DX,MSG1

MOV Ah,09H

INT 21H

MOV AH,01H

INT 21H

SUB AL,30H

MOV CL,AL

LEA DX,MSG2

MOV AH,09H

INT 21H

MOV AH,01H

INT 21H

SUB AL,30H

MOV CH,AL

MOV AH,02H

MOV DH,CL

MOV DL,CH

INT 10H

MOV AH,4CH

INT 21H

END

Page 47: VTU computer science Lab manual.pdf

46

-:DUC:-

.MODEL SMALL

.DATA

.CODE

MOV AL,30H

L2: MOV DL,AL

MOV AH,02H

INT 21H

PUSH AX

MOV BL,30H

L1: MOV DL,BL

MOV AH,02H

INT 21H

INC BL

CALL DELAY

MOV AH,03H

INT 10H

MOV AH,02H

MOV DL,01H

INT 10H

CMP BL,39H

JLE L1

MOV AH,02H

MOV DL,00H

INT 10H

POP AX

INC AL

CMP AL,39H

JLE L2

MOV AH,4CH

INT 21H

DELAY PROC

PUSH AX

MOV CX,0FFFFH

B2: MOV AX,0FFFH

B1: DEC AX

JNZ B1

LOOP B2

POP AX

RET

DELAY ENDP

END

Page 48: VTU computer science Lab manual.pdf

47

-:FACTORIAL:-

.MODEL SMALL

.DATA

NUM DW 0004H

RES DW ?

.CODE

MOV AX,@DATA

MOV DS,AX

MOV AX,01H

MOV CX,NUM

CMP CX,00H

JE L1

CALL FACT

L1: moV RES,AX

MOV AH,4CH

INT 21H

FACT PROC

CMP CX,01

JE L2

PUSH CX

DEC CX

CALL FACT

POP CX

MUL CX

RET

L2: MOV AX,01H

RET

FACT ENDP

END

Page 49: VTU computer science Lab manual.pdf

48

-:FIBUNOCCI:-

.MODEL SMALL

.DATA

FIB DB 30 DUP(00)

.CODE

MOV AX, @DATA

MOV DS ,AX

MOV AL,00

MOV SI,00

MOV CX,06

MOV FIB[SI],AL

INC SI

DEC CX

MOV BL,01

MOV FIB[SI],BL

DEC CX

L1: INC SI

ADD AL,BL

MOV BL,FIB[SI-1]

MOV FIB[SI],AL

LOOP L1

MOV AH,4CH

INT 21H

END

Page 50: VTU computer science Lab manual.pdf

49

-:MACRO:-

.MODEL SMALL

.DATA

ARR DB 10 DUP (0)

.CODE

INCLUDE C:\MASM\READ.MAC

INCLUDE C:\MASM\PRINTKB.MAC

MOV DS,AX

MOV SI,00

L2:READ

CMP AL,0DH

JE L1

MOV ARR[SI],AL

INC SI

JMP L2

L1:MOV DL,0AH

MOV AH,02H

INT 21H

MOV CX,SI

MOV SI,00

L3:MOV DL,ARR[SI]

PRINTKB

INC SI

LOOP L3

MOV AH,4CH

INT 21H

END

Page 51: VTU computer science Lab manual.pdf

50

-:NAME:-

.MODEL SMALL

.DATA

STR1 DB 50

DB 00

DB 50 DUP(0)

MSG DB "WAT S UR NAME:$"

.CODE

MOV AX,@DATA

MOV DS,AX

LEA DX,MSG

MOV AH,09H

INT 21H

LEA DX,STR1

MOV AH,0AH

INT 21H

MOV AX,0B800H

MOV ES,AX

MOV DI,00

MOV AX,0720H

MOV CX,25*80

REP STOSW

MOV AH,02H

MOV DH,12

MOV DL,25

INT 10H

LEA DX,MSG

MOV AH,09H

INT 21H

LEA SI,STR1+2

MOV CL,STR1+1

L1:MOV DL,[SI]

MOV AH,02H

INT 21H

INC SI

LOOP L1

MOV AH,4CH

INT 21H

END

Page 52: VTU computer science Lab manual.pdf

51

-:NCR:-

.MODEL SMALL

.DATA

N DW 05h

R DW 03h

NCR DW ?

.CODE

MOV AX,@DATA

MOV DS,AX

MOV AX,N

CMP AX,R

JE L1

JC L2

MOV BX,N

CALL FACT

MOV SI,AX

MOV BX,R

CALL FACT

MOV DI,AX

MOV BX,N

SUB BX,R

CALL FACT

MUL DI

MOV BX,AX

MOV AX,SI

DIV BX

MOV NCR,AX

JMP LAST

L1: MOV NCR,01

JMP LAST

L2: MOV NCR,00

LAST: MOV AH,4CH

INT 21H

FACT PROC

MOV AX,01

AGAIN: MUL BX

DEC BX

JNZ AGAIN

RET

FACT ENDP

END

Page 53: VTU computer science Lab manual.pdf

52

-:PALINDROME:-

.MODEL SMALL

.DATA

STR1 DB 10 DUP(0)

STR2 DB 10 DUP(0)

MSG DB "NTR THE STRNG $"

MSG1 DB 0AH,0DH, " STRNG IS PALINDRM $"

MSG2 DB 0AH,0DH, " STRNG IS NT PALNDRM $"

.CODE

MOV AX,@DATA

MOV DS,AX

MOV ES,AX

LEA DX,MSG

MOV AH,09H

INT 21H

L2:MOV AH,01H

INT 21H

CMP AL,0DH

JE L1

MOV STR1[SI],AL

INC SI

JMP L2

L1:MOV CX,SI

DEC SI

MOV DX,CX

MOV DI,00

L3:MOV AL, STR1[SI]

MOV STR2[DI],AL

DEC SI

INC DI

LOOP L3

MOV SI,00

MOV DI,00

MOV CX,DX

L6:MOV AL,STR1[SI]

CMP AL,STR2[DI]

JNE L4

INC SI

INC DI

LOOP L6

LEA DX,MSG1

MOV AH,09H

INT 21H

JMP LAST

L4:LEA DX,MSG2

MOV AH,09H

INT 21H

LAST:MOV AH,4CH

Page 54: VTU computer science Lab manual.pdf

53

INT 21H

END

Page 55: VTU computer science Lab manual.pdf

54

-:SORT:-

.MODEL SMALL

.DATA

ARR DB 72H,56H,10H,42H

LEN DW ($-ARR)

.CODE

MOV AX,@DATA

MOV DS,AX

MOV AH,01H

INT 21H

CMP AL,31H

JE L5

MOV BX,LEN

DEC BX

L3:MOV CX,BX

MOV SI,00

L2:MOV AL,ARR[SI]

INC SI

CMP AL,ARR[SI]

JB L1

XCHG AL,ARR[SI]

MOV ARR[SI-1],AL

L1:LOOP L2

DEC BX

JNZ L3

JMP LAST

L5:MOV BX,LEN

DEC BX

L6:MOV CX,BX

MOV SI,00

L7:MOV AL,ARR[SI]

INC SI

CMP AL,ARR[SI]

JA L8

XCHG AL,ARR[SI]

MOV ARR[SI-1],AL

L8:LOOP L7

DEC BX

JNZ L6

LAST:MOV AX,4CH

INT 21H

END

FS

Page 56: VTU computer science Lab manual.pdf

55

-:STREQ:-

.MODEL SMALL

.DATA

STR1 DB 10

DB 00

DB 10 DUP(?)

STR2 DB 10

DB 00

DB 10 DUP(?)

MSG1 DB "ENTER 1ST STRING: $"

MSG2 DB 10,13, "ENTER 2ND STRING:$"

MSG3 DB 10,13, "2 STRNG = $"

MSG4 DB 10,13, "2 STRNG NT = $"

MSG5 DB 10,13, " STR1 LEN : $"

MSG6 DB 10,13, " STR2 LEN : $"

.CODE

MOV AX,@DATA

MOV DS,AX

MOV ES,AX

LEA DX,MSG1

MOV AH,09H

INT 21H

LEA DX,STR1

MOV AH,0Ah

INT 21H

MOV DL,0AH

MOV AH,02H

INT 21H

LEA DX,MSG2

MOV AH,09H

INT 21H

LEA DX,STR2

MOV AH,0aH

INT 21H

LEA DX,MSG5

MOV AH,09H

INT 21H

MOV DL,STR1+1

ADD DL,30H

MOV AH,02H

INT 21H

LEA DX,MSG6

MOV AH,09H

INT 21H

MOV DL,STR2+1

ADD DL,30h

MOV AH,02H

INT 21H

Page 57: VTU computer science Lab manual.pdf

56

LEA SI,STR1+2

LEA DI,STR2+2

mov CL,STR1+1

REPZ CMPSB

JZ L1

LEA DX,MSG4

MOV AH,09H

INT 21H

JMP LAST

L1:LEA DX,MSG3

MOV AH,09H

INT 21H

LAST:MOV AH,4CH

INT 21H

END

MOV dL,STR2+1

add dl,30h

mov ah,02h

int 21h

lea si,str1+2

lea di,str2+2

mov cl,str1+1

REPZ CMPSB

JZ L1

LEA DX,MSG4

MOV AH,09H

INT 21H

JMP LAST

L1:LEA DX,MSG3

MOV AH,09H

INT 21H

LAST: MOV AH,4CH

INT 21H

END

Page 58: VTU computer science Lab manual.pdf

57

-:SUB STRING:-

.MODEL SMALL

.DATA

STR1 DB "WELCOME TO INFO SCIENCE $"

CN1 DW ($-STR1)

STR2 DB "COLLEGE $ "

CN2 DW ($-STR2)

MSG1 DB "STR IS PRESENT IN MAIN STR $"

MSG2 DB "STR IS NT PRESENT IN MAIN STRING $"

.CODE

MOV AX,@DATA

MOV DS,AX

MOV ES,AX

MOV CX,CN1

LEA SI,STR2

LEA DI,STR1

CLD

AGAIN : MOV AL,[SI]

REPNZ SCASB

INC SI

MOV DX,CX

CMP CX,CN2

JB FAIL

MOV CX,CN2

DEC CX

REPZ CMPSB

JNZ NEXT

LAST: LEA DX,MSG1

MOV AH,09H

INT 21H

JMP FINISH

FAIL: LEA DX,MSG2

MOV AH,09H

INT 21H

JMP FINISH

NEXT : MOV CX,DX

LEA SI,STR2

JMP AGAIN

FINISH: MOV AH,4CH

INT 21H

END

Page 59: VTU computer science Lab manual.pdf

58

-:TIME:-

DISPLAY MACRO NUM

MOV DL,NUM

MOV AH,02H

INT 21H

ENDM DISPLAY

.MODEL SMALL

.DATA

MSG DB "CURRENT TIME IS $"

.CODE

MOV AX,@DATA

MOV DS,AX

LEA DX,MSG

MOV AH,09H

INT 21H

MOV AH,2CH

INT 21H

MOV AL,CH

CALL PRINT

DISPLAY ':'

MOV AL,CL

CALL PRINT

DISPLAY':'

MOV AL,DH

CALL PRINT

DISPLAY 20H

CMP CH,0CH

JC NEXT

DISPLAY 'P'

DISPLAY 'M'

JMP LAST

NEXT: DISPLAY 'A'

DISPLAY 'M'

LAST: MOV AH,4CH

INT 21H

PRINT PROC

AAM

ADD AX,3030H

PUSH AX

DISPLAY AH

POP AX

DISPLAY AL

RET

PRINT ENDP

END

PRINT PROC

Page 60: VTU computer science Lab manual.pdf

59

MUX 8

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

-- Uncomment the following lines to use the declarations that are

-- provided for instantiating Xilinx primitive components.

--library UNISIM;

--use UNISIM.VComponents.all;

entity mux8 is

Port ( s : in std_logic_vector(2 downto 0);

i : in std_logic_vector(7 downto 0);

j : out std_logic);

end mux8;

architecture Behavioral of mux8 is

begin

process(s,i)

begin

case s is

when"000"=>j<=i(0);

when"001"=>j<=i(1);

when"010"=>j<=i(2);

when"011"=>j<=i(3);

when"100"=>j<=i(4);

when"101"=>j<=i(5);

when"110"=>j<=i(6);

when"111"=>j<=i(7);

when others=>null;

end case;

end process;

end Behavioral;

…………………………………………………………………………………………..

Page 61: VTU computer science Lab manual.pdf

60

MOD_8 UP_COUNTER

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

-- Uncomment the following lines to use the declarations that are

-- provided for instantiating Xilinx primitive components.

--library UNISIM;

--use UNISIM.VComponents.all;

entity mod8 is

Port ( clk : in std_logic;

q : out std_logic_vector(3 downto 0));

end mod8;

architecture Behavioral of mod8 is

signal q1: std_logic_vector(3 downto 0):="0000";

begin

process(clk)

begin

if(clk='1' and clk'event)then

q1<="0000";

if(q1="1111")then

q1<="0000";

else

q1<=q1+1;

end if;

q<=q1;

end if;

end process;

end Behavioral;

…………………………………………………………………………………………..

Page 62: VTU computer science Lab manual.pdf

61

FULL ADDER

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

-- Uncomment the following lines to use the declarations that are

-- provided for instantiating Xilinx primitive components.

--library UNISIM;

--use UNISIM.VComponents.all;

entity fulladder is

Port ( a : in std_logic;

b : in std_logic;

c : in std_logic;

sum : out std_logic;

carry : out std_logic);

end fulladder;

architecture Behavioral of fulladder is

begin

sum<=a xor b xor c;

carry<=(a and b) or (b and c) or (a and c);

end Behavioral;

……………………………………………………………………………………………

Page 63: VTU computer science Lab manual.pdf

62

JOHN_COUNTER

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

-- Uncomment the following lines to use the declarations that are

-- provided for instantiating Xilinx primitive components.

--library UNISIM;

--use UNISIM.VComponents.all;

entity john is

Port ( clk : in std_logic;

q : out std_logic_vector(3 downto 0));

end john;

architecture Behavioral of john is

signal q1:std_logic_vector(3 downto 0):="0000";

begin

process(clk)

begin

if(clk='1' and clk'event)then

q1(0)<=not(q1(3));

for i in 0 to 2 loop

q1(i+1)<=q1(i);

end loop;

end if;

q<=q1;

end process;

end Behavioral;

Page 64: VTU computer science Lab manual.pdf

63

D – FLIPFLOP

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

-- Uncomment the following lines to use the declarations that are

-- provided for instantiating Xilinx primitive components.

--library UNISIM;

--use UNISIM.VComponents.all;

entity DIF is

Port ( CLK : in std_logic;

D : in std_logic;

Q : out std_logic);

end DIF;

architecture Behavioral of DIF is

begin

Process(clk)

begin

if(clk='1' and clk'event)then

q<=d;

end if;

end process;

end Behavioral;