You are on page 1of 57

VELAMMAL ENGINEERING COLLEGE

VELAMMAL NAGAR AMBATTUR, SURAPET, CHENNAI -600066

DEPARTMENT OF COMPUTER SCIENCE

CS2307 SYSTEM SOFTWARE LAB
LAB MANUAL (EVEN SEM 2011-2012)

BRANCH SEMESTER STAFF

: : :

COMPUTER SCIENCE V Semester ‘A’ Section Mr. G.Sankar

Syllabus
IT2307 SYSTEM SOFTWARE LAB (Using C) 0 0 3 2

Implement a symbol table with functions to create, insert, modify, search, and display. 1. 2. Implement pass one of a two pass assembler. Implement pass two of a two pass assembler.

Implement a single pass assembler. Implement a two pass macro processor Implement a single pass macro processor. 3. 4. 5. 6. 7. Implement an absolute loader. Implement a relocating loader. Implement pass one of a direct-linking loader. Implement pass two of a direct-linking loader. Implement a simple text editor with features like insertion / deletion of a character, word, and sentence. 8. Implement a symbol table with suitable hashing

VELAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY LAB COURSE PLAN
DEPT: INFORMATION TECHNOLOGY SUB: SYSTEM SOFTWARE LAB SEM: III IT A CODE: IT2307

OBJECTIVE: To teach the principles of System software and to give a practical training in writing efficient programs implementing the algorithms and concepts of System software in C language. Batch-A Date Period Batch-B Date Period

S.No 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Name of the Experiment Hands on experience on File handling in C language Implement a symbol table with functions to create, insert, modify, search, and display. Implement pass one of a two pass assembler. Implement pass two of a two pass assembler. Implement a single pass assembler. Implement a two pass macro processor Implement a single pass macro processor. Model Examination – I Implement an absolute loader. Implement a relocating loader. Implement pass one of a direct-linking loader. Implement pass two of a direct-linking loader. Implement a simple text editor with features like insertion / deletion of a character, word, and sentence. Implement a symbol table with suitable hashing Model Examination – II

PREPARED BY Mr. R.ARUNPRAKASH Lecturer / IT Ex.No: 01

APPROVED BY (Dr. N. Durai Pandian ) (HOD/IT)

SYMBOL TABLE
AIM: To implement a Symbol table with functions to create, insert, modify, search and display in C language. ALGORITHM: 1. 2. 3. 4. 5. 6. 7. Start the program Define the structure of the symbol table Enter the choice for performing the operations in the symbol table If choice is 1, search symbol table for the symbol to be inserted. If the symbol is already present display “Duplicate Symbol”, else insert symbol and corresponding address in the symbol table If choice is 2, symbols present in the symbols table are displayed If choice is 3, symbol to be deleted is searched in the symbol table, if found deletes else displays “Not Found”. If choice is 5, the symbol to be modified is searched in the symbol table. The label or address or both can be modified. PROGRAM: /* C Program to implement SYMBOL TABLE */

#include<stdio.h> #include<conio.h> #include<alloc.h> #include<string.h> #define null 0 int size=0; void insert(); void del(); int search(char lab[]); void modify(); void display(); struct symbtab { char label[10]; int addr; struct symbtab *next; }; struct symbtab *first,*last; void main() { int op; int y; char la[10]; clrscr(); do

{ printf("\nSYMBOL TABLE IMPLEMENTATION\n"); printf("1.INSERT\n"); printf("2.DISPLAY\n"); printf("3.DELETE\n"); printf("4.SEARCH\n"); printf("5.MODIFY\n"); printf("6.END\n"); printf("\nEnter your option: "); scanf("%d",&op); switch(op) { case 1: insert(); display(); break; case 2: display(); break; case 3: del(); display(); break; case 4: printf("Enter the label to be searched: "); scanf("%s",la); y=search(la); if(y==1) printf("The label is already in the symbol table\n"); else printf("The label is not found in the symbol tablel\n"); break; case 5: modify(); display(); break; case 6: break; } }while(op<6); getch(); } void insert() { int n; char l[10]; printf("Enter the label: "); scanf("%s",l); n=search(l); if(n==1) printf("The label is already in the symbol table. Duplicate cant be inserted\n");

p=malloc(sizeof(struct symbtab)). p=first. printf("LABEL\tADDRESS\n"). } return flag. scanf("%d". } size++.i<size.i++) { if(strcmp(p->label. struct symbtab *p. printf("Enter the address: "). } } int search(char lab[]) { int i. } void modify() { char l[10]. . strcpy(p->label.nl[10].i<size.lab)==0) { flag=1.p->label. if(size==0) { first=p.else { struct symbtab *p. p->next=null. } p=p->next.i++) { printf("%s\t%d\n". last=p.p->addr).&p->addr).flag=0.l). struct symbtab *p. p=p->next. for(i=0. for(i=0. last=p. } } void display() { int i. } else { last->next=p. p=first.

Only the label\n"). printf("Enter the new address: ").&add).i++) { if(strcmp(p->label. case 2: printf("Enter the label whose address is to be modified: "). printf("1.choice.l)==0) { p->addr=add.&choice). printf("3.Only the address of a particular label\n"). } } break. printf("What do you want to modify?\n"). struct symbtab *p. else { for(i=0. . scanf("%s". s=search(l). printf("2. scanf("%d". } p=p->next.l).i.int add.i<size.l). if(s==0) printf("\nNo such label"). } } break.Both the label and address\n"). if(s==0) printf("\nNo such label").s. switch(choice) { case 1: printf("Enter the old label\n"). scanf("%d". printf("Enter the new label: "). scanf("%s".nl).i<size. scanf("%s".l)==0) { strcpy(p->label. s=search(l). } p=p->next. printf("Enter your choice: ").nl). else { for(i=0.i++) { if(strcmp(p->label. p=first.

nl). scanf("%s".nl). } } break. a=search(l).i<size.l)==0) { q=p->next.l)==0) first=first->next.l)==0) { strcpy(p->label. else { if(strcmp(first->label. while(strcmp(q->label.l). else { for(i=0. printf("Enter the new address: "). printf("Enter the new label: "). } p=p->next. scanf("%d". p->addr=add. else if(strcmp(last->label. } . } p->next=null. p=first. struct symbtab *p.l). last=p. q=q->next.case 3: printf("Enter the old label: "). printf("Enter the label to be deleted: "). char l[10]. scanf("%s".l)!=0) { p=p->next. scanf("%s". if(a==0) printf("Label not found\n"). s=search(l).i++) { if(strcmp(p->label.*q. if(s==0) printf("\nNo such label").&add). } } void del() { int a.

DELETE 4.SEARCH .END Enter your option: 1 Enter the label: SECOND Enter the address: 1080 LABEL ADDRESS FIRST 1002 SECOND 1080 SYMBOL TABLE IMPLEMENTATION 1.DISPLAY 3.MODIFY 6.INSERT 2.INSERT 2. } p->next=q->next.l)!=0) { p=p->next.END Enter your option: 1 Enter the label: FIRST Enter the address: 1002 LABEL ADDRESS FIRST 1002 SYMBOL TABLE IMPLEMENTATION 1.else { q=p->next. q=q->next.SEARCH 5.DISPLAY 3. } size--. while(strcmp(q->label.DISPLAY 3.DELETE 4.MODIFY 6.DELETE 4.SEARCH 5.INSERT 2. } } OUTPUT: SYMBOL TABLE IMPLEMENTATION 1.

END Enter your option: 4 Enter the label to be searched: SECOND The label is not found in the symbol table SYMBOL TABLE IMPLEMENTATION .DISPLAY 3.INSERT 2.DISPLAY 3.DELETE 4.DISPLAY 3.MODIFY 6.5.INSERT 2.MODIFY 6.END Enter your option: 3 Enter the label to be deleted: THIRD Label not found LABEL ADDRESS FIRST 1002 SECOND 1080 SYMBOL TABLE IMPLEMENTATION 1.MODIFY 6.SEARCH 5.END Enter your option: 2 LABEL ADDRESS FIRST 1002 SECOND 1080 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.SEARCH 5.DELETE 4.SEARCH 5.END Enter your option: 3 Enter the label to be deleted: SECOND LABEL ADDRESS FIRST 1002 SYMBOL TABLE IMPLEMENTATION 1.DELETE 4.MODIFY 6.

SEARCH 5.DELETE 4.Both the label and address Enter your choice: 2 Enter the label whose address is to be modified: RECORD Enter the new address: 2002 LABEL ADDRESS RECORD 2002 SYMBOL TABLE IMPLEMENTATION .DELETE 4.INSERT 2.INSERT 2.MODIFY 6.DELETE 4.DISPLAY 3.Only the label 2.Only the address of a particular label 3.END Enter your option: 5 What do you want to modify? 1.Both the label and address Enter your choice: 1 Enter the old label FIRST Enter the new label: RECORD LABEL ADDRESS RECORD 1002 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.MODIFY 6.1.DISPLAY 3.DISPLAY 3.SEARCH 5.END Enter your option: 4 Enter the label to be searched: FIRST The label is already in the symbol table SYMBOL TABLE IMPLEMENTATION 1.Only the address of a particular label 3.Only the label 2.END Enter your option: 5 What do you want to modify? 1.SEARCH 5.MODIFY 6.

insert. search and display is implemented in C language.DELETE 4.MODIFY 6.END Enter your option:6 RESULT: Thus a Symbol table with functions to create.Only the address of a particular label 3.SEARCH 5.END Enter your option: 5 What do you want to modify? 1.INSERT 2.SEARCH 5.Both the label and address Enter your choice: 3 Enter the old label: RECORD Enter the new label: FIRST Enter the new address: 8002 LABEL ADDRESS FIRST 8002 SYMBOL TABLE IMPLEMENTATION 1.Only the label 2.DELETE 4. modify.INSERT 2.1.DISPLAY 3.MODIFY 6.DISPLAY 3. .

2 If opcode is “WORD”.2.2 Otherwise if there is no value in the operand field then LOCCTR is set to 0 3.label[10].*fp3.*fp2. Write the input line to the intermediate file 4.1 Find if there is any operand field after “START”.dat".h> #include<conio. along with the memory address in which it is stored.2.dat".start. int locctr.Ex.2."w"). if the symbol is not available then enter that symbol into the SYMTAB.2 If there is a opcode 4. 4. To find the length of the program.4 If opcode is “RESW” then increment LOCCTR by the integer equivalent of the operand value * 3. Subtract the starting address of the program from the final value of the LOCCTR 6.Otherwise. FILE *fp1.3 If opcode is “BYTE”. Do the following steps until the opcode is END 4.h> #include<string."r").*fp4.h> #include<stdlib.length. initialize the LOCCTR to the operand value 2. fp3=fopen("out. then increment LOCCTR by 1. . Close all the files and exit PROGRAM: /* C Program to implement PASS ONE OF TWO PASS ASSEMBLER #include<stdio. clrscr(). */ 1.1 Check the Symbol table.mnemonic[10].5 If opcode is “RESB”. then increment LOCCTR by 3. then increment LOCCTR by the integer equivalent of the operand value 4. 4."w"). ALGORITHM: Open and Read the input file If the input line has the opcode “START” do the following 2.dat". fp1=fopen("input.No: 02 PASS ONE OF TWO PASS ASSEMBLER AIM: To implement pass one of a two pass assembler in C language.2. 2.2. the error message should be displayed 4.operand[10].code[10]. 4.3 Write the processed lines in the intermediate file along with their location counters 5. fp2=fopen("symtab.h> void main() { char opcode[10].1 If opcode is present in the OPTAB. then increment the LOCCTR by 3 4.

else if(strcmp(opcode.locctr)."RESW")==0) locctr+=(3*(atoi(operand))). locctr=start."%s\t%d\n". fscanf(fp1."%s". fclose(fp3). fclose(fp4). break."**")!=0) fprintf(fp2. printf("\nThe length of the program is %d". while(strcmp(opcode.label. if(strcmp(opcode. length=locctr-start.label.operand).operand). } fscanf(fp4. fclose(fp1). rewind(fp4)."END")!=0) { if(strcmp(opcode.label. fprintf(fp3. } .label.opcode. fprintf(fp3.dat".label.opcode."%s%s%s"."%s\t%s\t%s\n".length).operand). while(strcmp(mnemonic.locctr. else if(strcmp(opcode.label."START")==0) { start=atoi(operand). } if(strcmp(opcode."%d\t%s\t%s\t%s\n".locctr).operand).opcode.mnemonic)==0) { locctr+=3.opcode. } else locctr=0.label.fp4=fopen("optab. if(strcmp(label."%d\t"."END")!=0) { fprintf(fp3. } fprintf(fp3."r")."BYTE")==0) ++locctr. getch().opcode. fscanf(fp1."%s%s%s"."%s".mnemonic).operand). fscanf(fp1."WORD")==0) locctr+=3."%s%s%s". else if(strcmp(opcode."%s\t%s\t%s\n"."RESB")==0) locctr+=(atoi(operand)). fscanf(fp4.mnemonic). fclose(fp2).opcode.operand).

INPUT.DAT: ADD ADDR SUB SUBR MUL MULR DIV DIVR LDA LDB LDX LDCH STA STB STX STCH TIX J JSUB RSUB JEQ JLT JGT START END 18 90 1C 94 20 98 24 9C 00 68 04 50 0C 78 10 54 2C 3C 48 4C 30 38 34 * * SYMTAB.DAT: MAIN BEGINLDA ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** START 2000 NUM1 ADD NUM2 LDCH CHAR1 STCH CHAR2 WORD 5 RESW 1 BYTE C'A' RESB 1 END BEGIN OPTAB.DAT: BEGIN2000 NUM1 NUM2 CHAR1 CHAR2 2012 2015 2018 2019 .

DAT: MAIN 2000 2003 2006 2009 2012 2015 2018 2019 2020 START BEGINLDA ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** 2000 NUM1 ADD NUM2 LDCH CHAR1 STCH CHAR2 WORD5 RESW 1 BYTE C'A' RESB 1 END BEGIN RESULT: Thus pass one of a two pass assembler is implemented in C language.OUT. .

"r"). int i.locctr."\t%s\t%s\t%s\n". fp1=fopen("twoout.locctr.opcode."START")==0) { fprintf(fp1.opcode. len=strlen(operand). opcode and operand field values of the corresponding statement directly to the final output file.dat". fp3=fopen("out."w"). 2. ALGORITHM: 1. opcode and operand fields of the corresponding statement to the output file.label.opcode[10]. FILE *fp1.&locctr.actual_len. 3.label[10]. along with the object code. fp4=fopen("optab.dat".dat".label.ad[10].label. clrscr().h> #include<stdlib. Open and read the first line from the intermediate file.operand)."%d%s%s%s".operand).symbol[10]. if(strcmp(opcode.h> #include<conio."r"). PROGRAM: /* C Program to implement PASS TWO OF TWO PASS ASSEMBLER #include<stdio.add."%s%s%s". WORD.operand[10].h> #include<string.len.2 If there is no symbol/label in the operand field."BYTE")==0) { fprintf(fp1. fscanf(fp3. Close the files and exit 4.*fp2.dat".h> void main() { char a[10].*fp4. until an “END” statement is reached. } while(strcmp(opcode. then the operand address is assigned as zero and it is assembled with the object code of the instruction 3.operand). then write the label.code.opcode."r"). fscanf(fp3.operand). If the first line contains the opcode “START”. RESB etc convert the constants to the object code. 3. Do the following steps.opcode.mnemonic[10].*fp3.No: 03 PASS TWO OF TWO PASS ASSEMBLER AIM: To implement pass two of a two pass assembler in C language. 3. */ . fp2=fopen("symtab.Ex."%d\t%s\t%s\t%s\t".1 Start writing the location counter."END")!=0) { if(strcmp(opcode.label.3 If the opcode is BYTE.

operand.operand.mnemonic.locctr."%s%d".i++) { itoa(operand[i]."%s%d". fclose(fp1).locctr."%d\t%s\t%s\t%s\t00000%s\n".opcode.locctr. } . while(strcmp(operand.symbol.symbol.code)."%d\t%s\t%s\t%s\t%d0000\n".label."%s%d".operand).symbol)!=0) { fscanf(fp2.opcode.&add). fscanf(fp4."%d\t%s\t%s\t%s\n"."RESW")==0)) { fprintf(fp1. fscanf(fp2. } fprintf(fp1. fclose(fp3).i<(actual_len+2).label. } fprintf(fp1.opcode. } else { rewind(fp4).add).operand)."%d\t%s\t%s\t%s\n"."%s".&code). fclose(fp2). while(strcmp(opcode."**")==0) { fprintf(fp1.operand.operand).opcode.&code)."%d\t%s\t%s\t%s\t%d%d\n".mnemonic. fprintf(fp1.opcode."%s%d".16)."RESB")==0)||(strcmp(opcode.10). } else if((strcmp(opcode. getch(). } else if(strcmp(opcode."\n"). itoa(atoi(operand). } else { rewind(fp2). if(strcmp(operand.label.ad. printf("FINISHED").ad).&add)."%d%s%s%s\n". fprintf(fp1. } } fscanf(fp3.locctr.code.actual_len=len-3. } fprintf(fp1.opcode.&locctr.label. fclose(fp4)."WORD")==0) { len=strlen(operand).label.a. for(i=2.mnemonic)!=0) fscanf(fp4.a).label.locctr.

INPUT.DAT: BEGIN2000 NUM1 NUM2 CHAR1 CHAR2 2012 2015 2018 2019 .DAT: MAIN BEGINLDA ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** OPTAB: ADD ADDR SUB SUBR MUL MULR DIV DIVR LDA LDB LDX LDCH STA STB STX STCH TIX J JSUB RSUB JEQ JLT JGT START END 18 90 1C 94 20 98 24 9C 00 68 04 50 0C 78 10 54 2C 3C 48 4C 30 38 34 * * START 2000 NUM1 ADD NUM2 LDCH CHAR1 STCH CHAR2 WORD 5 RESW 1 BYTE C'A' RESB 1 END BEGIN SYMTAB.

DAT MAIN 2000 2003 2006 2009 2012 2015 2018 2019 2020 START BEGINLDA ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** 2000 NUM1 ADD NUM2 LDCH CHAR1 STCH CHAR2 WORD5 RESW 1 BYTE C'A' RESB 1 END BEGIN TWOOUT. .DAT: 2000 2003 2006 2009 2012 2015 2018 2019 2020 MAIN BEGINLDA ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** START 2000 NUM1 002012 ADD NUM2 182015 LDCH CHAR1 502018 STCH CHAR2 542019 WORD5 000005 RESW 1 BYTE C'A' 41 RESB 1 END BEGIN RESULT: Thus pass two of a two pass assembler is implemented in C language.OUT.

then the operand address is assigned as zero and it is assembled with the object code of the instruction 4.3 If opcode is “BYTE”.1 If opcode is present in the OPTAB. along with the memory address in which it is stored. of mnemonics in the array A void main() { */ . then increment LC by 3. then increment LC by 1.2.2.5 If opcode is “RESB”.h> #include<string. To find the length of the program.1 Find if there is any operand field after “START”. 4. opcode and operand fields of the corresponding statement to the output file. Do the following steps until the opcode is END 4. Otherwise. the error message should be displayed 4.h> #define q 11//no.2.2 Otherwise if there is no value in the operand field then LC is set to 0 3. If the input line has the opcode “START” do the following 2.2 If there is a opcode 4. Write the input line to the intermediate file 4.Ex.No: 04 SINGLE PASS ASSEMBLER AIM: To implement a single pass assembler in C language.4 If opcode is “RESW” then increment LC by the integer equivalent of the operand value * 3.2. then increment LC by the integer equivalent of the operand value 4. ALGORITHM: 1.1 Check the Symbol table. Close all the files and exit PROGRAM: /* SINGLE PASS ASSEMBLER #include<stdio. if the symbol is not available then enter that symbol into the SYMTAB.2.2.2. Subtract the starting address of the program from the final value of the LC 6.6 If there is no symbol/label in the operand field. 4. 4.7 Write the processed lines in the intermediate file along with their location counters 5. Open and Read the input file 2.2 If opcode is “WORD”. then increment the LC by 3 and Start writing the location counter. along with the object code. initialize the LC to the operand value 2. 4.

"JEQ"."03"."J"."28". fp1=fopen("INPUT."SUB"."24"."%s\t%s\t%s".". fp1=fopen("INPUT.lab."-------------------------------------").i<n.val[10].line=1."LINE NO. fprintf(fp3. fprintf(fp2.t*3). fprintf(fp3."J".op.f=0."%s\t%s\t%x"."STL".ni=0."0C"}.m*3)."16".op[10].".n=0. fprintf(fp4.*fp3."LDA".sym[i])==0) { f=1. char b[20][15]={"14". t++.DAT"."34".f1=0.lab.\t|ERROR FOUND\n"). fprintf(fp2."COMP". clrscr()."END")==0) break."%s\t%s\t%s".DAT"."%x"."r"). . if(m>10) fprintf(fp2. while((!feof(fp1))) { fscanf(fp1. char sym[15][10].l."ADD".num."SYMBOL\tADDRESS").t=0.&lc).j. if(strcmp(lab."-------------------------------------\n").address."H^%s^00%x^%x\n". fp2=fopen("OBJFILE.*fp4. fprintf(fp3.lc."T^00%x^"."w").")==0) m=0.val).val). fp3=fopen("ERROR. m++."r")."SUB"}.m=0.i=0. m--. fp4=fopen("SYMTAB."69".t1.lab."1E"). } t=t-1."w").err=0. fscanf(fp1. else fprintf(fp2. int s."w").op.DAT".DAT"."32".lc).ad.")!=0&&(!feof(fp1))) { fscanf(fp1.i++) { if(strcmp(lab."30". else if(strcmp(op. line++. fclose(fp1)."LDB". FILE *fp1. if(strcmp(op. int symadd[15].op."$")!=0) { for(i=0."48"."STCH".DAT".int lc.code[10]. s=lc. char lab[10]. char a[20] [15]={"STA".*fp2.lab. while((op.

break."BYTE")==0))) { for(i=0. } code[j]='\0'.} } num=atoi(val)."%s\t%s\t%s". else if(strcmp(op."END")==0) break. } f=0.lab. ni++. } else lc=lc+3.lc)."BYTE")==0) { num=strlen(val)-3.i<n. if((strcmp(lab.err++.i++) { code[j]=val[i]."RESW")!=0||strcmp(op."END")!=0&&(!feof(fp1))) { fscanf(fp1. } if(f==1){ fprintf(fp3."$")!=0)&&((strcmp(op. lc=lc+num. fprintf(fp4.i<strlen(val)-1. for(i=2.line)."WORD")!=0||strcmp(op."%d\t\t|SYMBOL ALREADY DEFINED\n". symadd[n]=lc."\n%s\t%x".j=0.i++) { if(strcmp(lab. if(strcmp(op.". if(strcmp(op. } if(f==0) { strcpy(sym[n]."RESB")==0) lc=lc+num.lab). . line++.code).op. } while(strcmp(op. n++."^%s". else if(strcmp(op.lab. j++.")==0) break.sym[i])==0) { f=1.val). if(strcmp(op."RESB")!=0|| strcmp(op. fprintf(fp2."RESW")==0) lc=lc+(num*3).

n++.err++. } else{ fprintf(fp3. ni++. f1=0.lab)."BYTE")==0) fprintf(fp3. symadd[n]=lc. } f=1.lab.line).lc)."\n%d\t\t|WRONG OPCODE".code.i++) { if(strcmp(val."RESB")!=0&&strcmp(op. break.address)."WORD")==0|| strcmp(op."\n%d\t\t|UNDEFINED SYMBOL"."WORD")! =0&&strcmp(op.err++.break. fprintf(fp4. } else ."RESB")==0||strcmp(op.} for(i=0. } if(f1==1){ fprintf(fp3.err++."^%s%x".i<q.} } if(ni<10) { fprintf(fp2. } f=0. break.i++) { if(strcmp(op. } if(f==0) { strcpy(sym[n]."RESW")!=0&&strcmp(op."\n%d\t\t|SYMBOL ALREADY DEFINED").sym[i])==0) { address=symadd[i]. if(strcmp(op.i<n.a[i])==0) { strcpy(code."BYTE")!=0) { for(i=0. } f1=1.line). } if(f){ fprintf(fp3. f=0.line)."RESW")==0||strcmp(op."\n%s\t%x".b[i]).} } else if(strcmp(op."\n%d\t\t|Declaration not allowed here".

ni=0. } else { fprintf(fp2."T^00%x^".address). } INPUT.s). fprintf(fp3.m*3). } } lc=lc+3."%x"."\nE^00%x". } fprintf(fp2. fprintf(fp2. fcloseall().DAT")."No of errors=%d\n--------------------------------------". getch().err).DAT OBJCODE. $ FIRST STA RETADR $ STL BUFFER $ J FIRST $ END START OUTPUT SCREEN: Output file:OBJCODE.DAT Errors are described in ERROR.DAT\nErrors are described in ERROR.DAT .DAT H^COPY^001000^18 T^001000^9^EOF^141000^321003^34100d E^001000 SYMTAB. m=m-10.code.{ fprintf(fp2.DAT Symbol table is in the file:SYMTAB."^%s%x".lc)."1E").DAT\nSymbol table is in the file:SYMTAB. printf("Output file:OBJCODE. if(m>10) { fprintf(fp2.DAT COPY START 1000 RETADR RESW 1 BUFFER RESB 4 EOF BYTE C`EOF` $ .

DAT ADDRESS 1000 1003 ------------------------------------LINE NO. .SYMBOL RETADR BUFFER EOF 1007 FIRST 100d ERROR. |ERROR FOUND -------------------------------------No of errors=0 -------------------------------------- RESULT: Thus single pass assembler is implemented in C language.

.*deftable.txt". int beg. int cnt = 0."r").opcode).h> char optable[50][20]. }namtab[5]."%s %s".*namtable.opcode[20].mnemonic.No: 05 TWO PASS MACRO PROCESSOR AIM: To implement two pass macro processor in C language. deftable = fopen("deftab.txt". }deftab[30].mnemonic). optab = fopen("optab. strcpy(optable[cnt++]. namtable = fopen("namtab.txt"."r"). char mnemonic[20]. do { fscanf(optab."r"). int def_cnt = 0. ALGORITHM: PROGRAM: /* program to Perform pass2 of macro */ #include <stdio.Ex.arg_cnt = 0. void initialize() { FILE *optab. char operand[30].nam_cnt = 0. FILE *exp. char argtab[20][10]. int end. struct name { char MacroName[20]. struct definition { char instruction[20].

char *args) { FILE *argtbl. } void expand(char *name.index.deftab[def_cnt].beg. nam_cnt++.txt".&namtab[nam_cnt]. for(i=0. def_cnt++.i<nam_cnt.&namtab[nam_cnt].namtab[nam_cnt]. } int iskeyword(char *str) { int i.end).end. }while(!feof(deftable)). argtbl = fopen("argtab. do { fscanf(namtable.instruction.MacroName.i++) if(!strcmp(optable[i]. for(i=0. fclose(optab). fclose(deftable).i<cnt.i.}while(!feof(optab)).str)) return 1.i<nam_cnt.i++) if(!strcmp(namtab[i]. for(i=0. return 0.deftab[def_cnt].j."%s %d %d". } int ismacro(char *str) { int i."a+").str)) return 1. fclose(namtable). char operand[30]. do { fscanf(deftable.tmp[20]."%s %s".i++) . int beg. }while(!feof(namtable)).operand).MacroName. return 0.

}while(args[i] != '. } fclose(argtbl).deftab[i].operand). do { argtab[arg_cnt][j++] = args[i++].i<=end.name)) { beg = namtab[i].i<arg_cnt. do { j=0.beg. .{ if(!strcmp(namtab[i]. strcpy(operand. for(i=0."%s\n".' && args[i] != '\0').j<strlen(operand).MacroName.deftab[i].j++) { if(operand[j] == '?') { operand[j] = '%'. arg_cnt++. for(j=0. argtab[arg_cnt][j] = '\0'. } for(i=beg+1.i++) { fprintf(exp.tmp). i=0. } } fprintf(exp.i++) { fprintf(argtbl. operand[j+1] = 's'.operand.argtab[i]). index = operand[j+1]-48. }while(args[i++] != '\0').argtab[index]).end.operand).instruction)."\t%s\t". sprintf(tmp. getch()."%s ". } } arg_cnt = 1.name). strcpy(operand. end = namtab[i]."\n%s :". fprintf(argtbl.

do { fscanf(source.str1)."w+").str1)."%s".} void main() { FILE *source.txt"."MACRO")) { strcpy(optable[cnt++]. fscanf(source.txt"."."\t%s\t%s\n".str1)."w+")."%s". if(ismacro(str1)) .*argtbl.str."r")."MEND")). expand(str. }while(strcmp(str. } else fprintf(exp."%s %s".str1).str.str2).str. } else if(iskeyword(str)) { if(ismacro(str)) { fprintf(exp.txt". initialize().\t%s\t%s\n".str2[30]. exp = fopen("exppgm.str2)."%s". beg: if(feof(source)){} else if(!strcmp(str1. fclose(argtbl).str1).str).str1)."%s %s". source = fopen("prog. char str[30]. goto beg. fscanf(source. int i. } else { fscanf(source.str1). strcpy(str.str1[30]. do { fscanf(source. argtbl = fopen("argtab.str.

} }while(!feof(source)). expand(str1.TXT EXPPGM.TXT OPTAB.TXT DEFTAB.str1.str2). fprintf(exp.TXT RESULT: Thus two pass macro processor is implemented in C language.str. } else fprintf(exp.str). } OUTPUT: PROG.".str2).TXT NAMTAB."%s".TXT ARGTAB. .str.str1."%s\t%s\t%s\n".str2).{ fprintf(exp.%s\t%s\t%s\n". fclose(source).

ilab). while(strcmp(iopd.i. fscanf(fp1.3 Enter the lines in the body of each macro in to the corresponding files already opened in step 4 5.iopd[20].NAMTAB[20][20]."MACRO")==0) { strcpy(NAMTAB[n].1 Enter the macro name present in the operand field 5. PROGRAM: /* C Program to implement SINGLE PASS MACRO PROCESSOR #include<stdio. 2.Ex. 5. then the number of macro “n” will be incremented by 1 Repeat the steps 1 and 2 until an end of file is encountered Open “n” number of macro files in write mode and rewind the input file pointer If the directive is “MACRO” then. ALGORITHM: 1.No: 06 SINGLE PASS MACRO PROCESSOR AIM: To implement a single pass macro processor in C language."%s%s%s".h> #include<conio.iopd. n=0."MEND")!=0) */ .oper).flag. 6.dat". rewind(fp1). fp1=fopen("macroin.*fp2.h> #include<stdlib.iopd. while(!feof(fp1)) { if(strcmp(iopd.h> #include<string. 3."w").dat". do the following 5."%s%s%s". 4.ilab.h> void main() { int n. char ilab[20]. Get the statement from the input file If the statement has the directive “MACRO”.oper).ilab. FILE *fp1. fp2=fopen("macroout."w")."r"). fscanf(fp1.oper[20]. DEFTAB=fopen(NAMTAB[n]. clrscr().4 Write the body of each macro to the expanded output file until a “MEND” is reached Write the remaining lines directly to the expanded file.*DEFTAB.2 Write the line to the expanded output file 5.

iopd. } fclose(DEFTAB).oper).ilab."%s\t%s\t%s\n".iopd."%s\t%s\t%s\n". n++.iopd.i++) { if(strcmp(iopd.oper). } break.iopd.ilab. fscanf(DEFTAB. while(!feof(DEFTAB)) { fprintf(fp2. fscanf(fp1.NAMTAB[i])==0) { flag=1. DEFTAB=fopen(NAMTAB[i].iopd. } } if(flag==0) fprintf(fp2.ilab.oper).oper).ilab. } fprintf(fp2. for(i=0."%s\t%s\t%s\n".oper).ilab. getch().oper).oper). } .iopd. } else { flag=0."%s%s%s"."%s\t%s\t%s\n".i<n.{ fprintf(DEFTAB."%s%s%s".iopd.ilab."%s%s%s\n"."%s%s%s"."r").iopd. fscanf(DEFTAB.ilab. } fscanf(fp1.ilab.oper).

DAT M1 ** ** ** ** M2 ** ** ** ** M3 ** ** ** ** ** ** ** ** ** MACRO LDA ADD STA MEND MACRO LDA SUB STA MEND MACRO LDA MUL STA MEND START M3 M2 M1 END ** N1 N2 N3 ** ** N1 N2 N4 ** ** N1 N2 N5 ** 1000 ** ** ** ** MACROOUT.MACROIN. .DAT ** ** ** ** ** ** ** ** ** ** ** START LDA MUL STA LDA SUB STA LDA ADD STA END 1000 N1 N2 N5 N1 N2 N4 N1 N2 N3 ** RESULT: Thus a single pass macro processor is implemented in C language.

FILE *fp1.input[0]."%d\t%c%c\n"."%d"."%s". int start. fscanf(fp1.h> #include<conio.address. clrscr().input[1]). fprintf(fp2. fp1=fopen("input.&start).&address). Read first Text record from the input file 4."w").length. */ .4 Read next Text record from the input file 5. Verify program name and length 3. Process the following steps until an End record is reached 5.3 Write the starting location counter value of a block of object code and the corresponding internal representation to the output file 5. } else if(strcmp(input."T")==0) { fscanf(fp1.No: 07 ABSOLUTE LOADER AIM: To implement an Absolute loader in C language. convert it to internal hexadecimal representation 5. while(strcmp(input."%s". fscanf(fp1. ALGORITHM: 1. fp2=fopen("output.input)."r").input)."H")==0) { fscanf(fp1.Ex. fscanf(fp1.2 Move object codes to specified locations in memory 5."E")!=0) { if(strcmp(input. fscanf(fp1.&length). Go to the address specified in End record 6.dat". Close all the files and exit PROGRAM: /* C Program to implement ABSOLUTE LOADER #include<stdio."%s".1 If object code is in character form.h> #include<string."%d"."%d". Read the Header record 2.address.*fp2.h> void main() { char input[10].dat".input).

input[5])."%d\t%c%c\n".DAT: 1000 14 1001 20 1002 33 1003 48 1004 30 1005 39 1006 10 1007 20 1008 36 2000 29 2001 83 2002 00 2003 23 2004 00 2005 00 2006 28 2007 20 2008 30 2009 30 2010 20 2011 15 RESULT: .input[4].input[3]).input[1]). fprintf(fp2.input[2]."%s". fscanf(fp1.(address+1). getch(). address+=3.(address+2).input[5]). fprintf(fp2. } else { fprintf(fp2. printf("FINISHED"). fprintf(fp2."%d\t%c%c\n". } INPUT. address+=3.input[3]).DAT: H 1000 232 T 1000 142033 483039 102036 T 2000 298300 230000 282030 302015 E OUTPUT. fclose(fp2)."%d\t%c%c\n".input[0].input[4]. fscanf(fp1."%d\t%c%c\n".input).address.fprintf(fp2.input[2]. } } fclose(fp1).input).(address+1).(address+2)."%d\t%c%c\n"."%s".

.Thus an Absolute loader is implemented in C language.

Write the starting location counter value of a block of object code and the corresponding internal hexadecimal representations to the output file PROGRAM: /* C Program to implement RELOCATING LOADER #include<stdio. Move object codes to specified locations in memory 11. clrscr().dat".dat". while(strcmp(input.input[10]. Move the consecutive next three strings from input to output 5. which is the relocation bit associated with each text record to binary form 6.actualadd."H")==0) { fscanf(fp1.h> #include<conio. */ ."%s". Repeat steps from 2 to 7 until end record is encountered 9.*fp2. fscanf(fp1.input).relocbit. convert it to internal hexadecimal representation 10. until ‘T’ is encountered 4.address. Convert the current string. If object code is in character form. fscanf(fp1. fp1=fopen("input. printf("Enter the actual starting address: ").add). Transfer the strings from input file to output file.i.h> #include<stdlib.opcode.&start). Read the content of the input file as strings one at a time 3.binary[12].length).Ex.bitmask[12]. Enter the new starting location to which the object code has to be relocated 2.h> void main() { char add[6]."%s".length[10]."E")!=0) { if(strcmp(input. Make the necessary changes in the corresponding words of object code by adding the new starting address with the address part of the object code for which the corresponding relocation bit is set and store the updated object code in the output 7.input).No: 08 RELOCATING LOADER AIM: To implement a Relocating loader in C language. fp2=fopen("output. fscanf(fp1. int start. FILE *fp1."w"). scanf("%d"."r")."%s".len.h> #include<string.addr.inp. ALGORITHM: 1. Move the object code for which the corresponding relocation bit is not set from output to input without change 8."%s".

&address). for(i=0. } } fclose(fp1). fprintf(fp2. address+=3. } INPUT. fscanf(fp1."%s". address+=start. else actualadd=addr+start. fscanf(fp1.input)."%d".} if(strcmp(input."%d".DAT: 7000 147033 7003 487039 7006 901776 7009 921765 7012 577765 8011 237838 8014 437979 8017 897060 8020 667849 8023 991477 RESULT: .i<len.opcode. relocbit=bitmask[i].bitmask).DAT: H 1000 200 T 1000 11001 14 1033 48 1039 90 1776 92 1765 57 1765 T 2011 11110 23 1838 43 1979 89 1060 66 1849 99 1477 E 1000 Enter the actual starting address: 7000 OUTPUT. len=strlen(bitmask)."%d". fclose(fp2). } fscanf(fp1.&addr).actualadd)."T")==0) { fscanf(fp1. if(relocbit=='0') actualadd=addr. printf("FINISHED").&opcode).i++) { fscanf(fp1."%s"."%d\t%d%d\n".address. getch().

.Thus a Relocating loader is implemented in C language.

ALGORITHM: 1. rewind(fp1).No: 09 PASS ONE OF DIRECT LINKING LOADER AIM: To implement pass one of direct-linking loader in C language. clrscr().dat". 5. b.*fp2.h> struct estab { char csect[10].&start). int i."CSect\tSym_Name\tAddress\t\tLength\n\n")."w"). 3. Enter the location where the program has to be loaded Assign the address got from the user as the first control section address Read the header record of the control section From the details of the header read and store the control section length in a variable Enter the control section name with its address into the external symbol table For each symbol in the subsequent ‘D’ records the symbol must be entered into the symbol table along with its address. scanf("%x".length."r"). printf("\nEnter the location where the program has to be located: ").dat". void main() { char input[10].h> #include<string.Ex.h> #include<stdlib. FILE *fp1. 4.start. a. int add. 6. while(!feof(fp1)) */ . char sym_name[10].count=0. fp2=fopen("linkout.h> #include<conio. fprintf(fp2.length. fp1=fopen("linkin. added along with the corresponding control section until the END record is reached Assign the starting address of next control section as the address of the current control section plus the length of the control section Repeat the process from step 3 to 5 until there is no more records PROGRAM: /* C Program to implement PASS ONE OF DIRECT LINKING LOADER #include<stdio. 2.loc. }table[10].

} while(strcmp(input."D")==0) { fscanf(fp1. table[count].add."%s".csect."T")!=0) fscanf(fp1."%s".table[i]. start=start+length.i++) fprintf(fp2.input).i<count. } if(strcmp(input.&loc). getch(). } for(i=0.input).DAT H D R T T PROGA LISTA LISTB 00002010 00005416 000000000070 000040ENDA 000054 ENDB LISTC ENDC 03201077100004 15001 10001415100006 00002F100014 ."**")."E")!=0) fscanf(fp1.length=length. while(strcmp(input. fscanf(fp1.input).&length).add=atoi(input)+start.input.input).table[i]. strcpy(table[count]. if(strcmp(input. } OUTPUT: Enter the location where the program has to be located: 5075 LINKIN."R")!=0) { strcpy(table[count]. fscanf(fp1."%s"."%s\t%s\t\t%x\t\t%x\n". fscanf(fp1. strcpy(table[count].table[i]. fcloseall()."%s".&loc).csect.csect.input.input).add=loc+start. table[count++].input)."**")."H")==0) { fscanf(fp1. fscanf(fp1.sym_name.{ fscanf(fp1.sym_name.input)."%x".sym_name.input).input)."%s"."%s". table[count++]."T")==0) while(strcmp(input. } if(strcmp(input.table[i]. strcpy(table[count]. table[count].length)."%s%x". fscanf(fp1."%s".length=0."%s%x".

M M M M E H D R T T M M M M M M E H D R T T M M M M M M E 00002405 000054 00005806 00006406 000000 PROGB LISTB LISTA 00003611 00007018 00003705 00004405 00007006 00007406 00007806 00008206 000000 PROGC LISTC LISTA 00001812 00004215 00001905 00002305 00002705 00004806 00005106 00005406 000000 06 +LISTB +LISTC +ENDC +ENDC 000000000088 000060ENDB 000070 ENDA LISTC ENDC 03100000 7720270510030 10000005100006 0510020 +LISTA +ENDA +ENDA +ENDC +ENDC +ENDA 0510030 000000000057 000030ENDC 000042 ENDA LISTB ENDB 03100000 77100004 05100000 100030100008100011100000 +LISTA +LISTB +ENDA +LISTA +ENDA +LISTB LINKOUT.DAT CSect PROGA ** ** PROGB ** ** PROGC ** ** RESULT: Sym_Name ** LISTA ENDA ** LISTB ENDB ** LISTC ENDC Address 5075 50b5 50c9 50e5 5145 5155 516d 519d 51af Length 70 0 0 88 0 0 57 0 0 .

Thus pass one of direct-linking loader is implemented in C language. .

"%s".val[40].name[20]. fp6=fopen("six.ptn[40]. ALGORITHM: 1."%s%s%x%s".mod[40][40].*fp4.sign[40] [1].lent[20].m2.Ex.*fp5. Assign the address got from the user as the first control section address 3.*fp2.*fp8."w")."r"). fp2=fopen("ESTAB.l++) { if(l==1) fp3=fopen("ntempb. Read the header record of the control section i.m1.st. added along with the corresponding control section until the END record is reached 5.txt".h> #include<conio.*fp3. Repeat the process from step 3 to 5 until there is no more records PROGRAM: /* C Program to implement PASS TWO OF DIRECT LINKING LOADER */ #include<stdio.count=0.len[20].est2[20].string[1000].txt". From the details of the header read and store the control section length in a variable ii. . For each symbol in the subsequent ‘D’ records the symbol must be entered into the symbol table along with its address."w").t[20]. Enter the control section name with its address into the external symbol table 4. int l. if(l==2) fp3=fopen("ntempc. fp4=fopen("memory.*fp7."w").*fp6.l<3.progstart .txt". fp1=fopen("DLL_IN.&st.temp.txt".est4[20].txt".No: 10 PASS TWO OF DIRECT LINKING LOADER AIM: To implement pass two of direct-linking loader in C language.est3.num.txt". char rec[20]. for(l=0. fscanf(fp1. fp3=fopen("ntemp. Enter the location where the program has to be loaded 2."r")."w").name.t).k=0. fscanf(fp1.est1[20].add[20].len).start[20].h=0.ptn2[20][20].txt".z1=0.offset[40].rec. Assign the starting address of next control section as the address of the current control section plus the length of the control section 6."w").j.h> main() { FILE *fp1. int ptn1[20][20].i.

mod[k]). z1++. fscanf(fp1. break.est4).name. if(h!=0) { for(i=0."%s".&start[h]."M")==0) { fscanf(fp1. } else if(strcmp(t. } else if(strcmp(t.t). if(l!=2) fscanf(fp1. break.&st. fscanf(fp2. } h++."%s". }while((strcmp(t."T")!=0)&&(strcmp(t."%s".i<(start[h]-(start[h-1]+lent[h-1])).sign[k].est1)==0) { val[z1]=est3.t). break. z1++."%x%x%s%s". } else if(strcmp(t.est2."R")==0) { while(strcmp(t.est4)."%s"."%s%s%x%s".&est3."D")==0) { . } fscanf(fp2.do{ if(strcmp(t.len).rec. do{ fprintf(fp3. } else if(strcmp(t.i++) fprintf(fp3."%s".est2)==0){ val[z1]=est3. } else if(strcmp(mod[k]."x"). fscanf(fp1.&ptn[k].t).&lent[h]).t)."%s". }while(!feof(fp2)). do{ if(strcmp(mod[k].est1. k++."T")!=0) fscanf(fp1.t)."%s%s%x%s".&offset[k]."T")==0) { fscanf(fp1. fscanf(fp1."E")==0) { fscanf(fp1."%s%s%x%s"."M")!=0)). progstart=est3.est1.est2."%x%x".&est3.t). rewind(fp2).

} k=0. fprintf(fp3."%s".i++){ if(l==0) fp3=fopen("ntemp.z1=0. fclose(fp3). if(l==1) fp3=fopen("ntempb. fscanf(fp3. fprintf(fp5."00%x". fp5=fopen("temp1.txt".string).j++) { fscanf(fp3.num). fscanf(fp3. if(offset[i]==5) fprintf(fp3. } else { num=num-val[i].j<offset[i]."R")!=0) fscanf(fp1.&num). fclose(fp3). fclose(fp5). fclose(fp3).num)."r"). for(i=0."%s".txt"."\n").while(strcmp(t."%s"."r")."r").0)."0%x".string). } fprintf(fp5.(ptn[i]*2)+1. else fprintf(fp3.txt".string). .txt"."r+").txt"."w").temp).h=0.t).num). fscanf(fp5.string)."%x"."r+"). fseek(fp3.0). fseek(fp3. fp5=fopen("temp1."%s"."%c".(ptn[i]*2)+1. } }while(1). fp3=fopen("ntempb. fclose(fp5).&temp).i<k. fseek(fp3."00%x". } fp3=fopen("ntemp. if(sign[i][0]=='+') { num=num+val[i]. if(l==2) fp3=fopen("ntempc. fprintf(fp6. } fclose(fp3).(ptn[i]*2)+1."%c"."r+").txt".0)."%s".txt". fprintf(fp6. for(j=0.

i++) { if(i==0) { fprintf(fp4. } DLL_IN. } return 0."\t"). for(i=0. } fprintf(fp4. progstart+=16."r").txt". fprintf(fp6. fscanf(fp6. fprintf(fp4. fclose(fp6). progstart+=16."%x\t".string[i]).TXT H PROGA 000000 00003A D LISTA 000030 ENDA 000050 . fp6=fopen("six."\n").i<strlen(string).txt".progstart). R LISTA ENDA T 000000 1D B410 B400 B440 77201F E3201B 332FFA DB2015 A004 332009 57900000 B850 T 000020 0E 3B2FE9 13100000 4F0000 F1000000 M 000007 05 + LISTA M 000022 05 + ENDA E 000000 H PROGC 000000 00001C D LISTC 000030 ENDC 000042 . fclose(fp3).string)."%c"."%s"."%s".progstart)."%s".fp3=fopen("ntempc."%x\t".string). if((i%32==0)&&(i!=0)) { fprintf(fp4.string)."r"). R LISTA ENDA T 000000 1C B410 77100000 E32012 332FFA 53900000 DF2008 B850 3B2FEE 4F000005 M 000006 05 + LISTA M 000013 06 + ENDA E 000000 . fscanf(fp3. R LISTB LISTC ENDC T 000000 1D 172027 4B100000 032023 290000 332007 4B100000 3F2FEC 032016 0F2016 T 00001D 0D 010003 0F200A 4B100000 3E2000 M 000004 05 + LISTB M 000011 05 + LISTC E 000000 H PROGB 000000 00002E D LISTB 000060 ENDB 000070 . } if((i%8==0)&&(i!=0)) fprintf(fp4.

LISTB 0030c3 .ENDA 003050 PROGB .0030e2 000051 .003063 00007f .LISTC 003112 .003000 000063 .ENDB 0030d3 PROGC .ESTAB.TXT PROGA .LISTA 003030 .TXT 3000 3010 3020 3030 3040 3050 3060 3070 1720274B 4B103112 0F200A4B 77205013 09579000 F0000F10 32FFA539 00005 1030c303 3F2FEC03 1000003E 201B332F 00B850xx 00000B41 00000DF2 20232900 20160F20 2000B410 FADB2015 x3B2FE91 07710000 008B0034 00332007 16010003 B400B440 A0043320 30305004 0E050423 02FEE4F0 RESULT: Thus pass two of direct-linking loader is implemented in C language. .ENDC 003124 – MEMORY.

printf("1.Ex. printf("2.New\n").Modify\n"). open the file in read mode else display not found If choice is “3” then replace the existing file by a new file name If choice is “4” then insert character or word or sentence PROGRAM: /* #include<stdio.Save As\n"). printf("Enter u'r choice: "). printf("3. Design a Menu using switch case Get the choice If choice is “1” then.h> void newf(). printf("4.Exit\n"). enter the filename in which the text is to be saved and. 2. 5. 6. scanf("%d". If the filename is found.h> #include<stdlib. clrscr(). 4.No: 11 SIMPLE TEXT EDITOR AIM: To implement simple text editor with features like insertion/deletion of a character.&op). void saveas(). do { printf("\nMENU\n").Open\n"). printf("5. void modify(). type the text using editor and type CTRL+Z to terminate If choice is “2” then enter the filename to be open.h> #include<string.h> #include<conio. void view(). switch(op) { case 1: SIMPLE TEXT EDITOR */ . ALGORITHM: 1. void main() { int op. word and sentence in C language. 3.

c. printf("\nEnter the Filename: ").f). break. while((c=getchar())!=EOF) { putc(c. } void view() { FILE *f. f=fopen(fname. default: printf("\nWrong choice!!!"). case 5: exit(0).fname). printf("\nType the content and CTRL+Z to terminate:\n"). break. } } else printf("\nThe file %s does not exist". fclose(f).c. } void newf() { FILE *f. . while((c=getc(f))!=EOF) { printf("%c". scanf("%s"."w"). printf("\nEnter the name of the file:"). case 2: view(). char fname[20]. rewind(f). scanf("%s". char fname[20]. case 4: modify(). break. } fclose(f).c)."r"). break. case 3: saveas(). break.newf().fname). } }while(op!=5). if(searchpath(fname)) { f=fopen(fname.&fname).

f2=fopen(des.Character"). } } else if(ch1==2) { printf("Enter the word: ").f2). scanf("%d". printf("\n3. } fclose(f1). fseek(f1.Word").Sentence"). FILE *f1.f1). while((c=getc(f1))!=EOF) { putc(c.des[20]. char c. printf("Enter the filename to be modified: "). printf("\nEnter the Source file name: ").*sent. getch(). printf("\n2. printf("Enter the character and CTRL+Z to exit:\n ")."a+").} void saveas() { FILE *f1. } else printf("\nFile does not exist").*word.sou[20]. char c. while((c=getchar())!=EOF) { putc(c. 0L.fname).fname[20].sou). if(searchpath(fname)) { printf("\n1. SEEK_END).&ch1)."r").des).*f2. scanf("%s". if(ch1==1) { f1=fopen(fname. if(searchpath(sou)) { printf("Enter the Destination file name: "). } void modify() { int ch1. printf("\nEnter U'r choice: "). scanf("%s"."w"). scanf("%s". . f1=fopen(sou. fclose(f2).

Exit Enter u'r choice: 2 Enter the name of the file:c. Arrays 2. SEEK_END).Modify 5. } else { printf("Enter the sentence and CTRL+Z to exit: "). String 3."a+"). fcloseall(). fseek(f1.scanf("%s".txt Type the content and CTRL+Z to terminate: C is a procedure oriented language It includes 1. 0L. f1=fopen(fname.Save As 4. SEEK_END).Modify 5.Exit Enter u'r choice: 1 Enter the Filename: clang. } OUTPUT: MENU 1.Save As 4.txt . fputs(word. } } } else printf("\nFilename does not exist"). Pointer etc ^Z MENU 1.f1). while((c=getchar())!=EOF) { putc(c.f1). f1=fopen(fname.New 2.Open 3. fseek(f1.word).New 2."a+"). 0L.Open 3.

String 3.txt C is a procedure oriented language It includes 1.Modify 5. Arrays 2.Open 3.Exit Enter u'r choice: 3 Enter the Source file name: clang.Modify 5.Open 3.Save As 4.Exit .New 2. Pointer etc MENU 1.Sentence Enter U'r choice: 1 Enter the character and CTRL+Z to exit: R ^Z MENU 1.The file c.Modify 5.New 2.Save As 4.txt does not exist MENU 1.Word 3.Open 3.txt 1.New 2.txt Enter the Destination file name: cprogram.Modify 5.New 2.Exit Enter u'r choice: 2 Enter the name of the file:clang.Save As 4.Save As 4.Open 3.txt MENU 1.Character 2.Exit Enter u'r choice: 4 Enter the filename to be modified: clang.

Save As 4.txt 1. Arrays 2.New 2.txt C is a procedure oriented language It includes 1.Exit Enter u'r choice: 2 Enter the name of the file:clang.Word 3.Exit Enter u'r choice: 4 Enter the filename to be modified: clang. String 3.txt 1.Modify 5.New 2.Sentence Enter U'r choice: 3 Enter the sentence and CTRL+Z to exit: IIIIIIIIIIIIII JJJJJJJJJJ KKKKKKKKK ^Z MENU 1.Word 3. Pointer etc R Template IIIIIIIIIIIIII JJJJJJJJJJ KKKKKKKKK .Character 2.Character 2.Modify 5.Enter u'r choice: 4 Enter the filename to be modified: clang.Open 3.Save As 4.Sentence Enter U'r choice: 2 Enter the word: Template MENU 1.Open 3.

MENU 1.Modify 5.Open 3. word and sentence is implemented in C language. .Save As 4.New 2.Exit Enter u'r choice:5 RESULT: Thus a simple text editor with features like insertion/deletion of a character.