Вы находитесь на странице: 1из 37

SYMBOL TABLE OPERATION AIM To create a program to implement the symbol table operation.

ALGORITHM STEP1: Start the program STEP2: Declare the structure with two character variables. STEP3: Declare the functions of insert(), delete(), search(), display() and modify. STEP4: Derive the functions separately in routines. STEP5: Print the values in the symbol table STEP 6: Stop the program. CODING #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> struct table { char var[10]; int value; }; struct table tb1[20]; int i,j,n; void create(); void modify(); int search(char variable[],int n); void insert(); void display(); void main() { int ch,result=0; char v[10]; clrscr(); do { printf("\t1.CREATE 2.INSERT 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT \n");

printf("\nenter your choice:"); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: insert(); break; case 3: modify(); break; case 4: printf("\nenter the variable to be searched:"); scanf("%s",&v); result=search(v,n); if(result==0) printf("\nthe variable doesnot belong to the table.\n\n"); else printf("\nthe location of the variable is %d.\n\nthe value of %s is %d.\n\n",result,tb1[result].var,tb1[result].value); break; case 5: display(); break; case 6: exit(1); } }while(ch!=6); getch(); } void create() { printf("\nenter the no of entries:"); scanf("%d",&n); printf("\nenter the variable and the value:\n"); for(i=1;i<=n;i++) { scanf("%s %d",tb1[i].var,&tb1[i].value); check: if(tb1[i].var[0]>='0' && tb1[i].var[0]<='1') { printf("\n the variable should startwith an alphabet.\n enter correct variable name:\n"); scanf("%s %d",tb1[i].var,&tb1[i].value); goto check; }

check1: for(j=1;j<i;j++) { if(strcmp(tb1[i].var,tb1[j].var)==0) { printf("\nthe variable already exists.\nenter another variable:\n"); scanf("%s%d",tb1[i].var,&tb1[i].value); goto check1; } } } printf("the table after creation is:\n"); display(); } void insert() { if(i>=20) printf("\ncannot insert.table is full.\n"); else { n++; printf("\nenter the variable and the value:\n"); scanf("%s %d",tb1[n].var,&tb1[i].value); check: if(tb1[i].var[0]>='0' && tb1[i].var[0]<='9') { printf("\nthe variable should start with an alphabet.\nenter currct variable name:\n"); scanf("%s %d",tb1[i].var,&tb1[i].value); goto check; } check1: for(j=1;j<n;j++) { if(strcmp(tb1[j].var,tb1[i].var)==0) { printf("\nthe variable already exists. enter another variable.\n"); scanf("%s %d",tb1[i].var,&tb1[i].value); goto check1; } } printf("\n the table after insertion is:\n"); display(); } } void modify() { char variable[10];

int result=0; printf("\n enter the variable to be modified:"); scanf("%s",&variable); result=search(variable,n); if(result==0) printf("\n%s doesnot belong to the table.",variable); else { printf("\nthe current value of the variable %s is %d.\n enter the new variable and its value:\n",tb1[result].var,tb1[result].value); scanf("%s %d",tb1[result].var,&tb1[result].value); check: if(tb1[i].var[0]>='0'&&tb1[i].var[0]<='9'); { printf("\nthe variable should start with an alphabet. \n enter currct variable name:\n"); scanf("%s%d",tb1[i].var,&tb1[i].value); goto check; } } printf("\n\nthe table after modification is:"); display(); } int search(char variable[],int n) { int flag; for(i=0;i<=n;i++) if(strcmp(tb1[i].var,variable)==0) { flag=1; break; } if(flag==1) return 1; else return 0; } void display() { printf("\nVARIABLE\t VALUE\n"); printf("\n"); for(i=1;i<=n;i++) printf("%s\t\t%d\n",tb1[i].var,tb1[i].value); printf("\n"); }

OUTPUT: 1. Create 2.Insert 3.Modify 4.Search 5.Display 6.Exit Enter your choice:1 Enter the number of entries:3 Enter the variable and Value A1 B2 C3 The table after creation is Variable Value a 1 b 2 c 3 The table after creation is Variable values a 1 b 2 c 3 1.create 2.insert 3.modify 4.search 5.display 6.exit Enter your choice:2 Enter the variable and value The table after insertion is Variable value a 1 b 2 c 3 d 4 1.create 2.insert 3.modify 4.search 5.display 6.exit Enter your choice:6

RESULT Thus the program to implement the symbol table operation has been created and executed successfully.

PASS ONE OF A TWO PASS ASSEMBLER AIM To create a program to implement the pass one of two pass assembler ALGORITHM STEP1: Start the program STEP2: Create three files namely source, intermediate and opcode files with a symbol name. STEP3: Declare the variables for all three files using arrays. STEP4: Create two files in read mode and the others in write mode. STEP5: Get the values of n and set a variable address to zero. STEP 6: Copy intermediate file and source file to opcode and symbol file. STEP 7: Print label, opcode, mnemonics from the intermediate file to output symbol file. STEP 8: Set a loop and add intermediate file address. STEP 9: Close files and stop the program. CODING #include<stdio.h> #include<conio.h> #include<stdlib.h> struct source { char label[10]; char mnem[10]; char op[10]; }s; struct opcode { int length; char mnem[10]; char op[10]; }o; struct intermediate

{ int addr; char label[10]; char mnem[10]; char op[10]; }in; struct symbol { int addr; char label[10]; }sy; void main() { int i,n,addr,leng; FILE *a,*b,*c,*d; clrscr(); a=fopen("source.txt","r"); rewind(a); b=fopen("opcode.txt","r"); rewind(b); c=fopen("inter.txt","w"); rewind(c); d=fopen("symbol.txt","w"); rewind(d); printf("\nEnter the value of n"); scanf("%d",&n); in.addr=0; for(i=0;i<n;i++) { fscanf(a,"\t%s\t%s\t%s",s.label,s.mnem,s.op); printf("\nSource is\n"); printf("\t%s\t%s\t%s",s.label,s.mnem,s.op); if(strcmp(s.mnem,"END")!=0) { sy.addr=in.addr; printf("\n%d",sy.addr); strcpy(in.label,s.label); strcpy(sy.label,in.label); strcpy(in.mnem,s.mnem); strcpy(in.op,s.op); printf("\n\tIntermediate File is"); fprintf(c,"\n%d\t%s\t%s\t%s\n",in.addr,in.label,in.mnem,in.op); printf("%d\t%s\t%s\t%s\n",in.addr,in.label,in.mnem,in.op); fscanf(b,"\n%d\t%s\t%s\n",&o.length,&o.mnem,&o.op); rewind(b); printf("\nOpcode is"); printf("\n%d\t%s\t%s\n",o.length,o.mnem,o.op);

printf("\nSymbol is\n"); printf("\t%d\t%s",sy.addr,sy.label); fprintf(d,"\n%d\t%s\n",sy.addr,sy.label); addr=atoi(s.op); if(strcmp(s.mnem,o.mnem)==0) addr=o.length; in.addr=in.addr+addr; } } fcloseall(); printf("\nPass1 is completed"); getch(); } INPUT FILES Source1.txt COPY START 500 15 ADD 2A A BYTE 10 NULL END COPY Opcode1.txt 2 ADD 3F 2 INC 4F 1 END 04 OUTPUT Enter the value of n 2 Source is COPY START 500 Intermediate file is 0 COPY START 500 Opcode is 2 ADD 3 Symbol is 0 COPY Source is 16 ADD 2A Intermediate file is 500 16 ADD 2A Opcode is 2 ADD 3 Symbol is 500 16 Pass1 is completed

OUTPUT FILES Inter1.txt 0 COPY START 500 500 15 ADD 2A 502 A BYTE 10 symbol1.txt 0 COPY 500 15 502 A

RESULT Thus the program of pass1 of two pass assembler has been implemented and executed successfully.

PASS TWO OF TWO PASS ASSEMBLER AIM To create a program to implement the pass two of two pass assembler. ALGORITHM STEP1: Start the program STEP2: Declare all the structures and variables. STEP3: Open the source, inter, symbol, opcode files in read mode and intermediate file in write mode. STEP4: Get the values of n and form the value of n, get the address, label and opcodes of the instruction. STEP5: Using strcmp function compare in.mnem and op.mnem. If true, copy the contents of opcode file to output file. STEP 6: Repeat the same for all other values, address and labels of other instructions. STEP 7: After the process and operations are done, close the files. STEP 8: Stop the program. CODING #include<stdio.h> #include<conio.h> #include<stdlib.h> struct source { char label[10]; char mnem[10]; char op[10]; }s; struct opcode { int length; char mnem[10]; char op[10]; }o; struct intermediate

{ int addr; char label[10]; char mnem[10]; char op[10]; }in; struct symbol { int addr; char label[10]; }sy; struct output { int addr; char op[10]; int oper; }out; void main() { int i,n; FILE *a,*b,*c,*d,*e; clrscr(); a=fopen("source2.txt","r"); rewind(a); b=fopen("inter2.txt","r"); rewind(b); c=fopen("symbol2.txt","r"); rewind(c); d=fopen("opcode2.txt","r"); rewind(d); e=fopen("output2.txt","w"); rewind(e); fscanf(b,"\n%d\t%s\t%s\t%s\n",&in.addr,&in.label,&in.mnem,&in.op); fscanf(d,"\n%d\t%s\t%s\n",&o.length,&o.mnem,&o.op); fscanf(c,"\n%d\t%s\n",&sy.addr,&sy.label); fscanf(a,"\n%s\t%s\t%s\n",&s.label,&s.mnem,&s.op); printf("\nEnter the value of n"); scanf("%d",&n); fseek(d,01,0); fseek(c,01,0); printf("\naddr\topcode\toperator\n"); for(i=1;i<n;i++) { fscanf(b,"\n%d\t%s\t%s\t%s\n",&in.addr,&in.label,&in.mnem,&in.op); fscanf(d,"\n%d\t%s\t%s\n",&o.length,&o.mnem,&o.op); fscanf(c,"\n%d\t%s\n",&sy.addr,&sy.label); fscanf(a,"\n%s\t%s\t%s\n",&s.label,&s.mnem,&s.op);

if(strcmp(in.mnem,o.mnem)==0) { out.addr=in.addr; strcpy(out.op,o.op); if(strcmp(s.op,sy.label)==0) out.oper=sy.addr; } if(strcmp(in.mnem,o.mnem)==0) { fprintf(e,"\n%d\t%s\t%d\n",&out.addr,&out.op,&out.oper); printf("\n%d\t%s\t%d\n",out.addr,out.op,out.oper); } } rewind(b); rewind(d); rewind(c); fcloseall(); getch(); } INPUT FILES Source2.txt 0 START 500 FIRST ADD A A BYTE 1 B BYTE 1 NULL END FIRST Inter2.txt 0 CLOOP START 500 500 FIRST ADD A 502 F1 INC B 504 A BYTE 1 505 B BYTE 1 506 NULL END CLOOP Symbol2.txt 504 A 505 B 0 FIRST

Opcode2.txt 2 ADD 3E 2 INC 4F 1 END 04 OUTPUT Enter the value of n 6 addr opcode operator 500 3E 504 502 4F 504 506 04 0 OUTPUT FILES Output2.txt addr 500 502 506 opcode 3E 4F 04 operator 504 504 0

RESULT Thus the program of pass2 of two pass assembler has been implemented and executed successfully.

SINGLE PASS ASSEMBLER AIM To create a single pass assembler of the program in C. ALGORITHM STEP1: Start the program STEP2: The instruction present in the source, symbol and intermediate are read. STEP3: The values are read and loop is introduced upto level n. STEP4: Compare s.op and in.label is equal to NULL. STEP5: Then the symbol, label and inner label is compared. STEP 6: Read the symbol, address equal to inner address. STEP 7: Then copying the values from sy.label to s.opcode can be done. STEP 8: Stop the program. CODING #include<stdio.h> #include<conio.h> #include<stdlib.h> struct source { char label[10]; char mnem[10]; char op[10]; }s; struct inter { int addr; char label[10]; char mnem[10]; char op[10]; }in; struct symbol { int addr;

char label[10]; }sy; void main() { FILE *a,*b,*c; int i,n; clrscr(); a=fopen("source3.txt","r"); rewind(a); b=fopen("inter3.txt","r"); rewind(b); c=fopen("symbol3.txt","w"); rewind(c); fscanf(b,"\n%d\t%s\t%s\t%s\n",&in.addr,in.label,in.mnem,in.op); fscanf(a,"\n%s\t%s\t%s\n",s.label,s.mnem,s.op); fseek(a,01,0); printf("\nEnter the number of instructions"); scanf("%d",&n); for(i=0;i<n;i++) { fscanf(b,"\n%d\t%s\t%s\t%s\n",&in.addr,in.label,in.mnem,in.op); fscanf(a,"\n%s\t%s\t%s\n",s.label,s.mnem,s.op); if(strcmp(in.label,s.op)==0) { strcpy(sy.label,in.label); sy.addr=in.addr; printf("\n%d\t%s\t%n",sy.addr,sy.label); fprintf(c,"\n%d\t%s\t\n",sy.addr,sy.label); } else { printf("*\t"); fprintf(c,"*\t"); strcpy(sy.label,s.op); printf("%s\t\n",sy.label); fprintf(c,"%s\t\n",sy.label); } } rewind(a); rewind(b); fcloseall(); getch(); }

INPUT FILES Source3.txt FIRST START CLOOP F1 ADD A A INC B Inter3.txt 0 FIRST START CLOOP 500 F1 ADD A 502 A INC B OUTPUT Enter the no. of instructions 3 * CLOOP 502 A * B OUTPUT FILES Symbol3.txt * CLOOP 502 A * B

RESULT Thus the program of single pass assembler has been implemented and executed successfully.

MACRO PROCESSOR AIM To perform macro processor program in C program. ALGORITHM STEP1: Start the program STEP2: Create a file name macr.txt. STEP3: Get the macro name. STEP4: Get the macro instructions. STEP5: Solve the macro and its definitions in its file. STEP 6: Close file. STEP 7: Enter the instructions of the macro. STEP 8: The macro will automatically substitute the arguments in the macr.txt file. STEP 9: Close the file and stop the program. CODING #include<stdio.h> #include<conio.h> #include<string.h> struct macro { char str[50]; }mm[10]; void main() { FILE *fp; char ch,mname[50],arg1[50],temp[50],arg2[50]; int l,i,flag=0,j,k,pos,z=0,cnt=0,y; clrscr(); fp=fopen("macr.txt","w"); printf("\nEnter the macro definition\n"); do { printf("\nEnter the instruction\n"); gets(mm[z].str);

fwrite(&mm[z],sizeof(mm[z]),1,fp); fflush(stdin); printf("\nContinue....[y/n]"); ch=getch(); if(ch=='y'||ch=='Y') z++; }while(ch=='y'||ch=='Y'); fclose(fp); fflush(stdin); z=0; printf("\nEnter the macro name\n"); gets(mname); fflush(stdin); fp=fopen("macr.txt","r+"); fread(&mm[z],sizeof(mm[z]),1,fp); fflush(stdin); if(strcmp(mm[z].str,"MACRO")==0) { while(!feof(fp)) { z++; if(flag==0) { fread(&mm[z],sizeof(mm[z]),1,fp); l=strlen(mm[z].str); printf("\nString1=\n"); puts(mm[z].str); for(i=0;i<l;i++) { if(mm[z].str[i]!=' ') temp[i]=mm[z].str[i]; else break; } temp[i]='\0'; if(strcmp(temp,mname)==0) { printf("\nEnter the two actual arguments"); gets(arg1); gets(arg2); strcat(temp," "); strcat(temp,arg1); strcat(temp,","); strcat(temp,arg2); strcpy(mm[z].str,temp); pos=ftell(fp); fseek(fp,pos-sizeof(mm[z]),SEEK_SET);

fwrite(&mm[z],sizeof(mm[z]),1,fp); fseek(fp,pos,SEEK_SET); flag++; } else printf("\nMacro not found"); } else { fread(&mm[z],sizeof(mm[z]),1,fp); printf("\nString2=\n"); puts(mm[z].str); if(strcmp(mm[z].str,"MEND")!=0) { l=strlen(mm[z].str); for(i=0;i<l;i++) { if(mm[z].str[i]==' ') temp[i]=mm[z].str[i]; else break; } temp[i]='\0'; strcpy(mm[z].str,temp); if(cnt%2==0) strcat(mm[z].str,arg1); else strcat(mm[z].str,arg2); cnt++; pos=ftell(fp); printf("pos\t%d",pos); fseek(fp,pos-sizeof(mm[z]),SEEK_SET); fwrite(&mm[z],sizeof(mm[z]),1,fp); puts(mm[z].str); fseek(fp,pos,SEEK_SET); } else break; }} fclose(fp); } getch(); }

INPUT Enter the macro definition Enter the instruction MACRO Continue..[y/n] Y Enter the instruction ADD A,B Continue..[y/n] Y Enter the instruction LDA A Continue..[y/n] Y Enter the instruction ADD B Continue.[y/n] Y Enter the instruction MEND Continue[y/n] N Enter the macro name ADD String1= ADD A,B Enter the two actual arguments 25 45 String2= LDA A Pos 15025 String2= ADD B Pos 20045 String2= MEND OUTPUT FILES macr.txt MACRO SUM 25,45 A 25 B 45 MEND

RESULT Thus the required macro processor program has been performed and executed successfully.

ABSOLUTE LOADER AIM To write a program in C language to implement absolute loader. ALGORITHM STEP1: Start the program and declare all variables. STEP2: Open the intermediate, opcode and symbol file in read mode and memory in write mode. STEP3: Get values of n and enter all the necessary information in input files. STEP4: Using strcmp function, compare intermediate and opcode mnem, if both are same, then using strcpy function copy intermediate address to memory address. STEP5: Also copy the opcode to memory file object code. STEP 6: Next compare intermediate operand and symbol table label. If same, copy symbol table address to object code of memory. STEP 7: Else copy the symbol table address to object code of memory. STEP 8: Stop the program. CODING #include<stdio.h> #include<conio.h> #include<stdlib.h> struct optab { int length; char mnem[10]; int opcode; }o; struct symtab { char label[10]; int addr; }sy;

struct inter { int addr; char label[10]; char mnem[10]; char op[10]; }in; struct memory { int loc; int opcode; int addr; }m; void main() { int i; FILE *o1,*sy1,*in1,*m1; clrscr(); o1=fopen("opcode4.txt","r"); rewind(o1); sy1=fopen("symbol4.txt","r"); rewind(sy1); in1=fopen("inter4.txt","r"); rewind(in1); m1=fopen("memory4.txt","w"); rewind(m1); for(i=0;i<3;i++) { fscanf(in1,"%d%s%s%s",&in.addr,in.label,in.mnem,in.op); fscanf(sy1,"%s%d",sy.label,&sy.addr); fscanf(o1,"\n%d%s%d",&o.length,o.mnem,&o.opcode); if(strcmp(in.mnem,"END")!=0) { if(strcmp(in.mnem,o.mnem)==0) m.loc=in.addr; fprintf(m1,"%d\t",m.loc); m.opcode=o.opcode; fprintf(m1,"%d",m.opcode); printf("%d",m.opcode); } if(strcmp(in.op,sy.label)==0) { m.addr=sy.addr; fprintf(m1,"%d\t",m.addr); printf("%d\t",m.addr); printf("\n"); fprintf(m1,"\n");

} } rewind(in1); rewind(o1); rewind(sy1); getch(); } INPUT FILES Inter4.txt 0000 COPY START C 1001 F1 ADD A 1045 A INC B 1200 NULL END F1 opcode4.txt 0 START 56 2 ADD 14 4 INC 45 symbol4.txt C 3000 A 1044 B 1200 OUTPUT Enter the value of n 3 0 563000 1001 141044 1045 451200 OUTPUT FILES Memory4.txt 0 563000 1001 141044 1045 451200 RESULT Thus the C program for absolute loader has been implemented and executed successfully.

RELOCATING LOADER AIM To implement the relocation using a C program. ALGORITHM STEP1: Start the program. STEP2: Open the intermediate in read mode and source file in write mode. STEP3: Get the starting address of the program to be relocated. STEP4: Read the input of intermediate. STEP5: Check if intermediate bitmask is equal to 1. STEP 6: If equal to one then add the intermediate address with starting address and store it. STEP 7: Then copy intermediate label, mnemonic. STEP 8: Stop the program. CODING #include<stdio.h> #include<conio.h> #include<string.h> struct inter { int addr; char label[10]; char mnem[10]; char op[10]; int bit; }in; struct reloc { int addr; char label[10]; char mnem[10]; char op[10]; int bit; }re; void main() {

int i,n; FILE *a,*b; clrscr(); a=fopen("inter5.txt","r"); rewind(a); b=fopen("reloc5.txt","w"); rewind(b); printf("\nEnter the starting address"); scanf("%d",&n); for(i=0;i<4;i++) { fscanf(a, "\n%d\t%s\t%s\t%s\t%d\n", &in.addr, in.label, in.mnem, in.op, &in.bit); if(in.bit==1) { in.addr=in.addr+n; fprintf(b, "\n%d\t%s\t%s\t%s\t%d\n", in.addr, in.label, in.mnem, in.op, &in.bit); printf("\n%d\t%s\t%s\t%s\t%d\n", in.addr, in.label, in.mnem, in.op, in.bit); } } rewind(a); rewind(b); fcloseall(); getch(); }

INPUT FILES Inter5.txt 0 CLOOP START 500 0 500 F1 ADD A 1 502 F2 INC B 1 504 NULL END CLOOP 1 OUTPUT Enter the starting address 3000 3500 F1 ADD A 1 3502 F2 INC B 1 3504 NULL END CLOOP 1 OUTPUT FILES reloc5.txt 3500 F1 ADD A 1 3502 F2 INC B 1 3504 NULL END CLOOP 1

RESULT Thus the C program for relocating loader has been implemented and executed successfully.

PROGRAM LINKING AIM To create a C program for program linking. ALGORITHM STEP1: Start the program. STEP2: Compare a mnemonic is not equal to end. STEP3: Address is assigned to address with sum to get. STEP4: Then copy s.mnem to a.mnemonic and copy s.label to a.label. STEP5: Copy also s.op and a.oper with return statement. STEP 6: If string compare mnem is not equal to end ad same as above operation is done. STEP 7: Stop the program. CODING #include<stdio.h> #include<conio.h> #include<string.h> struct proga { int addr; char label[10]; char mnem[10]; char oper[10]; }a; struct progb { int addr; char label[10]; char mnem[10]; char oper[10]; }b; struct link { int addr; char label[10]; char mnem[10]; char oper[10];

}s; struct estab { char cs[10]; char label[10]; int addr; int len; }e; void main() { FILE *p,*q,*r,*t; int i,j,sa; p=fopen("proga.txt","r"); rewind(p); q=fopen("progb.txt","r"); rewind(q); r=fopen("link.txt","w"); rewind(r); t=fopen("estab.txt","w"); rewind(t); printf("Enter the starting address"); scanf("%d",&sa); fscanf(p,"\n%d\t%s\t%s\t%s\n",&a.addr,a.label,a.mnem,a.oper); rewind(p); for(i=0;i<2;i++) { fscanf(p,"\n%d\t%s\t%s\t%s\n",&a.addr,a.label,a.mnem,a.oper); if(strcmp(a.mnem,"END")!=0) { j=a.addr; a.addr=a.addr+sa; s.addr=a.addr; strcpy(s.label,a.label); strcpy(s.mnem,a.mnem); strcpy(s.oper,a.oper); fprintf(r,"\n%d\t%s\t%s\t%s\n",s.addr,s.label,s.mnem,s.oper); } } rewind(p); for(i=0;i<2;i++) { fscanf(q,"\n%d\t%s\t%s\t%s\n",&b.addr,b.label,b.mnem,b.oper); if(strcmp(b.mnem,"END")!=0) { s.addr=b.addr+a.addr+03; strcpy(s.label,b.label); strcpy(s.mnem,b.mnem);

strcpy(s.oper,b.oper); fprintf(r,"\n%d\t%s\t%s\t%s\n",s.addr,s.label,s.mnem,s.oper); } } rewind(q); rewind(r); fclose(r); r=fopen("link1.txt","r"); for(i=0;i<4;i++) { fscanf(r,"\n%d\t%s\t%s\t%s\n",&s.addr,s.label,s.mnem,s.oper); if(strcmp(s.mnem,"START")==0) { strcpy(e.cs,s.label); e.addr=s.addr; fprintf(t,"\n%s\t%d\n",e.cs,e.addr); } if(strcmp(s.mnem,"START")!=0) { strcpy(e.label,s.oper); e.addr=s.addr; if(strcmp(s.oper,"LISTA")==0) { e.len=j+03; } else { e.len=b.addr+03; } fprintf(t,"\n%s\t%d\t%d\n",e.label,e.addr,e.len); } } rewind(r); fcloseall(); getch(); } INPUT FILES proga.txt 0000 PROGA START LISTA 0020 LISTA LDA LISTB 0060 NULL END LISTA progb.txt 0000 PROGB START LISTB

0033 LISTB LDA LISTA 0045 NULL END LISTB OUTPUT Enter the starting address 3000 3000 PROGA START LISTA 3020 LISTA LDA LISTB 3023 PROGB START LISTB 3056 LISTB LDA LISTA OUTPUT FILES Link.txt 3000 PROGA START LISTA 3020 LISTA LDA LISTB 3023 PROGB START LISTB 3056 LISTB LDA LISTA estab.txt PROGA 3000 LISTB 3020 36 PROGB 3023 LISTA 3056 23

RESULT Thus the C program for program linking has been implemented and executed successfully.

TEXT EDITOR AIM To implement a single text editor with features like insertion, deletion of a character, word, sentences. ALGORITHM STEP1: Start the program. STEP2: The header file is included. STEP3: In the main function a file pointer is created. STEP4: Then a txt file is opened using fopen. STEP5: The text file is printed in the output screen. STEP 6: Then using printf statement the line to be changed is printed. STEP 7: Then the line is printed in the screen. STEP 8: The line is changed using keyword. STEP 9: Then the file is saved and compiled. STEP 10: The editor file is printed on the screen. STEP 11: Stop the program. CODING #include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> #include<string.h> void main() { FILE *fp; int i=0,n=0,line=0,x=1,y=1,len=0,j=0; char s[100][100],temp[100],ch,choice='y'; clrscr(); if((fp=fopen("in1.txt","r+"))==NULL) { printf("\nFile cannot be opened"); exit(1);

} while(!feof(fp)) { fgets(s[i],79,fp); n=++i; } clrscr(); flushall(); printf("in1.txt"); for(i=0;i<n;i++) puts(s[i]); while(choice=='y') { printf("\nEnter the number to be changed:"); scanf("%d",&line); strcpy(temp,s[line-1]); clrscr(); puts(temp); len=strlen(temp); gotoxy(x,y); ch=getch(); while(ch!=13) { switch(ch) { case 77: x++; gotoxy(x,y); break; case 75: x--; gotoxy(x,y); break; case 83: for(i=x-1;j<len;i++,j++) temp[i]=temp[j]; clrscr(); puts(temp); gotoxy(x,y); break; default: if(isalnum(ch)) { for(i=len+1,j=len;j>x;i--,j--) temp[i]=temp[j]; clrscr(); len++;

temp[x]=ch; puts(temp); x++; gotoxy(x,y); } } ch=getch(); } clrscr(); printf("\nDo u want to save change (y/n):"); ch=getch(); if(ch=='Y'||ch=='y') strcpy(s[line-1],temp); for(i=0;i<n;i++) puts(s[i]); fclose(fp); fp=fopen("in1.txt","w"); for(i=0;i<n;i++) fputs(s[i],fp); getch(); printf("\nDo u want to continue the openpress (y/n):"); scanf("%s",&choice); } getch(); } INPUT FILES In1.txt Hai Hello This is system software lab OUTPUT Screen Output Hai Hello This is system software lab Enter the number to be changed: 2 Welcome hello Do you want to save change(y/n): y Hai Welcome hello This is system software lab Do you want to continue the openpress(y/n): n OUTPUT FILE

In1.txt Hai Welcome hello This is system software lab

RESULT Thus the text editor has been implemented and executed successfully.

ARITHMETIC OPERATIONS IN MACRO PROCESSOR AIM To execute all arithmetic operations using C pre processor and get the results ALGORITHM STEP1: Start the program. STEP2: The header file is included. STEP3: All the arithmetic operations are defined STEP4: Each defined operations are called and executed STEP 5: Then using printf statement the result is printed. STEP 7: Then the output is displayed in the screen. STEP 8: Then the file is saved and compiled. STEP 9: Stop the program. CODING: #include<stdio.h> #include<conio.h> #include<string.h> struct macro { char str[50]; }mm[10]; void main() { FILE *fp; char ch,mname[50],arg1[50],temp[50],arg2[50]; int l,i,flag=0,j,k,pos,z=0,cnt=0,y; clrscr(); fp=fopen("macr.txt","w"); printf("\nEnter the macro definition\n"); do { printf("\nEnter the instruction\n"); gets(mm[z].str); fwrite(&mm[z],sizeof(mm[z]),1,fp);

fflush(stdin); printf("\nContinue....[y/n]"); ch=getch(); if(ch=='y'||ch=='Y') z++; }while(ch=='y'||ch=='Y'); fclose(fp); fflush(stdin); z=0; printf("\nEnter the macro name\n"); gets(mname); fflush(stdin); fp=fopen("macr.txt","r+"); fread(&mm[z],sizeof(mm[z]),1,fp); fflush(stdin); if(strcmp(mm[z].str,"MACRO")==0) { while(!feof(fp)) { z++; if(flag==0) { fread(&mm[z],sizeof(mm[z]),1,fp); l=strlen(mm[z].str); printf("\nString1=\n"); puts(mm[z].str); for(i=0;i<l;i++) { if(mm[z].str[i]!=' ') temp[i]=mm[z].str[i]; else break; }}

INPUT FILE: Enter the value to be added 4 5 OUTPUT FILE: The resultant value is 9 RESULT Thus the required macro processor program has been performed and executed successfully.

Вам также может понравиться