System programmin practical file

668 views
583 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
668
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
45
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

System programmin practical file

  1. 1. Ankit Dixit Univ. Roll No. 100420825160 [1] 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 ofstreamoutfile ("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 ifstreaminfile ("data.txt"); while(infile) { infile>>a>>b; cout<<a<<b; infile.close(); } getch(); } Output :
  2. 2. Ankit Dixit Univ. Roll No. 100420825160 [2] 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(); } }
  3. 3. Ankit Dixit Univ. Roll No. 100420825160 [3] Output :
  4. 4. Ankit Dixit Univ. Roll No. 100420825160 [4] 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]; intr,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 studentsn"; cin>>c; for(inti=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);
  5. 5. Ankit Dixit Univ. Roll No. 100420825160 [5] }cout<<endl; } k.close(); cout<<"nNew datan"; 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 :
  6. 6. Ankit Dixit Univ. Roll No. 100420825160 [6] 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; inti; 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<<"ntt MENU"; cout<<"ntt1.inter"; cout<<"ntt2.union "; intch; cout<<"n enter choice"; cin>>ch; if(ch==2) {for(i=0;i<n;i++)
  7. 7. Ankit Dixit Univ. Roll No. 100420825160 [7] { 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)";
  8. 8. Ankit Dixit Univ. Roll No. 100420825160 [8] cin>>choice; if(choice==1) break; }} Output :
  9. 9. Ankit Dixit Univ. Roll No. 100420825160 [9] 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> voiddisp() {cout<<"INVALID STATEMENT"; getch(); exit(0);} void main() {char s[20],a[20],b[20],c[20]; intsp=0,i=0; cout<<"Enter the stringn"; 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++; a[j]='0';
  10. 10. Ankit Dixit Univ. Roll No. 100420825160 [10] 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<<" typen"; getch(); exit(0); }} g.close(); disp(); getch(); }
  11. 11. Ankit Dixit Univ. Roll No. 100420825160 [11] Output :When statement is invalid Output :When statement is valid
  12. 12. Ankit Dixit Univ. Roll No. 100420825160 [12] 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 problem 2. Specify data structure 3. Define format of DS 4. Specify algorithm 5. 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.
  13. 13. Ankit Dixit Univ. Roll No. 100420825160 [13] Pass 1 Algorithm’s Flowchart:
  14. 14. Ankit Dixit Univ. Roll No. 100420825160 [14] Pass 2 :
  15. 15. Ankit Dixit Univ. Roll No. 100420825160 [15] 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
  16. 16. Ankit Dixit Univ. Roll No. 100420825160 [16] 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 Phase Unlike 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.
  17. 17. Ankit Dixit Univ. Roll No. 100420825160 [17] 3. Interpretation Phase Once 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. Optimization Compiler 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 Assignment It is the process of allocating memory to various resources. Memory may be allocated to various literals, storage, symbols, machine OP etc. 6. Code Generation It 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 language b. Relocate machine code c. Absolute machine language The absolute machine code is the one which is ready to be executed.
  18. 18. Ankit Dixit Univ. Roll No. 100420825160 [18] 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-Codeor 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,Rs M,Rs Rd,M Copy from source to destination. MVI Rd,Data M,Data Move immediate 8-bit LDA 16-bit address Load Accumulator LXI Reg. pair, 16-bitdata Load register pair immediate HLD 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 thecontents of memory location can be added to thecontents 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 addeddirectly to the contents of register C.
  19. 19. Ankit Dixit Univ. Roll No. 100420825160 [19] 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 locationcan 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 R M Add reg. or memory to accumulator ADC R M Add register or memory to accumulator with carry ADI 8-bit data Add immediate to accumulator ACI 8-bit data Add immediate to accumulator with carry DAD Reg. pair Add register pair to H-L pair INR R M 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 orright 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.
  20. 20. Ankit Dixit Univ. Roll No. 100420825160 [20] Op-code Operand Description CMP R M Compare register or memory with accumulator 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 sequentialflow. 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.
  21. 21. Ankit Dixit Univ. Roll No. 100420825160 [21] 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 operation HLT None Halt DI None Disable interrupt EI None Enable interrupt
  22. 22. Ankit Dixit Univ. Roll No. 100420825160 [22] 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 Debugging 2. 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.
  23. 23. Ankit Dixit Univ. Roll No. 100420825160 [23] 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.
  24. 24. Ankit Dixit Univ. Roll No. 100420825160 [24] 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.
  25. 25. Ankit Dixit Univ. Roll No. 100420825160 [25] 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
  26. 26. Ankit Dixit Univ. Roll No. 100420825160 [26] 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 left j Cursor down k Cursor up l Cursor right 0 Cursor to beginning of line $ Cursor to end of line w Cursor to next word e Cursor to end of current word b Cursor to beginning of current word H Cursor to top of screen M Cursor to middle of screen L Cursor to last line on screen ^F Scroll forward 1 screen ^D Scroll down 1/2 screen
  27. 27. Ankit Dixit Univ. Roll No. 100420825160 [27] ^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 cursor dw Delete word under cursor dd Delete current line D Delete from cursor position to end of line Editing Commands a Append text after cursor A Append at end of line i Insert text at current cursor position I Insert at beginning of line r Replace character under cursor o 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 Viwrite commands, listed below, always write over the file if it already existed. They do not make any backups. :wq Write file (save), then quit ZZ Write file (save), then quit :x Write file (save), then quit :q! Quit without saving Saving your File :w Write/save current file
  28. 28. Ankit Dixit Univ. Roll No. 100420825160 [28] 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.

×