LIST OF EXPERIMENTS
(Using C)
1. Implement a symbol table with functions to create, insert, modify, search, and display.
2. Implement pass one of a two pass assembler.
3. Implement pass two of a two pass assembler.
4. Implement a single pass assembler.
5. Implement a two pass macro processor
6. Implement a single pass macro processor.
7. Implement an absolute loader.
8. Implement a relocating loader.
9. Implement pass one of a direct-linking loader.
10. Implement pass two of a direct-linking loader.
11. Implement a simple text editor with features like insertion / deletion of a character,
word, and sentence.
12. Implement a symbol table with suitable hashing
Ex:No:1 IMPLEMENT A SYMBOL TABLE WITH FUNCTIONS TO CREATE,INSERT,SEARCH AND DISPLAY
AIM:
To implement a Symbol table with functions to create, insert, modify, search and display in C language.
ALGORITHM:
1. Start the program
2 . Define the structure of the symbol table
3 . Enter the choice for performing the operations in the symbol table
4 .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
5. If choice is 2, symbols present in the symbols tables are displayed.
6.If choice is 3, symbol to be deleted is searched in the symbol table, if found deletes else displays “Not Found”.
7 .If choice is 4, the symbol to be searched in the Symbol table. If found display the label, else displays “Label not found...”.
8.If choice is 5, this will be exiting...
9. Stop the program.
PROGRAM:
#include
#include
#include
#include
#define NULL 0
int size=0;
void insert();
void del();
int search(char tab[]);
void display();
struct symtab
{
char label[13];
int addr;
struct symtab*next;
};
struct symtab*first,*last;
void main()
{
int op,y,i;
char la[10];
closer();
do
{
loop:
print("\n \t===============================");
print("\n \t||SYMBOL TABLE IMPLEMENTATION|| ");
print("\n \t===============================\n");
printf("\n\t\t1.INSERT\n\t\t2.DISPLAY\n\t\t3.DELETE\n\t\t4.SEARCH\n\t\t5.EXIT\n");
print("\n Enter urn option: ");
scanf("%d",&op);
switch(op)
{
case 1:
insert();
display();
break;
case 2:
display();
break;
case 3:
del();
display();
break;
case 4:
printf("\n Enter the label to be search :");
scanf("%s",&la);
y=search(la);
if(y==1)
{
struct symtab *p;
printf("\n =*=*=*=*=");
printf("\n||LABEL ||");
printf("\n =*=*=*=*=\n");
printf("|| %s ||\n",la);
printf("----------\n");
}
else
{
printf("\n LABEL NOT FOUND...\n");
}
break;
case 5:
exit(1);
default:
printf("\n INVALID OPTION...\n");
goto loop;
}
}while(op<5);
getch();
}
void insert()
{
char n[10],l[10];
int i;
printf("\n Enter the label:");
scanf("%s",&l);
n[i]=search(l);
if(n[i]==1)
{
printf("\n\t\tALREADY EXIST...\n");
}
else
{
struct symtab *p;
p=malloc(sizeof(struct symtab));
strcpy(p->label,l);
printf("\n Enter the address:");
scanf("%x",&p->addr);
p->next=NULL;
if(size==0)
{
first=p;
last=p;
}
else
{
last->next=p;
last=p;
}
size++;
}
}
void display()
{
int i;
struct symtab *p;
p=first;
printf("\n\t\t===========================\n");
printf("\t\t|| LABEL || ADDRESS ||\n");
printf("\t\t===========================\n");
for(i=0;i
{
printf("\t\t|| %s || %x || \n",p->label,p->addr);
p=p->next;
printf("\t\t---------------------------\n");
}
}
int search(char lab[])
{
int i,flag=0;
struct symtab *p;
p=first;
for(i=0;i
{
if(strcmp(p->label,lab)==0)
{
flag=1;
}
p=p->next;
}
return flag;
}
void del()
{
int a;
char l[10];
struct symtab *p,*q;
p=first;
printf("\n Enter the lable to be deleted:");
scanf("%s",&l);
a=search(l);
if(a==0)
{
printf("\n\t\t LABEL NOT FOUND...\n");
}
else
{
if(strcmp(first->label,l)==0)
{
first=first->next;
}
else if(strcmp(last->label,l)==0)
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=NULL;
last=p;
}
else
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=q->next;
}
size--;
}
}
OUTPUT:
================================
||SYMBOL TABLE IMPLEMENTATION||
================================
1. INSERT
2. DISPLAY
3. DELETE
4. SEARCH
5. EXIT
Enter your option: 1
Enter the label: RLOOP
Enter the address: 2010
==========================
|| LABEL || ADDRESS ||
==========================
|| RLOOP || 2010 ||
--------------------------------------------
================================
||SYMBOL TABLE IMPLEMENTATION||
================================
1. INSERT
2. DISPLAY
3. DELETE
4. SEARCH
5. EXIT
Enter your option: 1
Enter the label: FOUR
Enter the address: 201c
.
.
==========================
|| LABEL || ADDRESS ||
==========================
|| RLOOP || 2010 ||
--------------------------------------------
|| FOUR || 201C ||
--------------------------------------------
|| SEVEN || 2021 ||
--------------------------------------------
|| LOOP || 2029 ||
--------------------------------------------
================================
||SYMBOL TABLE IMPLEMENTATION||
================================
1. INSERT
2. DISPLAY
3. DELETE
4. SEARCH
5. EXIT
Enter your option: 2
==========================
|| LABEL || ADDRESS ||
==========================
|| RLOOP || 2010 ||
--------------------------------------------
|| FOUR || 201C ||
-------------------------------------------
|| SEVEN || 2021 ||
--------------------------------------------
|| LOOP || 2029 ||
--------------------------------------------
================================
||SYMBOL TABLE IMPLEMENTATION||
================================
1. INSERT
2. DISPLAY
3. DELETE
4. SEARCH
5. EXIT
Enter your option: 3
Enter the label to be deleted: RLOOP
==========================
|| LABEL || ADDRESS ||
==========================
|| FOUR || 201C ||
-------------------------------------------
|| SEVEN || 2021 ||
--------------------------------------------
|| LOOP || 2029 ||
--------------------------------------------
================================
||SYMBOL TABLE IMPLEMENTATION||
================================
1. INSERT
2. DISPLAY
3. DELETE
4. SEARCH
5. EXIT
Enter your option: 4
Enter the label to be search: SEVEN
=*=*=*=*=*=
|| LABEL ||
=*=*=*=*=*=
|| SEVEN ||
------------------
================================
||SYMBOL TABLE IMPLEMENTATION||
================================
1. INSERT
2. DISPLAY
3. DELETE
4. SEARCH
5. EXIT
Enter your option: 5
Exiting…….
RESULT:
Thus, a Symbol table with functions to create, insert, search and display is implemented successfully using in C language.
Ex.No:2
IMPLEMENTATION OF PASS ONE OF TWO PASS ASSEMBLER
AIM:
To write a C program to implement pass 1 of two pass assembler.
ALGORITHM:
1. Read the input line.
2. Check to see if the opcode in the input line is “START”
3. If there is any operand field after START, initialize the LOCCTR to the operand value.
4. If there is no value in the operand field, the LOCCTR is set to zero and the program starts loading from the starting address zero.
5. Write the line to the intermediate file.
6. If there is a symbol I the label field,
· Check the symbol table to see if it has already been stored .
· If so then it is a duplicate symbol, the error message should be displayed.
· Otherwise the symbol is entered into the SYMTAB, along with the memory address in which it is stored.
7. If there is an opcode in the line
· Search the OPTAB to see if the opcode is present, if so increment the location counter by three.
· If the opcode is WORD, increment the location counter by three.
· If the opcode is BYTE , increment the location counter by one.
· If the opcode is RESW, increment the location counter by the integer equivalent of the operand value *3
· If the opcode is RESB, increment the location counter by the integer equivalent of the operand value.
8. Write each and every line processed to the intermediate file along with their location counters.
9. Subtract the starting address of the program from the final value of the LOCCTR, to calculate the length of the program.
10. Close all the operand files and exit.
PROGRAM:
#include
#include
#include
FILE *fp_src,*fp_symtab;
char srcfile[25],label[10],opcode[10],operand[10];
int hexaddr=0X1000,startaddr=0,length,i;
void main()
{
startaddr=hexaddr;
clrscr();
printf("\n\t\t Pass-1 of 2-pass assembler \n");
printf("\n\t\t=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");
printf("Enter filename that contain the source assembly program: ");
scanf("%s",&srcfile[i]);
printf("\nSource Program filename is : %s",srcfile);
fp_src=fopen(srcfile,"r");
while(fscanf(fp_src,"%s%s%s",label,opcode,operand)!=EOF)
{
fclose(fp_src);
printf("\n\t\t======================");
printf("\n\t\t|| Intermediate file|| ");
printf("\n\t\t======================\n\n");
fp_src=fopen(srcfile,"r");
fp_symtab=fopen("symtab.txt","w");
fscanf(fp_src,"%s%s%s",label,opcode,operand);
if((strcmp(opcode,"START"))==0)
printf("%x\t%s\t%s\t%s\n",hexaddr,label,opcode,operand);
while(fscanf(fp_src,"%s%s%s",label,opcode,operand)!=EOF)
{
if(strcmp(opcode,"END")==0)
{
hexaddr=hexaddr;
printf("\t%s\t%s\t%s\n",label,opcode,operand);
}
else
printf("%x\t%s\t%s\t%s\n",hexaddr,label,opcode,operand);
if(strcmp(label,"0")!=0)
{
fprintf(fp_symtab,"%s\t%x\n",label,hexaddr);
}
if(strcmp(opcode,"WORD")==0)
hexaddr=hexaddr+3;
else if(strcmp(opcode,"BYTE")==0)
hexaddr=hexaddr+1;
else if(strcmp(opcode,"END")!=0)
hexaddr=hexaddr+3;
}
fclose(fp_src);
fclose(fp_symtab);
printf("\n\t\t\t================");
printf("\n\t\t\t||SYMBOL TABLE||");
printf("\n\t\t\t================\n\n");
fp_symtab=fopen("symtab.txt","r");
length=hexaddr-startaddr;
while(fscanf(fp_symtab,"%s%x",label,&hexaddr)!=EOF)
{
printf("%s\t%x\n",label,hexaddr);
}
fclose(fp_symtab);
printf("\nThe length of the program is %d...\n",length);
getch();
}
}
INPUT:
input.dat:
** START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C’Z’
C1 RESW 1
** END **
Symtab.txt:
** 1000
** 1003
** 1006
** 1009
ALPHA 100c
FIVE 100f
CHARZ 1012
C1 1013
** 1016
OUTPUT:
PASS-1 OF 2-PASS ASSEMBLER
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
Enter the filename that contain source assembly program: input.dat
Source Program filename is :input.dat
INTERMEDIATE FILE
=*=*=*=*=*=*=*=*=*=
1000 ** START 2000
1000 ** LDA FIVE
1003 ** STA ALPHA
1006 ** LDCH CHARZ
1009 ** STCH C1
100C ALPHA RESW 1
100F FIVE WORD 5
1012 CHARZ BYTE C’Z’
1013 C1 RESW 1
** END **
SYMBOL TABEL
=*=*=*=*=*=*=*=
** 1000
** 1003
** 1006
** 1009
ALPHA 100c
FIVE 100f
CHARZ 1012
C1 1013
** 1016
The length of the program is 22...
RESULT:
Thus, pass one of a two pass assembler is implemented successfully using in C language.
Ex.No:3 IMPLEMENTATION OF PASS TWO OF A TWO PASS ASSEMBLER
AIM:
To write a C program to implement pass 2 of two pass assembler.
ALGORITHM:
1. Read the first line from the intermediate file.
2. Check to see if the opcode from the first line read is “START”
3. If so then write the label, opcode and operand field values of the corresponding statement directly to the final output file.
4. Start the following processing, for the other lines in the intermediate file if it is not a comment line until an “END” statement is reached.
· Start writing the location counter, opcode and operand fields of the corresponding statement to the output file, along with the object code. The object code is found by assembling each statement opcode’s machine equivalent with the label address.
· If there is no symbol/label in the operand field, then the operand address is assigned as zero, and it is assembled with the object code of the instruction.
· If the opcode is BYTE, WORD, RESB etc convert the constants to the object code.
5. Close the operand fields and exit.
PROGRAM:
#include
#include
#include
void main()
{
FILE *opsht_fp,*inter_fp,*symtab_fp;
char opsheet[15],label[12],opcode[12],operand[12],sourcefile1[16];
char mnemonic[10],objcode[10],symbol[10];
int locctr,startaddr,saddr,length;
clrscr();
printf("\n\t\t Pass-2 of 2-Pass assembler \n");
printf("\n\t\t=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");
printf("Enter the Opcode sheet filename: ");
scanf("%s",&opsheet);
printf("\n\t\t======================================\n");
printf("\t\t||Contains in the Opcode sheet file:||\n");
printf("\t\t=======================================\n");
opsht_fp=fopen(opsheet,"r");
while(fscanf(opsht_fp,"%s\t%s\n",mnemonic,objcode)!=EOF)
fprintf(stdout,"\t%s\t%s\n",mnemonic,objcode);
fclose(opsht_fp);
printf("\t\t===========================\n");
printf("\t\t||Contents in the symtab:|| \n");
printf("\t\t===========================\n");
symtab_fp=fopen("symtab.txt","r");
while(fscanf(symtab_fp,"%s%x\n",symbol,&locctr)!=EOF)
printf("\t%s\t%x\n",symbol,locctr);
fclose(symtab_fp);
printf("\nEnter the name of the intermediate file: ");
scanf("%s",sourcefile1);
inter_fp=fopen(sourcefile1,"r");
while(fscanf(inter_fp,"%x%s%s%s",&locctr,label,opcode,operand)!=EOF)
{
if(strcmp(label,"END")==0)
printf("\t\t%s\t%s\t%s\n","0",label,opcode,operand);
else
printf("\t%x\t%s\t%s\t%s\n",locctr,label,opcode,operand);
}
fclose(inter_fp);
printf("\nEnter the program length: ");
scanf("%d",&length);
inter_fp=fopen(sourcefile1,"r");
while(fscanf(inter_fp,"%x%s%s%s",&locctr,label,opcode,operand)!=EOF)
{
if(strcmp(opcode,"START")==0)
{
startaddr=locctr;
printf(" Header Record:\n");
printf("=*=*=*=*=*=*=*=\n");
printf("H %s %x %x\n",label,locctr,length);
printf("\n Text record:\n");
printf("=*=*=*=*=*=*=\n");
printf("T %x %x ",locctr,length);
}
opsht_fp=fopen(opsheet,"r");
while(fscanf(opsht_fp,"%s%s",mnemonic,objcode)!=EOF)
{
if(strcmp(opcode,mnemonic)==0)
printf(" %s",objcode);
}
fclose(opsht_fp);
symtab_fp=fopen("symtab.txt","r");
while(fscanf(symtab_fp,"%s%x",symbol,&saddr)!=EOF)
{
if(strcmp(operand,symbol)==0)
printf("%x ",saddr);
}
fclose (symtab_fp);
}
fclose(inter_fp);
printf("\n\n");
printf("\n End Record:");
printf("\n=*=*=*=*=*=*=\n");
printf("E %x\n",startaddr);
getch();
}
INPUT:
Optab.txt:
LDA 00
LDB 68
LDX 04
LDCH 50
STA 0C
STB 78
STX 10
STCH 54
TIX 2C
J 3C
JSUB 48
RSUB 4C
JEQ 30
JLT 38
JGT 34
START *
END *
Symtab.txt
** 1000
** 1003
** 1006
** 1009
ALPHA 100c
FIVE 100f
CHARZ 1012
C1 1013
** 1016
Inter.txt
1000 ** START 2000
1000 ** LDA FIVE
1003 ** STA ALPHA
1006 ** LDCH CHARZ
1009 ** STCH C1
100C ALPHA RESW 1
100F FIVE WORD 5
1012 CHARZ BYTE C'Z'
1013 C1 RESW 1
1016 ** END *
OUTPUT:
Enter the Opcode sheet filename:optab.txt
==================
|| OPCODE SHEET ||
==================
LDA 00
LDB 68
LDX 04
LDCH 50
STA 0C
STB 78
STX 10
STCH 54
TIX 2C
J 3C
JSUB 48
RSUB 4C
JEQ 30
JLT 38
JGT 34
START *
END *
============
|| SYMTAB ||
============
** 1000
** 1003
** 1006
** 1009
ALPHA 100c
FIVE 100f
CHARZ 1012
C1 1013
** 1016
Enter the intermediate filename:inter.txt
1000 ** START 2000
1000 ** LDA FIVE
1003 ** STA ALPHA
1006 ** LDCH CHARZ
1009 ** STCH C1
100C ALPHA RESW 1
100F FIVE WORD 5
1012 CHARZ BYTE C'Z'
1013 C1 RESW 1
1016 ** END *
Enter the length : 22
Header Record:
=*=*=*=*=*=*=*=
H ** 1000 16
Text Record:
=*=*=*=*=*=*=
T 1000 16 * 00100f 0c100c 501012 541013 * 1000 1003 1006 1016
End Record:
=*=*=*=*=*=*=
E 1000
RESULT:
Thus, pass two of a two pass assembler is implemented successfully using in C language.
Ex.No:4 Implement a single pass assembler
AIM:
To implement a single pass assembler in C language.
ALGORITHM:
1. Open and Read the input file
2. If the input line has the opcode “START” do the following
2.1 Find if there is any operand field after “START”, initialize the LC to the operand value
2.2 Otherwise if there is no value in the operand field then LC is set to 0
3. Write the input line to the intermediate file
4. Do the following steps until the opcode is END
4.1 Check the Symbol table, if the symbol is not available then enter that symbol into the SYMTAB, along with the memory address in which it is stored. Otherwise, the error message should be displayed
4.2 If there is a opcode
4.2.1 If opcode is present in the OPTAB, then increment the LC by 3 and Start writing the location counter, opcode and operand fields of the corresponding statement to the output file, along with the object code.
4.2.2 If opcode is “WORD”, then increment LC by 3;
4.2.3 If opcode is “BYTE”, then increment LC by 1;
4.2.4 If opcode is “RESW” then increment LC by the integer equivalent of the operand value * 3;
4.2.5 If opcode is “RESB”, then increment LC by the integer equivalent of the operand value
4.2.6 If there is no symbol/label in the operand field, then the operand address is assigned as zero and it is assembled with the object code of the instruction
4.2.7 Write the processed lines in the intermediate file along with their location counters
5. To find the length of the program, Subtract the starting address of the program from the final value of the LC
6. Close all the files and exit
PROGRAM:
#include
#include
#include
#define q 11 //no. of mnemonics in the array A
void main()
{
int lc,ad,address,err=0;
int s,num,l,i=0,j,n=0,line=1,f=0,f1=0,t=0,ni=0,m=0,t1;
FILE *fp1,*fp2,*fp3,*fp4;
char lab[10],op[10],val[10],code[10];
char a[20][15]={"STA","STL","LDA","LDB","J","JEQ","J","SUB","COMP","STCH","ADD","SUB"};
char b[20][15]={"14","32","03","69","34","30","48","28","24","16","0C"};
char sym[15][10];
int symadd[15];
clrscr();
fp1=fopen("INPUT.DAT","r");
fp2=fopen("OBJFILE.DAT","w");
fp3=fopen("ERROR.DAT","w");
fp4=fopen("SYMTAB.DAT","w");
while((!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
t++;
m++;
if(strcmp(op,".")==0)
m=0;
else if(strcmp(op,"END")==0)
break;
}
t=t-1;
m--;
fclose(fp1);
fp1=fopen("INPUT.DAT","r");
fscanf(fp1,"%s\t%s\t%x",lab,op,&lc);
fprintf(fp3,"-------------------------------------\n");
fprintf(fp3,"|| LINE NO. \t|| ERROR FOUND ||\n");
fprintf(fp3,"-------------------------------------\n");
fprintf(fp4,"SYMBOL\tADDRESS\n");
s=lc;
fprintf(fp2,"H %s 00%x %x\n",lab,lc,t*3);
fprintf(fp2,"T 00%x ",lc);
if(m>10)
fprintf(fp2,"1E");
else
fprintf(fp2,"%x",m*3);
while((op,".")!=0&&(!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
line++;
if(strcmp(lab,"**")!=0)
{
for(i=0;i
{
if(strcmp(lab,sym[i])==0)
{
f=1;
break;
}
f=0;
}
if(f==0)
{
strcpy(sym[n],lab);
symadd[n]=lc;
fprintf(fp4,"\n%s\t%x",lab,lc);
n++;
}
if(f==1)
{
fprintf(fp3,"%d\t\t|SYMBOL ALREADY DEFINED\n",line);
err++;
}
}
num=atoi(val);
if(strcmp(op,"RESW")==0)
lc=lc+(num*3);
else if(strcmp(op,"RESB")==0)
lc=lc+num;
else if(strcmp(op,"BYTE")==0)
{
num=strlen(val)-3;
lc=lc+num;
for(i=2,j=0;i
{
code[j]=val[i];
j++;
}
code[j]='\0';
fprintf(fp2," %s",code);
ni++;
}
else
lc=lc+3;
if(strcmp(op,".")==0)
break;
}
while(strcmp(op,"END")!=0&&(!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
line++;
if(strcmp(op,"END")==0)
break;
if((strcmp(lab,"**")!=0)&&((strcmp(op,"RESW")!=0||strcmp(op,"RESB")!=0||strcmp(op,"WORD")!=0||strcmp(op,"BYTE")==0)))
{
for(i=0;i
{
if(strcmp(lab,sym[i])==0)
{
f=1;
break;
}
f=0;
}
if(f==0)
{
strcpy(sym[n],lab);
symadd[n]=lc;
fprintf(fp4,"\n%s\t%x",lab,lc);
n++;
}
else
{
fprintf(fp3,"\n%d\t\t|SYMBOL ALREADY DEFINED");
err++;
}
}
else if(strcmp(op,"RESW")==0||strcmp(op,"RESB")==0||strcmp(op,"WORD")==0||strcmp(op,"BYTE")==0)
fprintf(fp3,"\n%d\t\t|Declaration not allowed here",line);
if(strcmp(op,"RESW")!=0&&strcmp(op,"RESB")!=0&&strcmp(op,"WORD")!=0&&strcmp(op,"BYTE")!=0)
{
for(i=0;i
{
if(strcmp(op,a[i])==0)
{
strcpy(code,b[i]);
f1=0;
break;
}
f1=1;
}
if(f1==1)
{
fprintf(fp3,"\n%d\t\t|WRONG OPCODE",line);
err++;
}
for(i=0;i
{
if(strcmp(val,sym[i])==0)
{
address=symadd[i];
f=0;
break;
}
f=1;
}
if(f)
{
fprintf(fp3,"\n%d\t\t|UNDEFINED SYMBOL",line);
err++;
}
}
if(ni<10)
{
fprintf(fp2," %s%x",code,address);
ni++;
}
else
{
fprintf(fp2,"T 00%x ",lc);
if(m>10)
{
fprintf(fp2,"1E");
m=m-10;
}
else
{
fprintf(fp2,"%x",m*3);
fprintf(fp2," %s%x",code,address);
ni=0;
}
}
lc=lc+3;
}
fprintf(fp2,"\nE 00%x",s);
fprintf(fp3,"No of errors=%d\n--------------------------------------",err);
printf("Output file: OBJCODE.DAT\n\nErrors are described in ERROR.DAT\n\nSymbol table is in the file: SYMTAB.DAT");
getch();
fcloseall();
}
INPUT:
INPUT.DAT:
COPY START 1000
RETADR RESW 1
BUFFER RESB 4
EOF BYTE C`EOF`
** . **
FIRST STA RETADR
** STL BUFFER
** J FIRST
** END START
OUTPUT:
Output file: OBJCODE.DAT
Errors are described IN: ERROR.DAT
Symbol table is in the file: SYMTAB.DAT
OBJCODE.DAT:
H COPY 001000 18
T 001000 9 EOF 141000 321003 34100d
E 001000
ERROR.DAT:
----------------------------------
|| LINE NO. || ERROR FOUND ||
----------------------------------
No of errors = 0
----------------------------------
SYMTAB.DAT:
SYMBOL ADDRESS
RETADR 1000
BUFFER 1003
EOF 1007
FIRST 100d
RESULT:
Thus single pass assembler is implemented successfully in C language.
Ex.No:5 Implement a two pass macro processor
AIM:
To implement two pass macro processor using in C language.
ALGORITHM:
1. Start the program execution.
2. Macro instructions are included in a separate file.
3. The instructions with ‘macro’,’mend’,’call’ on them should not
be printed in the output.
4. Print all other instructions such as start,load,store,add,sub
Etc with their values.
5. Stop the program execution.
PROGRAM:
#include
#include
#include
#include
void main()
{
char n1,n,c1,i;
char fn[10][10],ilab[20],iopd[20],m[20][3],oper[20],opd[20];
FILE *fp1,*fp2,*p[5];
clrscr();
n=0;
fp1=fopen("macin.txt","r");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"MACRO")==0)
n++;
}
printf("No.of macros=%d\n",n);
n1=n;
printf("Enter the text filename \n");
for(i=0;i
{
scanf("%s",fn[i]);
p[i]=fopen(fn[i],"w");
}
n=0;
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"MACRO")==0)
{
strcpy(m[n],oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"MEND")!=0)
{
fprintf(p[n],"%s %s %s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fclose(p[n]);
n++;
}
}
for(i=0;i
p[i]=fopen(fn[i],"r");
fp2=fopen("outm.txt","w");
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,"CALL")==0)
{
for(i=0;i
{
if(strcmp(m[i],oper)==0)
{
rewind(p[i]);
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
while(!feof(p[i]))
{
fprintf(fp2,"%s %s %s",ilab,iopd,oper);
c1=1;
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
}
break;
}
}
}
if(c1!=1)
fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
c1=0;
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
}
Input:
macin.txt
** MACRO M1
** MOVE A,B
** MEND ----
** MACRO M2
** LDA B
** MEND ----
** START 1000
** LDA A
** CALL M1
** CALL M2
** ADD A,B
Output:
No. of Macros = 2
Enter the Text file Names : ma1.dat
ma2.dat
outm.txt
** MACRO M1
** MOVE A,B
** MEND ----
** MACRO M2
** LDA B
** MEND ----
** START 1000
** LDA A
** MOVE A,B
** LDA B
** ADD A,B
ma1.dat
** MOVE A,B
ma2.dat
** LDA B
RESULT:
Thus, a two pass macro processor is implemented successfully using in C language.
Ex.No:6 Implement a single pass macro processor
AIM:
To implement a single pass macro processor using in C language.
ALGORITHM:
Step 1: Get the statement from the input file
Step 2: If the statement has the directive “MACRO”, then the
number of macro “n” will be incremented by 1
Step 3: Repeat the steps 1 and 2 until an end of file is
encountered
Step 4: Open “n” number of macro files in write mode and rewind the
input file pointer
Step 5: If the directive is “MACRO” then, do the following
Step 5.1: Enter the macro name present in the operand field
Step 5.2: Write the line to the expanded output file
Step 5.3: Enter the lines in the body of each macro in to
the corresponding files already opened in step 4.
Step 5.4: Write the body of each macro to the expanded
output file until a “MEND” is reached
Step 6: Write the remaining lines directly to the expanded file.
PROGRAM:
#include
#include
#include
#include
void main()
{
int n,flag,i;
char ilab[20],iopd[20],oper[20],NAMTAB[20][20];
FILE *fp1,*fp2,*DEFTAB;
clrscr();
fp1=fopen("macroin.dat","r");
fp2=fopen("macroout.dat","w");
n=0;
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,"MACRO")==0)
{
strcpy(NAMTAB[n],ilab);
DEFTAB=fopen(NAMTAB[n],"w");
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"MEND")!=0)
{
fprintf(DEFTAB,"%s\t%s\t%s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fclose(DEFTAB);
n++;
}
else
{
flag=0;
for(i=0;i
{
if(strcmp(iopd,NAMTAB[i])==0)
{
flag=1;
DEFTAB=fopen(NAMTAB[i],"r");
fscanf(DEFTAB,"%s%s%s\n",ilab,iopd,oper);
while(!feof(DEFTAB))
{
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
fscanf(DEFTAB,"%s%s%s",ilab,iopd,oper);
}
break;
}
}
if(flag==0)
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
}
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
getch();
}
Input:
MACROIN.DAT;
M1 MACRO **
** LDA N1
** ADD N2
** STA N3
** MEND **
M2 MACRO **
** LDA N1
** SUB N2
** STA N4
** MEND **
M3 MACRO **
** LDA N1
** MUL N2
** STA N5
** MEND **
** START 1000
** M3 **
** M2 **
** M1 **
** END **
OUTPUT:
MACROOUT.DAT:
** START 1000
** LDA N1
** MUL N2
** STA N5
** LDA N1
** SUB N2
** STA N4
** LDA N1
** ADD N2
** STA N3
** END **
RESULT:
Thus a single pass macro processor is implemented successfully in C language.
Ex.No.7
Implement an absolute loader
AIM:
To implement an absolute loader using in C language.
ALGORITHM:
STEP 1: Read the Header record
STEP 2: Verify program name and length
STEP 3: Read first Text record from the input file
STEP 4: Process the following steps until an End record is
reached
STEP 4.1: If object code is in character form, convert
it to internal hexadecimal representation.
STEP 4.2: Move object codes to specified locations in
Memory.
STEP 4.3: Write the starting location counter value of
a block of object code and the corresponding
internal representation to the output file.
STEP 4.4: Read next Text record from the input file
STEP 5: Go to the address specified in End record.
STEP 6: Close all the files and exit.
STEP 7: Stop the program.
PROGRAM:
#include
#include
#include
void main()
{
char input[10];
int start,length,address;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("i.dat","r");
fp2=fopen("output.dat","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%d",&start);
fscanf(fp1,"%d",&length);
fscanf(fp1,"%s",input);
}
else if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",input);
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
else
{
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
printf("FINISHED...");
getch();
}
INPUT:
Input.dat
H 1000 232
T 1000 142033 483039 102036
T 2000 298300 230000 282030 302015
E 1000
OUTPUT:
Finished...
Output.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:
Thus an Absolute loader is implemented successfully in C language.
Ex.No: 8 IMPLEMENT A RELOCATION LOADER
AIM:
To write a C program to implement relocation loader.
ALGORITHM:
STEP 1: Start the program execution.
STEP 2: Enter the starting address location for relocating the
object code.
STEP 3: Transfer the input array into output array.
STEP 4: Convert the current string array into binary form, where
current string is relocating bit.
STEP 5: Relocating bit is subjected to required changes before
transferring input to output and also move object code.
STEP 6: Stop the program execution.
PROGRAM:
#include
#include
#include
#include
struct object_code
{
int locctr;
char add[10];
}obcode[300];
void main()
{
char input[100][16],output[100][16],binary[20],address[20],stloc[10];
int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("input.dat","r");
fp2=fopen("output.dat","w");
printf("Enter the location where the program has to be loaded:\n");
scanf("%s",stloc);
start=atoi(stloc);
location=start;
tloc=start;
fscanf(fp1,"%s",input[i]);
while(strcmp(input[i],"T")!=0)
{
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
itoa(start,output[2],10);
while(strcmp(input[i],"E")!=0)
{
strcpy(output[i],input[i]);
if(strcmp(input[i],"T")==0)
{
for(j=0;j<3;j++)
{
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
bitmask=atoi(output[i]);
itoa(bitmask,binary,2);
strcpy(output[i],"NULL");
textloc=atoi(output[i-2]);
textloc=textloc+start;
itoa(textloc,output[i-2],10);
for(n=0;n<(textloc-(tloc+tlen));n++)
{
strcpy(obcode[inc].add,"xx");
obcode[inc++].locctr=location++;
}
tlen=atoi(output[i-1]);
tloc=textloc;
k=0;
}
else
{
if(binary[k]=='1')
{
num=0;
len=strlen(output[i]);
strcpy(address,NULL);
for(j=2;j
{
address[num]=output[i][j];
output[i][j]='\0';
num++;
}
loc=atoi(address);
loc=loc+start;
itoa(loc,address,10);
strcat(output[i],address);
}
k++;
len=strlen(output[i]);
num=0;
for(n=0;n
{
obcode[inc].add[num++]=output[i][n];
if(num>1)
{
obcode[inc++].locctr=location++;
num=0;
}
}
}
i++;
fscanf(fp1,"%s",input[i]);
}
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
loc=atoi(input[i]);
loc=loc+start;
strcpy(output[i],itoa(loc,address,10));
count=0;
i=0;
n=0;
fprintf(fp2,"%d\t",obcode[n].locctr);
for(n=0;n
{
fprintf(fp2,"%s",obcode[n].add);
i++;
if(i>3)
{
fprintf(fp2,"\t");
i=0;
count++;
}
if(count>3)
{
fprintf(fp2,"\n%d\t",obcode[n+1].locctr);
count=0;
}
}
getch();
}
INPUT:
input.dat
H COPY 000000 001073
T 000000 10 015 140035 431039 100036 280030 300065 481061 311003 200030 211033 200033
T 000011 19 045 140036 481060 380033 412000 454196 100005 20000
T 000031 15 135 140030 430030 141013 301044 241064 210030 301057 546275 212064 381045
T 000058 05 056 100036 520000 151000 301000
T 000065 19 080 340030 141079 301064 503039 152079 220030 3810064 432000 25
E 000000
Output:
Enter the location where the program has to be loaded: 7000
output.dat
7000 14703543 80391070 36287030 30006548
7016 10613110 03200030 21103320 0033xx14
7032 70364810 60387033 41900045 41961070
7048 052000xx 14703043 00301410 13301044
7064 24106421 70303080 57541327 21206438
7080 1045xxxx xxxxxxxx xxxxxxxx xxxx1073
7096 65527000 15800030 1000xxxx 34703014
7112 10793080 64503039 15207922 00303810
7128 06432000 25
RESULT:
Thus, a Relocating loader is implemented successfully in C language.
Ex.No: 9 IMPLEMENT PASS ONE OF A DIRECT LINKING LOADER
AIM:
To write a C program to implement pass one of direct linking loader.
ALGORITHM:
STEP 1: Start the program execution.
STEP 2: Enter the location when the program has to be loaded.
STEP 3: Assign the address got from the user as the first
control section address.
STEP 4: Read the loader record of control section.
STEP 5: Enter the symbol into the symbol table with address and
also with control section address
STEP 6: Address of the control section and control section
length together is the starting address of next control
section.
STEP 7: Stop the program execution
PROGRAM:
#include
#include
#include
#define MAX 10
struct estab
{
char csect[10];
char sym_name[10];
long int add;
int length;
}
table[MAX];
void main()
{
FILE *fp1,*fp2;
char input[10];
long int i,count=0,start,length,loc;
clrscr();
fp1=fopen("infile.dat","r");
fp2=fopen("outfile.dat","w");
printf("Enter the location where the program has to be loaded: ");
scanf("%lx",&start);
fprintf(fp2,"\n=================================================");
fprintf(fp2,"\n|| CSECT || SYMNAME || ADDRESS || LENGTH ||\n");
fprintf(fp2,"===================================================\n");
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s",input);
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",input);
strcpy(table[count].csect,input);
strcpy(table[count].sym_name,"\0");
fscanf(fp1,"%s",input);
table[count].add=atoi(input)+start;
fscanf(fp1,"%s",input);
length=atoi(input);
table[count++].length=atoi(input);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"D")==0)
{
fscanf(fp1,"%s%lx",input,&loc);
while((strcmp(input,"R")!=0))
{
strcpy(table[count].csect,"\0");
strcpy(table[count].sym_name,input);
table[count].add=loc+start;
table[count++].length=0;
fscanf(fp1,"%s%lx",input,&loc);
}
while(strcmp(input,"T")!=0)
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
while(strcmp(input,"E")!=0)
fscanf(fp1,"%s",input);
fscanf(fp1,"%s",input);
start=start+length;
}
for(i=0;i
fprintf(fp2,"%s\t\t%s\t\t%lx\t\t%d\n",table[i].csect,
table[i].sym_name,table[i].add,table[i].length);
getch();
}
Input:
Input.dat:
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 150014
T 000054 16 100014 15100006 00002F 100014 FFFFC0
M 000024 05+LISTB
M 000054 06+LISTC
M 000058 06+ENDC
M 000064 06+LISTB
E 000000
H PROGB 000000 000088
D LISTB 000060 ENDB 000070
R LISTA ENDA LISTC ENDC
T 000036 11 031000000 772027 05100000
T 000070 18 100000 05100000 05100020 05100030 100000
M 000037 05+LISTA
M 000044 05+ENDA
M 000070 06+ENDA
M 000074 06+ENDC
M 000078 06+ENDC
M 000082 06+ENDA
E 000000
H PROGC 000000 000057
D LISTC 00003 ENDC 00042
R LISTA ENDA LISTB ENDB
T 000018 12 031000000 77100004 05100000
T 000042 15 100030 100008 1000011 100000 100000
M 000019 05+LISTA
M 000023 05+LISTB
M 000027 05+ENDA
M 000048 06+LISTA
M 000051 06+ENDA
M 000054 06+LISTB
E 000000
Output:
Enter the location where the program has to be load: 7000
Output.dat:
===========================================================
|| CSECT || SYMNAME || ADDRESS || LENGTH ||
===========================================================
PROGA 7000 70
LISTA 7040 0
ENDA 7054 0
PROGB 7046 88
LISTB 70a6 0
ENDB 70b6 0
PROGC 709e 57
LISTC 70a1 0
ENDC 70e0 0
RESULT:
Thus, pass one of a direct-linking loader is implemented successfully in C language.
Ex.No:10 IMPLEMENTATION OF PASS 2 OF A DIRECT LINKING LOADER
AIM:
To write a C program to implement pass 2 of a direct linking loader.
ALGORITHM:
1. Assign the control section address in a variable ,CSADR
2. Read the header record.
§ From the information available in the header record read ,store the control section length in a variable.
3. Do the following process until an end record is reached.
§ If the subsequent records read is a ‘text record T’ , and if the object code is in character form convert it into machine representation, and move the object code from the record to the memory location control sections address plus the specified address in the text record.
§ If the subsequent records read is modification record ‘M’ then search for the modifying symbol name in the external symbol table created by pass 1 if it is found then add or subtract the corresponding symbol address found with the value starting at the location.
4. Add the control section length to the current control sections address to find the address of the next control section and repeat the entire process until there is no more input.
PROGRAM:
#include
#include
#include
#define MAX 10
struct estab
{
char csect[10];
char sym_name[10];
long int add;
int length;
}
table[MAX];
void main()
{
FILE *fp1,*fp2;
char input[10];
long int i,count=0,start,length,loc;
clrscr();
fp1=fopen("link.dat","r");
fp2=fopen("linko.dat","w");
printf("\n Enter the location where the program has to loaded:");
scanf("%lx",&start);
fprintf(fp2,"CSECT\tSymName\tAddress\tLength\n");
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s",input);
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",input);
strcpy(table[count].csect,input);
strcpy(table[count].sym_name,"\0");
fscanf(fp1,"%s",input);
table[count].add=atoi(input)+start;
fscanf(fp1,"%s",input);
}
if(strcmp(input,"\n")==0)
{
fscanf(fp1,"%s%1x",input,&loc);
break;
while((strcmp(input,"R"))!=0)
{
strcpy(table[count].csect,"\0");
strcpy(table[count].sym_name,input);
table[count].add=loc+start;
table[count++].length=0;
fscanf(fp1,"%s%1x",input,&loc);
}
while(strcpy(input,"T")!=0)
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
while(strcmp(input,"E")!=0)
fscanf(fp1,"%s",input);
//fscanf(fp1,"%s",input);
start=start+length;
}
for(i=0;i
fprintf(fp2,"%s\t%s\t%1x\t%d\n",table[i].csect,table[i].sym_name,table[i].length);
getch();
}
INPUT:
Link.dat
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 1500014
T 000052 16 100014 15100006 00002F FFFFC0
M 000024 05 +LISTB
M 000054 06 +LISTC
M 000064 06 +ENDC
M 000064 06 +LISTB
E 000000
H PROGB 000000 000088
D LISTB 000060 ENDB 000070
R LISTA ENDA LISTC ENDC
T 000036 11 03100000 772027 05100000
T 000070 18 100000 05100006 05100030 100000
M 000037 05 +LISTA
M 000044 05 +ENDA
M 000070 06 +ENDA
M 000074 06 +ENDC
M 000078 06 +ENDC
M 000082 06 +ENDA
E 000000
H PROGC 00000 0000057
D LISTC 000030 ENDC 000042
R LISTA ENDA LISTB ENDB
T 000018 12 03100000 7710004 05100000
T 000042 15 100030 100008 1000011 1000011 100000 100000
M 000019 05 +LISTA
M 000023 05 +LISTB
M 000027 05 +ENDA
M 000048 06 +LISTA
M 000051 06 +ENDA
M 000054 06 +LISTB
E 000000
OUTPUT:
Linko.dat
CSECT SymNmae Address Length
PROGA d 70
PROGB D 64
RESULT:
Thus the pass 2 of Direct Linking loader is executed successfully.
Ex.No:11 Implement a simple text editor with features like insertion / deletion of a character, word, and sentence
AIM:
To implement simple text editor with features like insertion/deletion of a character, word and sentence in C language.
ALGORITHM:
STEP 1: Design a Menu using switch case.
STEP 2: Get the choice
STEP 3: If choice is “1” then, enter the filename in which the text is to be saved
and, type the text using editor and type CTRL+Z to terminate.
STEP 4: If choice is “2” then enter the filename to be open, If the filename is
found, open the file in read mode else display not found
STEP 5: 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
#include
#include
#include
void newf();
void view();
void saveas();
void modify();
void main()
{
int op;
clrscr();
do
{
printf("\n\t\t============");
printf("\n\t\t|| MENU ||\n");
printf("\t\t============\n");
printf("\t\t1.New\n");
printf("\t\t2.Open\n");
printf("\t\t3.Save As\n");
printf("\t\t4.Modify\n");
printf("\t\t5.Exit\n");
printf("Enter u'r choice: ");
scanf("%d",&op);
switch(op)
{
case 1:
newf();
break;
case 2:
view();
break;
case 3:
saveas();
break;
case 4:
modify();
break;
case 5:
exit(0);
default:
printf("\nWrong choice!!!");
break;
}
}while(op!=5);
}
void newf()
{
FILE *f;
char fname[20],c;
printf("\nEnter the Filename: ");
scanf("%s",fname);
printf("\nType the content and CTRL+Z to terminate:\n");
f=fopen(fname,"w");
rewind(f);
while((c=getchar())!=EOF)
{
putc(c,f);
}
fclose(f);
}
void view()
{
FILE *f;
char fname[20],c;
printf("\nEnter the name of the file:");
scanf("%s",&fname);
if(searchpath(fname))
{
f=fopen(fname,"r");
while((c=getc(f))!=EOF)
{
printf("%c",c);
}
}
else
printf("\nThe file %s does not exist",fname);
fclose(f);
}
void saveas()
{
FILE *f1,*f2;
char c,sou[20],des[20];
printf("\nEnter the Source file name: ");
scanf("%s",sou);
if(searchpath(sou))
{
printf("Enter the Destination file name: ");
scanf("%s",des);
f1=fopen(sou,"r");
f2=fopen(des,"w");
while((c=getc(f1))!=EOF)
{
putc(c,f2);
}
fclose(f1);
fclose(f2);
}
else
printf("\nFile does not exist");
getch();
}
void modify()
{
int ch1;
FILE *f1;
char c,*word,*sent,fname[20];
printf("Enter the filename to be modified: ");
scanf("%s",fname);
if(searchpath(fname))
{
printf("\n1.Character");
printf("\n2.Word");
printf("\n3.Sentence");
printf("\nEnter U'r choice: ");
scanf("%d",&ch1);
if(ch1==1)
{
f1=fopen(fname,"a+");
fseek(f1, 0L, SEEK_END);
printf("Enter the character and CTRL+Z to exit:\n ");
while((c=getchar())!=EOF)
{
putc(c,f1);
}
}
else if(ch1==2)
{
printf("Enter the word: ");
scanf("%s",word);
f1=fopen(fname,"a+");
fseek(f1, 0L, SEEK_END);
fputs(word,f1);
}
else
{
printf("Enter the sentence and CTRL+Z to exit: ");
f1=fopen(fname,"a+");
fseek(f1, 0L, SEEK_END);
while((c=getchar())!=EOF)
{
putc(c,f1);
}
}
}
else
printf("\nFilename does not exist");
fcloseall();
}
OUTPUT:
========
|| MENU ||
========
1. NEW
2. OPEN
3. SAVE As
4. MODIFY
5. EXIT
Enter u'r choice: 1
Enter the Filename: cse
Type the content and CTRL+Z to terminate:
We are cse department...
we are very good students...
we are always smart...
^Z
========
|| MENU ||
========
1. NEW
2. OPEN
3. SAVE As
4. MODIFY
5. EXIT
Enter u'r choice: 2
Enter the name of the file:cse
We are cse department...
we are very good students...
we are always smart...
========
|| MENU ||
========
1. NEW
2. OPEN
3. SAVE As
4. MODIFY
5. EXIT
Enter u'r choice: 3
Enter u'r choice: 3
Enter the Source file name: cse
Enter the Destination file name: cse_dept
========
|| MENU ||
========
1. NEW
2. OPEN
3. SAVE As
4. MODIFY
5. EXIT
Enter u'r choice: 4
Enter the filename to be modified: cse
1. CHARACTER
2. WORD
3. SENTENCE
Enter U'r choice: 3
Enter the sentence and CTRL+Z to exit: we are an indian...
we are speaking in tamil...
^Z
========
|| MENU ||
========
1. NEW
2. OPEN
3. SAVE As
4. MODIFY
5. EXIT
Enter u'r choice: 2
Enter the name of the file:cse
We are cse department...
we are very good students...
we are always smart...
we are speaking in tamil...
========
|| MENU ||
========
1. NEW
2. OPEN
3. SAVE As
4. MODIFY
5. EXIT
Enter u'r choice: 5
Exiting...
RESULT:
Thus a simple text editor with features like insertion/deletion of a character, word and sentence is implemented successfully in C language.
Ex.No:12 IMPLEMENT SYMBOL TABLE WITH SUITABLE HASHING
AIM:
To write a C program to implement a symbol table using Hashing technique.
ALGORITHM:
1.Start the program
2.Declare the required variable
3.List the operation that are to be performed
4.If choice 1 read the address and name of the label from the user until the option ‘N’ is given
5.If choice 2 read the label from the user and search the user and search in the Hash table. If the value is present print the address else print the label is not present.
6.If the choice 3 then exit the program
7.Stop the program
PROGRAM:
#include
#include
void main()
{
int a[50],b[50],n,i,data,temp,key,s,k,y=1,flag=0;
clrscr();
printf("\t\t HASHING TABLE\n");
while(y==1)
{
printf("enter how many numbers:");
scanf("%d",&n);
printf("enter the elements one by one\n");
for(i=0;i
{
b[i]=0;
}
for(i=0;i
{
scanf("%d",&data);
temp=data%n;
if(b[temp]==0)
a[temp]=data;
else
{
while(b[temp]==1&&temp
temp ++;
if(temp>=n)
temp=0;
while(b[temp]==1)
temp++;
a[temp]=data;
}
b[temp]=1;
}
printf("\n Data in hash table:\n");
printf("\t Location\t Value");
for(i=0;i
{
printf("\n\t %d\t\t%d",i,a[i]);
}
printf("\n enter the search element:");
scanf("%d",&s);
key=s%n;
i=key;
while(i>=0)
{
if(a[i]==s)
{
printf("Element %D is present in %d position\n",s,i);
flag=1;
break;
}
else if(i<=n)
{
i++;
}
else
{
for(i=0;i<=key;i++)
{
if(a[i]==s)
{
flag=1;
break;
}
i++;
}
}
if(flag!=0)
{
printf("element %d is not present\n",s);
}
}
printf("Do u want to continue?(0-exit,1-continue)");
scanf("%d",&y);
}
getch();
}
RESULT:
Thus the C program to implement Symbol table using Hashing technique has been executed.
No comments:
Post a Comment