35
Ankit Dixit Univ. Roll No. 100420825160 Program 1. WAP to read and write a string into a file. #include<iostream.h> #include<fstream.h> #include<conio.h> void main() { clrscr(); char college[20],name[20]; cin>>name; cout<<endl; cin>>college; // Writing to a file ofstream outfile ("data.txt"); outfile<<"name -"<<name<<"\ncollege-"<<college; outfile.close(); char a[10],b[10]; cout<<"The data in FILE is "<<endl; // Reading from a file ifstream infile ("data.txt"); while(infile) { infile>>a>>b; cout<<a<<b; infile.close(); } getch(); } Output : [1]

System programmin practical file

Embed Size (px)

DESCRIPTION

this document is intended only for educational purposes,any mistakes if you face,is not intentional.

Citation preview

Page 1: System programmin practical file

Univ. Roll No. 100420825160

Program 1. WAP to read and write a string into a file.

#include<iostream.h>#include<fstream.h>#include<conio.h>void main(){clrscr();char college[20],name[20];cin>>name;cout<<endl;cin>>college;

// Writing to a file

ofstream outfile ("data.txt"); outfile<<"name -"<<name<<"\ncollege-"<<college; outfile.close();char a[10],b[10];cout<<"The data in FILE is "<<endl;

// Reading from a file

ifstream infile ("data.txt");while(infile){ infile>>a>>b; cout<<a<<b; infile.close(); }getch();}

Output :

[1]

Page 2: System programmin practical file

Univ. Roll No. 100420825160

Program 2. WAP to insert names of the students alphabetically into two different files from a single read only file using file handling.

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

void main()

{ clrscr();

char n[50];

ifstream a("f1.txt");

ofstream b("f2.txt",ios::app);

ofstream c("f3.txt",ios::app);

while(a.eof()==0)

{

a>>n;

if(n[0]=='a')

{

b<<n<<endl;

}

else

{ c<<n<<endl;

}

a.close();

b.close();

c.close();

cout<<"done";

getch();

} }

[2]

Page 3: System programmin practical file

Univ. Roll No. 100420825160

Output :

[3]

Page 4: System programmin practical file

Univ. Roll No. 100420825160

Program 3. WAP to create a file for student (name, roll No. , marks) and also append a new student record to existing file.

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

#include<fstream.h>

char n[20];

int r,m;

void get()

{ cout<<"Enter name, roll no. and marks :\n";

cin>>n>>r>>m; }

void main()

{ clrscr();

char show[50],showup[50];

int c;

ofstream g("a.txt");

cout<<"Enter the total no. of students\n";

cin>>c;

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

{ get();

g<<n<<setw(5)<<r<<setw(5)<<m<<endl; }

g.close();

ifstream k("a.txt");

while ( k.eof()==0)

{ for(int x=0;x<3;x++)

{ k>>show;

cout<<show<<setw(5);

[4]

Page 5: System programmin practical file

Univ. Roll No. 100420825160

} cout<<endl; }

k.close();

cout<<"\nNew data\n";

g.open("a.txt",ios::app);

{ get();

g<<n<<setw(5)<<r<<setw(5)<<m<<endl; }

g.close();

ifstream l("a.txt");

while ( l.eof()==0)

{for(int x=0;x<3;x++)

{ l>>showup;

cout<<showup<<setw(5);

} cout<<endl; }

getch();

}

Output :

[5]

Page 6: System programmin practical file

Univ. Roll No. 100420825160

Program 4. WAP to perform different set operations (union, intersection).

#include<iostream.h>

#include<conio.h>

void main()

{ clrscr();

int a[20],b[20],c[40],j,k,m,n,choice;

int i;

cout<<"Enter no. of elements of a : ";

cin>>n;

cout<<"\n";

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

{ cin>>a[i]; }

cout<<"Enter no. of elements of b : ";

cin>>m;

cout<<"\n enter";

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

{ cin>>b[i]; }

while(1)

{ cout<<"\n\t\t MENU";

cout<<"\n\t\t1.inter";

cout<<"\n\t\t2.union ";

int ch;

cout<<"\n enter choice";

cin>>ch;

if(ch==2)

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

[6]

Page 7: System programmin practical file

Univ. Roll No. 100420825160

{ c[i]=a[i]; }

int k=n;

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

{ for(j=0;i<n+1;j++)

{ if(c[j]==b[i])

break; }

if (j==n+i)

{ c[k]=b[i];

k++; } }

cout<<"\n UNION";

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

{ cout<<c[i];}}

k=0;

if(ch==1)

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

{ for(j=0;j<m;j++)

{ if(a[i]==b[j])

{ c[k]=a[i];

k++;

break;

} } }

cout<<"\n INTERSECTION";

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

{ cout<<c[i];

} }

cout<<"\n Want to exit ?(enter 1)";

[7]

Page 8: System programmin practical file

Univ. Roll No. 100420825160

cin>>choice;

if(choice==1)

break;

} }

Output :

[8]

Page 9: System programmin practical file

Univ. Roll No. 100420825160

Program 5. WAP to generate lexical & syntax analyzer for a declaration statement.

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<process.h>

#include<stdio.h>

#include<string.h>

void disp()

{cout<<"INVALID STATEMENT";

getch();

exit(0);}

void main()

{char s[20],a[20],b[20],c[20];

int sp=0,i=0;

cout<<"Enter the string\n";

gets(s);

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

{ if(s[i]==' ')

{sp++; }

i++; }

if (sp!=2)

{ disp(); }

int x=0,j=0,k=0,l=0;

while(s[x]!=' ')

{ a[j]=s[x];

j++;

x++; }

x++;

[9]

Page 10: System programmin practical file

Univ. Roll No. 100420825160

a[j]='\0';

while(s[x]!=' ')

{ b[k]=s[x];

k++;

x++;

} x++;

b[k]='\0';

while(s[x]!='\0')

{ c[l]=s[x];

l++;

x++;

}x++;

c[l]='\0';

if(strcmp(c,";")!=0)

{ disp(); }

ifstream g("f11.txt");

char show[50];

while(g.eof()==0)

{ g>>show;

if(strcmp(a,show)==0)

{ cout<<"Variable "<<b<<" is of "<<a<<" type\n";

getch();

exit(0);

}}

g.close();

disp();

getch();

}

[10]

Page 11: System programmin practical file

Univ. Roll No. 100420825160

Output : When statement is invalid

Output : When statement is valid

[11]

Page 12: System programmin practical file

Univ. Roll No. 100420825160

6. Study of PASS1 and PASS2 of the assembler

ASSEMBLERS

Assembler is a system program which converts a program written in assembly language into machine language. Assembly language is special language in which we use special mnemonics symbols for each instruction. It inputs of an assembly language program known as source program and converts it into an equivalent machine language program which is our object program.

General design procedure :-

1. Specify the problem2. Specify data structure3. Define format of DS4. Specify algorithm5. Look for modularity i.e. capability on one program to be subdivided into several sub

program.6. Repeat step 1 to 5 until our program is resolved.

There are two passes in assembler.

(1) Pass1 (2) Pass2

PASS1

The purpose of pass 1 is to define symbols and literals. Following are the steps included in pass 1.

1. Determine length of machine instruction. It is known as MOTGET1.2. Keep track of location counter.3. Remember values of symbols until pass2.it is known as STSTO.4. Process some pseudo ops like EQU, DS. it is known as POTGET1.5. Remember literals. It is known as LITSTO(literals store)

PASS 2

The purpose of pass 2 is to generate object program. Following are steps included in pass2.

1. Look up values of symbols. It is known as STGET (single table get).2. Generate instructions. It is known as MOTGET.3. Generate data for DS (define storage), DC (define constant) and literals.4. Process of pseudo ops. It is known as POTGET2.

[12]

Page 13: System programmin practical file

Univ. Roll No. 100420825160

Pass 1 Algorithm’s Flowchart:

[13]

Page 14: System programmin practical file

Univ. Roll No. 100420825160

Pass 2 :

[14]

Page 15: System programmin practical file

Univ. Roll No. 100420825160

7. Study of the compiler and its various phases.

Compiler: Compiler is a system program which converts a program written in HLL into machine language. The HLL program given as input is known as source program and machine equivalent is known as object program. The functionality of compiler is divided into some phases as follows:

Phases of Compiler

The compiler has a number of phases plus symbol table manager and an error handler. 

Symbol Table Manager

Input Source Program

↓Lexical Analyzer

↓Syntax Analyzer

↓Semantic Analyzer

↓Intermediate Code

Generator

↓Code Optimizer

↓Code Generator

↓Out Target Program

 Error Handler

[15]

Page 16: System programmin practical file

Univ. Roll No. 100420825160

Front End vs Back End of a Compilers. The phases of a compiler are collected into front end and back end.

The front end includes all analysis phases end the intermediate code generator.

The back end includes the code optimization phase and final code generation phase.

The front end analyzes the source program and produces intermediate code while the back end synthesizes the target program from the intermediate code.

A naive approach (front force) to that front end might run the phases serially.

1. Lexical analyzer takes the source program as an input and produces a long string of tokens.

2. Syntax Analyzer takes an out of lexical analyzer and produces a large tree.3. Semantic analyzer takes the output of syntax analyzer and produces another tree.4. Similarly, intermediate code generator takes a tree as an input produced by semantic

analyzer and produces intermediate code.

Phases of Compiler :

1. Lexical Phase:Action of parsing source program into proper classes is the task of lexical analyzer. The program is scanned and separated into basic elements known as tokens. The basic elements like identifier, literals are placed into tables as other phases recognize the use and meaning of elements.

2. Syntax PhaseUnlike other aspects of the compiler, the syntax analysis parts are not very separable, since they are mixed up with calls to all other parts, such as semantic analysis. The method depends on writing a separate parsing procedure for each kind of syntactic structure, such as if statement, assignment statement, expression and so on, and each of these is only responsible for analyzing its own kind of structure. If any structure contains another structure then the parsing procedure can call the procedure for this contained structure.

[16]

Page 17: System programmin practical file

Univ. Roll No. 100420825160

3. Interpretation PhaseOnce the program is correct according to syntax, the compiler generates a intermediate code. The intermediate code may be in the form of parse tree , postfix notation and three address code.

4. OptimizationCompiler optimization is the process of tuning the output of a compiler to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the amount of memory occupied. Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output programs that uses less resources.

5. Storage AssignmentIt is the process of allocating memory to various resources. Memory may be allocated to various literals, storage, symbols, machine OP etc.

6. Code GenerationIt is the last phase of the compiler. This phase receives optimized intermediate code and generates the code for execution. The target program may be in the form of :a. assembly languageb. Relocate machine codec. Absolute machine language

The absolute machine code is the one which is ready to be executed.

[17]

Page 18: System programmin practical file

Univ. Roll No. 100420825160

8. Study of instruction set of 8085 microprocessor

An instruction is a binary pattern designed inside a microprocessor to perform a specific function. The entire group of instructions that a microprocessor supports is called Instruction Set.8085 has 246 instructions. Each instruction is represented by an 8-bit binary value. These 8-bits of binary value are called Op-Code or Instruction Byte.

Classification of Instruction Set:• Data Transfer Instruction• Arithmetic Instructions• Logical Instructions• Branching Instructions• Control Instructions

Data Transfer Instructions :

These instructions move data between registers, or between memory and registers. These instructions copy data from source to destination. While copying, the contents of source are not modified.

Op-code Operand Description

MOV Rd, RsM,RsRd, M

Copy from source to destination.

MVI Rd, DataM, Data

Move immediate 8-bit

LDA 16-bit address Load Accumulator

LXI Reg. pair, 16-bit data Load register pair immediateHLD 16-bit address Load H-L registers direct

STA 16-bit address Store accumulator direct

SHLD 16-bit address Store H-L registers direct

XCHG None Exchange H-L with D-E

Arithmetic Instructions :

These instructions perform the operations like:• Addition • Subtract • Increment • Decrement

Addition Any 8-bit number, or the contents of register, or the contents of memory location can be added to the contents of accumulator. The result (sum) is stored in the accumulator. No two other 8-bit registers can be added directly. Example: The contents of register B cannot be added directly to the contents of register C.

[18]

Page 19: System programmin practical file

Univ. Roll No. 100420825160

Subtraction Any 8-bit number, or the contents of register, or the contents of memory location can be subtracted from the contents of accumulator. The result is stored in the accumulator. Subtraction is performed in 2’s complement form. If the result is negative, it is stored in 2’s complement form. No two other 8-bit registers can be subtracted directly.

Increment / Decrement The 8-bit contents of a register or a memory location can be incremented or decremented by 1.The 16-bit contents of a register pair can be incremented or decremented by 1.Increment or decrement can be performed on any register or a memory location.

Opcode Operand Description

ADD RM

Add reg. or memory to accumulator

ADC RM

Add register or memory to accumulator with carry

ADI 8-bit data Add immediate to accumulatorACI 8-bit data Add immediate to accumulator with carry

DAD Reg. pair Add register pair to H-L pair

INR RM

Increment register or memory by 1

INX R Increment register pair by 1

Logical Instructions

These instructions perform logical operations on data stored in registers, memory and status flags. The logical operations are:

• AND • OR • XOR • Rotate • Compare • Complement

AND, OR, XOR: Any 8-bit data, or the contents of register, or memory location can logically have AND operation, OR operation, XOR operation with the contents of accumulator. The result is stored in accumulator.

Rotate: Each bit in the accumulator can be shifted either left or right to the next position.

Compare: Any 8-bit data or the contents of register, or memory location can be compares for Equality, Greater Than, less than with the contents of accumulator. The result is reflected in status flags.

Complement: The contents of accumulator can be complemented. Each 0 is replaced by 1 and each 1 is replaced by 0.

[19]

Page 20: System programmin practical file

Univ. Roll No. 100420825160

Op-code Operand Description

CMP RM

Compare register or memory withaccumulator

CPI 8-bit data Compare immediate with accumulator

RAR None Rotate accumulator right through carry

CMA None Complement accumulator

CMC None Complement carry

STC None Set carry

Branching Instructions

The branching instruction alters the normal sequential flow. These instructions alter either unconditionally or conditionally.

JMP The program sequence is transferred to the memory location specified by the 16-bit address given in the operand. Example: JMP 2034 H.

Jx The program sequence is transferred to the memory location specified by the 16-bit address given in the operand based on the specified flag of the PSW. Example: JZ 2034 H.

CALL The program sequence is transferred to the memory location specified by the 16-bit address given in the operand. Before the transfer, the address of the next instruction after CALL is pushed onto the stack. Example: CALL 2034 H.

Cx The program sequence is transferred to the memory location specified by the 16-bit address given in the operand based on the specified flag of the PSW. Before the transfer, the address of the next instruction after the call (the contents of the program counter) is pushed onto the stack. Example: CZ 2034 H.

RET The program sequence is transferred from the subroutine to the calling program. The two bytes from the top of the stack are copied into the program counter, and program execution begins at the new address. Example: RET.

RST The RST instruction jumps the control to one of eight memory locations depending upon the number. These are used as software instructions in a program to transfer program execution to one of the eight locations. Example: RST 3.

[20]

Page 21: System programmin practical file

Univ. Roll No. 100420825160

Op-code Operand Description

JMP 16-bit address Jump unconditionally

Jx 16-bit address Jump conditionally

CALL 16-bit address Call unconditionally

Cx 16-bit address Call conditionally

RET None Return unconditionally

RST 0 – 7 Restart (Software Interrupts)

Control Instructions

The control instructions control the operation of microprocessor.

Opcode Operand Description

NOP None No operationHLT None Halt

DI None Disable interrupt

EI None Enable interrupt

[21]

Page 22: System programmin practical file

Univ. Roll No. 100420825160

9. Debugging in C++ environment

Locating and removing errors is known as debugging.

A debugger is a system program which locates bugs and removes them.

Debugging Procedure :

1. Type any program in the given language.2. Compile it.3. Look for the errors.4. Remove errors and recompile the program.5. Repeat steps 3 & 4 until our program becomes error free.

Case Study:

We can divide the debugging procedure in C++ in two categories:

1. Single-step Control Debugging2. Break point Control Debugging

Single-step Control Debugging : There are three main methods of debugging under this category :

a) Single line Control b) Watch window c) Procedure control

a) Single line Control: After compilation if there are errors in our program then by pressing F7 we check our program in single line control. Where there is any error or bug in any line then that particular line is skipped and the control is transferred to next adjacent line.

b) Watch window: After pressing F7 , if we press Ctrl+F7 then a watch window will be displayed on the screen. The user has to type the variable in the add watch window and value of that variable will be displayed.

[22]

Page 23: System programmin practical file

Univ. Roll No. 100420825160

c) Procedure control This technique is used if our program is very large and it is divided into several sub-procedures. Simply pressing F8 will result our program to be compiled on procedure basis.

Break point Control Debugging

As the name implies , it allows the user to set break=points in the program, in order to check the contents of the registers, memories and other programming entities like variables etc. it allows the user to halt the execution of the program at that break-point. Then the user can examine intermediate results or contents. The break=point can be inserted at any particular position in our program by the user himself.

[23]

Page 24: System programmin practical file

Univ. Roll No. 100420825160

10. Study of editors and its types :

Editor: An editor is a program which creates or edits the documents. it is basically a program that provides a user to create and edit the target document. The document include the objects such as: computer programs, text, equations, tables, diagrams, paragraphs etc. that so anything that one might find on a printed page.

# GENERAL EDITING PROCEDURE:

1. Select a part of target document to be viewed and manipulated.

2. Examine how to format this view and how to display it.

3. Specify and execute the operations that modify the target document.

4. Update the view as per requirement.

# BASIC FUNCTIONS PERFORMED BY AN EDITOR: travelling, editing, formatting, viewing, filtering, display

Travelling: movement of editing context to new position within the text is called as travelling. This process can be done by user itself explicitly.

Editing: All the operations like copy, select, allocation, paste ,replace etc. comes under editing,

Formatting: Specify & execute operations the modify the documents comes under this. Viewing: It compiles formatting. The text in a manner desired by the user. This is an

abstract view independent of physical features of i/o devices. Filtering: Determining how to format this, view and how to display it. Display: This maps the viewing data into physical features of display device. The main

function of display is to determine how data looks like on the computer screen.

# TYPES OF AN EDITOR :

Text editor: A text editor is a small program that has never been developed mainly for writing and editing plain text. It is a computer program that lets a user enter, change, store, print text. Typically a text editor provides an empty screen with a fixed length and visible line number.

[24]

Page 25: System programmin practical file

Univ. Roll No. 100420825160

A special line allows you to move to a new page, scroll forward and backward, make global changes in the document, save the document and perform other actions. Text editor can be used to enter a language source statement or to create documents such as technical manuals.

Features of a text editor:

# find, search and replace

# cut copy paste

# text formatting

# undo and redo

# import and export

# filtering

# open and close files in text modes

# edit and merge files

# recover the lost files

Example: of the text editor are: GUI based (notepad and MAC) and command based (UNIX and DOS window)

Following are the further types of the text editor:--

line editor, full screen editor, multiwindow editor, source code editor

Description of these editors are as follows:

Line editor: it provide the user to edit only one line of a file at a time. Text editor is line oriented. These editors are restricted to the text oriented i/o method. In this, simultaneous typing, editing and document display is not allowed. Programmers may modify the text by entering commands on a text terminal (shell) e.g. UNIX, edlin

Full screen editor: it is used to edit the files and scripts in the full screen mode. Some full screen editors are available which provides the user to navigate in different modes of the editor. It provides maximum power to the programmer in editing and creating the text

[25]

Page 26: System programmin practical file

Univ. Roll No. 100420825160

files. They provide freedom to user in using most of the features like cut, copy, paste. e.g. notepad, MS word.

Multi-Window editor: This editor provides a user to open different windows and workspaces at a time. Using such editors, a programmer can open and compare the different text files at same time. e.g. MS word, web browsers.

Source code editor: it is also known as structure editor. These are the text editors with editing features to create and update the source code of software program. These provide features like syntax highlighting, macros, coding tools. Generally programming language editors like PHP editor and various html editors comes under this category.

Case Study: Vi Editors

This is the Original Unix Default Editor for e-mail and news articles

Vi is a modal editor; that is, when using vi, you are in an edit mode, or in a command mode. When in the command mode, all keystrokes are interpreted as commands. When in the edit mode, these same keystrokes are interpreted as characters to enter into the text. On mutant, vi provides no visual clue as to which mode you are in. In order to ensure that you are in the command mode, press the <M> key. If you are in doubt as to which mode you are in, press the <M> key a couple of times. This will ensure that you are in the command mode, and then you can go to the edit mode if you wish.

Vi Command Mode Commands:

Cursor Movement:

h     Cursor leftj      Cursor downk     Cursor upl      Cursor right0    Cursor to beginning of line$     Cursor to end of linew    Cursor to next worde     Cursor to end of current wordb     Cursor to beginning of current wordH    Cursor to top of screenM    Cursor to middle of screenL     Cursor to last line on screen

^F     Scroll forward 1 screen^D     Scroll down 1/2 screen

[26]

Page 27: System programmin practical file

Univ. Roll No. 100420825160

^U     Scroll up 1/2 screen^B     Scroll back 1 screen

:0     Move cursor to top of file:$      Move cursor to last line of file

Deletion Commands

x         Delete character under cursordw     Delete word under cursordd     Delete current lineD     Delete from cursor position to end of line

Editing Commands

a     Append text after cursorA     Append at end of linei     Insert text at current cursor positionI     Insert at beginning of liner     Replace character under cursoro     Open a new line above current line and enter edit mode

While editing, the backspace key works as a destructive backspace (delete). Backspacing over characters automatically deletes them, and they must be retyped.

Exiting from Vi

The Vi write commands, listed below, always write over the file if it already existed. They do not make any backups.

:wq Write file (save), then quitZZ Write file (save), then quit :x Write file (save), then quit:q! Quit without saving

Saving your File

:w Write/save current file

[27]

Page 28: System programmin practical file

Univ. Roll No. 100420825160

Getting a new file

:n filename Read in the file called filename

Extra Commands

ncommand A number n followed by a command will repeat the command n times. For example 9dd will delete the current line and the 8 subsequent lines.

[28]