SP Lab Manual 48,49

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

LAB MANUAL

SUBMITTED TO: Mam Maham


SUBMITTED BY: Kineeza habib,Mehak Nadeem
ROLL NO: 19148, 19149
SEMESTER: 7TH
SECTION: MORNING A
PROGRAM: BSCS
COURSE TITLE: System programing
DEPARTMENT OF COMPUTER SCEINCE
Program no 18.1

#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
int n=10;
char ch='*';
double d=38.12;
ofstream file("e:\\test.txt");
if(!file)
{
cout<<"File opening error";
exit(1);
}
file<<n<<" "<<ch<<" "<<d;
file.close();
}

Program no 18.2

#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
char city[50];
ofstream file("e:\\city.txt");
for(int i=0;i<=5;i++)
{
cout<<"Enter the name of any city:";
cin>>city;
file<<city<<'\n';
}
file.close();
}

Program no 18.3

#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
int n;
char ch;
double d;
ifstream file("e:\\test.txt");
if(!file)
{
cout<<"File opening error";
exit(1);
}
file>>n>>ch>>d;
cout<<"The contents of file are as follows:"<<endl;
cout<<n<<endl<<ch<<endl<<d<<endl;
file.close();
}
Program no 18.4
#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
char city[50];
ifstream file("e:\\city.txt");
if(!file)
{
cout<<"Error in opening file:";
exit(1);
}
cout<<"The list of cities is as follows:"<<endl;
while(!file.eof())
{
file>>city;
cout<<city<<endl;
}
file.close();
}

Program no 18.5
#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
char str[100];
ofstream out("e:\\strings.txt");
ifstream in("e:\\strings.txt");
for(int i=0;i<5;i++)
{
cout<<"Enter a string:";
gets(str);
out<<str<<'\n';
}
out.close();
cout<<"The list of strings is as follows:"<<endl;
while(!in.eof())
{
in.getline(str,100);
cout<<str<<endl;
}
in.close();
}

Program no 18.6
#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
char ch;
ofstream out("e:\\strings.txt");
for(int i=0;i<5;i++)
{
cout<<"Enter a character:";
cin>>ch;
out.put(ch);
}
out.close();
}

Program no 18.7

#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
char ch;
ifstream in("e:\\strings.txt");
while(!in.eof())
{
in.get(ch);
cout<<ch<<endl;
}
in.close();
}

Program no 18.9

#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
int n;
ofstream out("e:\\test.txt",ios::binary);
for(int i=1;i<=5;i++)
{
cout<<"Enter an integer:";
cin>>n;
out.write((char*)n,sizeof(n));
}
out.close();
}

Program no 18.10
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
struct Student
{
int rno;
char name[50];
int marks;
};
int main()
{
Student s;
ofstream out("e:\\students.txt",ios::binary);
for(int i=1;i<=3;i++)
{
cout<<"Enter your roll no:";
cin>>s.rno;
cout<<"Enter your name:";
cin>>s.name;
cout<<"Enter your marks:";
cin>>s.marks;
out.write((char*)&s,sizeof(s));
}
out.close();
}

Program no 18.11

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
struct Student
{
int rno;
char name[50];
int marks;
};
int main()
{
Student s;
ifstream in("e:\\students.txt",ios::binary);
while(!in.eof())
{
in.read((char*)&s,sizeof(s));
cout<<"Roll No:"<<s.rno<<endl;
cout<<"Name:"<<s.name<<endl;
cout<<"Marks:"<<s.marks<<endl;
}
in.close();
}
Program 18.12
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
struct email
{
char name[20];
char id[30];
};
Void main()
{
email user;
email check;
cout<<”enter a name”;
cin>>user.name;
cout<<”enter the email address”;
cin>>user.id;
ofstream out(“c:/email.txt” , ios::out l ios::binary);
out.write( (char*) &user, sizeof(struct email) );
out.close();
cout<<endl<<”contents of file are”;
ifstream in (“c:/email.txt” , ios::in l ios:: binary);
in.read((char*) &check, sizeof(struct email));
cout<<check name;
cout<<check id;
in.close;
getch();
}

Program 18.13
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class Country
{
private:
int id;
char name[50];
public:
void get()
{
cout<<"Enter country id: " ;
cin>>id;
cout<<"Enter country name: ";
cin>>name;
}
void show()
{
cout<<"Country ID: "<<id<<endl;
cout<<"Country Name: "<<name<<endl;
}
void main()
{
Clrscr();
char text[80], ch;
strcpy(text,"It is a test file");
out<<text;

out.close( );
ifstream in("c://mydoc.txt” , ios::binary);
in.seekg(8);
cou<<endl<<"The contents from position 8 are:"<<endl;
while(!in.eof())
{
in:get(ch);
if(!in.eof())
cout<<ch;
}
in.close( );
getch();
}

Program 18.13
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class Country
{
private:
int id;
char name[50];
public:
void get()
{
Cout<<"Enter country id: ";
cin>>id;
cout<<"Enter country name. ";
cin>>name;
}
void show()
{
Cout<<"Country ID: "<<id<<endl;
cout<<"Country Name' "<<name<<endl;
}
};
void main()
{
cirscr();
Country cn;
ifstream in("c:11country.bd", ios::binary);
in.read((char*)&cn, sizeof(cn));
cn.show();
in.close();
getch();
}

Program 18.16
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
{
ofstream print;
print.open("LPT1");
print<<"hello printer…..";
pprint.close();
}

Program no# 01
#include<BIOS.H>
#include<DOS.H>
char st[80] ={"Hello World$"};
char st1[80] ={"Hello Students!$"};
void interrupt (*oldint65)( );
void interrupt newint65( );
void main()
{
oldint65 = getvect(0x65);
setvect(0x65, newint65);
keep(0, 1000);
}
void interrupt newint65( )
{
if (( _AH ) == 0) //corrected
{
_AH = 0x09;
_DX = (unsigned int) st;
geninterrupt (0x21);
}
else
{
if (( _AH ) == 1) //corrected
{
_AH = 0x09;
_DX = (unsigned int) st1;
geninterrupt (0x21);
}
}
}

Program no# 02

#include<BIOS.H>
#include<DOS.H>
void main()
{
_AH = 1;
geninterrupt (0x65);
_AH = 0;
geninterrupt (0x65);
}

Program no# 03
Sample Program for interrupt Interception

void interrupt newint();


void interrupt (*old)();
void main()
{
old=getvect(0x08);
setvect(0x08,newint);
keep(0,1000);
}
void interrupt newint ()
{


(*old)();
}

Program no# 04
#include <dos.h>
void interrupt (*old)();
void interrupt new();
char far *scr=(char far* ) 0x00400017;
void main()
{
old=getvect(0x08);
setvect(0x08,new);
keep(0,1000);
}
void interrupt new (){
*scr=64;
(*old)();
}

Program no# 05
#include <dos.h>
void interrupt (*old)();
void interrupt newfunc();
char far *scr=(char far* ) 0xb8000000;
void main()
{
old=getvect(0x08);
setvect(0x08,newfunc);
keep(0,1000);
}
void interrupt newfunc ()
{
*scr=0x41; //corrected
*(scr+1)=0x07; //corrected
(*old)();
}

Program no# 06
#include <stdio.h>
void interrupt (*old)();
void interrupt newfunc();
char far *scr=(char far* ) 0xb8000000;
int j;
void main( )
{
old=getvect(0x08);
setvect(0x08,newfunc); //corrected
keep(0,1000); //corrected
}
void interrupt newfunc ( )
{
for ( j=0;j<4000;j+=2){ //corrected
if(*(scr+j)==‘1’){
*(scr+j)=‘9’; }
}
(*old)();
}

Program no# 07
The keyboard Interrupt
#include <dos.h>
void interrupt (*old)( );
void interrupt newfunc( );
void main( )
{
old = getvect(0x09);
setvect(0x09,newfunc);
keep(0,1000);
}
void interrupt newfunc ( )
{
(*old)( );
(*old)( );
(*old)( );
}
Program no# 08

#include <dos.h>
void interrupt (*old)( );
void interrupt newfunc( );
char far *scr = (char far* ) 0x00400017;
void main( )
{
old = getvect(0x09);
setvect(0x09,newfunc);
keep(0,1000);
}
void interrupt newfunc ( )
{
*scr = 64;
(*old)( );
}
Program no# 09
void interrupt (*old)( );
void interrupt newfunc( );
char far *scr = (char far* ) 0xB8000000;
int j;
void main( )
{
old = getvect(0x09);
setvect(0x09,newfunc);
keep(0,1000);
}
void interrupt newfunc ( )
{ for( j = 0;j < 4000; j += 2)
{
if (*(scr +j) == ‘1’)
*(scr + j) = ‘9’;
}
(*old) ( ); }

Program no# 10
Timer & Keyboard Interrupt Program
#include <dos.h>
void interrupt (*oldTimer)( ); //corrected
void interrupt (*oldKey)( ); //corrected
void interrupt newTimer ( );
void interrupt newKey ( );
char far *scr = (char far* ) 0xB8000000;
int i, t = 0, m = 0;
char charscr [4000];
void main( )
{
oldTimer = getvect(8);
oldKey = getvect (9);
setvect (8,newTimer);
setvect (9,newKey);
getch();
getch();
getch();
getch();
}
void interrupt newTimer ( )
{
t++;
if ((t >= 182) && (m == 0))
{
for (i =0; i < 4000; i ++)
charscr [i] = *(scr + i);
for (i =0; i <=4000; i +=2)
{
*(scr + i) = 0x20;
*(scr + i + 1) = 0x07;
}
t = 0; m = 1;
}
(*oldTimer) ( );
}
void interrupt newKey ( )
{
int w;
if (m == 1)
{
for (w =0; w < 4000; w ++)
*(scr + w) = charscr [w];
m = 0;
}
(*oldKey) ( );
}

Program no# 11
Reentrant Procedures & Interrupt
#include <stdio.h>
void interrupt *old();
void interrupt newint()
void main ()
{
old = getvect(0x65);
setvect(0x65,newint);
_AX=0xf00f;
geninterrupt(0x65);
a = _AX
printf(“%x”,a);
}
void interrupt newint()
{
_AX=0x1234;
}
Program no# 12
#pragma inline
#include <dos.h>
#include <bios.h>
void interrupt (*oldtsr) ( );
void interrupt newtsr (unsigned int BP, …, flags);
//must provide all the arguments
void main ( )
{
oldtsr = getvect (0x13);
setvect(0x13, newtsr); //corrected
keep (0, 1000);
}
void interrupt newtsr(unsigned int BP, unsigned int DI,
unsigned int SI, unsigned int DS, unsigned int ES, unsigned
int DX, unsigned int CX, unsigned int BX, unsigned int AX,
unsigned int IP, unsigned int CS,
unsigned int flags) //corrected
{
if ( _AH == 0x03)
if(( _DH == 1 && _CH == 0 && _CL == 1)&& _DL >= 0x80)
{
asm clc;
asm pushf;
asm pop flags;
return;
}
_ ES = ES; _DX = DX;
_CX = CX; _BX = BX;
_AX = AX;
*oldtsr;
asm pushf;
asm pop flags;
AX = _AX; BX = _BX;
CX = _CX; DX = _DX;
ES = _ES;
}
Program no# 13
The keyboard Hook
#include <dos.h>
#include <bios.h>
#include <stdio.h>
void interrupt (*oldint15) ( );
void interrupt newint15(unsigned int BP, …, flags);
void main ( )
{
oldint15 = getvect (0x15);
setvect (0x15, newint15);
keep (0, 1000);
}
void interrupt newint15(unsigned int BP, unsigned int DI,
unsigned int SI, unsigned int DS, unsigned int ES, unsigned
int DX, unsigned int CX, unsigned int BX, unsigned int AX,
unsigned int IP, unsigned int CS,
unsigned int flags)
{
if (*(((char*)&AX) + 1) == 0x4F )
{
if (*((char*)&AX) == 0x2C)
*(((char*)&AX)) = 0x1E;
else if (*((char*)&AX) == 0x1E)
*((char*)&AX) = 0x2C; //corrected
}
else
(*oldint15)();
}

Program no# 14
Port Addresses
#include <stdio.h>
#include <bios.h>
void main()
{
outport(0x21,0x02);
}

Program no# 15
#include <dos.h>
#include <stdio.h>
#include <bios.h>
void interrupt(*oldints)();
void interrupt newint8();
int t=0; //corrected
void main()
{
oldints=getvect(0x08);
setvect(0x08,newint8);
keep(0,1000);
}
void interrupt newint8()
{
t++:
if (t==182)
{
outport(0x21,2);
}
else{
if (t==364)
{
outport(0x21,0);
t=0;
}
}
(*oldints)();
}

Program no# 16
#include <dos.h>
void interrupt(*old)();
void interrupt newint9();
char far *scr=(char far *) 0x00400017;
void main()
{
old=getvect(0x09);
setvect(0x09,newint9);
keep(0,1000);
}
void interrupt newint9()
{
if (inportb(0x60)==83
&&(((*scr)&12)==12)) //corrected
{
outportb(0X20,0x20);
return;
}
(*old)();
}

Program no# 17
#include <dos.h>
void interrupt(*old)();
void interrupt newint9();
void main()
{
old=getvect(0x09);
setvect(0x09,newint9);
keep(0,1000);
}
void interrupt newint9()
{
if (inportb(0x60)==0x1F) //corrected

{
outportb(0X20,0x20);
return;
}
(*old)();
}
Program no# 18
#include <dos.h>
void interrupt (*old)();
void interrupt new1()!
unsigned char far *scr = (unsigned char far
*) 0x0040001C
void main()
{
old=getvect(0x09);
setvect(0x09,new1);
keep(0,100);
}
void interrupt new1 ()
{
if(inportb(0x60)==83)
{
*((unsigned char far*)0x00400000+*scr)=25;
if((*scr)==60)
*scr=30;
else
*scr+=2;
outportb(0x20,0x20);
return;
}
}

Program no# 19
A sample program
#include <stdio.h>
#include <dos.h>
#include <bios.h>
void main (void)
{ char a;
outport(0x20,8);
outport(0x20,0x0A);
a=inport(0x20);
printf (“value of IRR is %x”; ,a);
outport(0x20,0x08);
outport(0x20,0x0B);
a=inport(0x20);
printf (“value of ISR is %x”; ,a);
}

Program no# 20

int 65H is empty, we can use


its vector as a flag.
Address of vector
seg = 0
offset = 65H * 4
Solution
#include<stdio.h>
#include<BIOS.H>
#include<DOS.H>
unsigned int far * int65vec =
(unsigned far *)
MK_FP(0,0x65*4)
void interrupt (*oldint) ( );
void interrupt newfunc ( );
void main()
{
if((*int65vec) != 0xF00F)
//corrected
{
oldint =getvect (0x08);
setvect(0x08, newint);
(*int65vec) = 0xF00F;
keep (0,1000);
}else
{
puts (“Program Already Resident”);
}}
void interrupt newfunc ()
{ :::::::
:::::::
(*oldint) ( );
}
Program no# 21
#include<stdio.h>
#include<BIOS.H>
#include<DOS.H>
void interrupt (*oldint) ( );
void interrupt newfunc ( unsigned int BP,..…,flags);
void main()
{
_DI = 0
_AH = 0xFF;
geninterrupt (0x13);
if (_DI = = 0xF00F) {
puts (“Program Already Resident”);
exit (0);
}
Program no# 22

Else
{
oldint = getvect (0x13);
setvect (0x13, newint);
keep (0, 1000);
}}
void interrupt newint ( )
{
if (_AH == 0xFF)
{
DI = 0xF00F;
return;
}
else
{ :::::::
:::::::
:::::::
}
(*oldint) ( );
}

You might also like