Symbol Table EX - NO: Date: Aim
Symbol Table EX - NO: Date: Aim
EX.NO:
DATE :
AIM:
To write a C program to implement Symbol Table
ALGORITHM:
1. Start the program for performing insert, display, delete, search and modify
option in symbol table
2. Define the structure of the Symbol Table
3. Enter the choice for performing the operations in the symbol Table
4. If the entered choice is 1, search the symbol table for the symbol to be
inserted.
5. If the symbol is already present, it displays "Duplicate Symbol".
6. Else, insert the symbol and the corresponding address in the symbol table.
7. If the entered choice is 2, the symbols present in the symbol table are
displayed.
8. If the entered choice is 3, the symbol to be deleted is searched in the symbol
table.
9. If it is not found in the symbol table it displays "Label Not found". Else, the
symbol is deleted.
10.If the entered choice is 5, the symbol to be modified is searched in the
symbol table.
11. The label or address or both can be modified.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0,k=0,opt,b[200],tab[200];
char ch,oz;
clrscr();
printf("\n Symbol Table Creation:");
option:
printf("\n1.Create table \n2.Insert character \n3.Modify character \n4.Search character
\n5.Display table \n6.Exit");
printf("Enter ur choice:\t");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the total no of character to be inserted in a table\t");
fflush(stdin);
scanf("%d",&k);
printf("\nTable created for %d character\t",k);
goto option;
case 2:
printf("\nEnter the character \t");
fflush(stdin);
scanf("%c",&ch);
tab[i]=((int)ch);
printf("\nEnter the reference number\t");
scanf("%d",&b[i]);
i++;
goto option;
case 3:
printf("\nEnter the character to be modify\t");
fflush(stdin);
scanf("%c",&ch);
if(k==0)
printf("\nCreate the table first");
for(i=0;i<k;i++)
{if(ch==tab[i])
j=1;
else
j=0;
if(j==1)
{
printf("\nEnter the new character to be modify:\t");
fflush(stdin);
scanf("%c",&oz);
tab[i]=((int)oz);
printf("\nCharacter modified");
goto option;
}}
case 4:
printf("\nEnter the character to be searched:\n");
fflush(stdin);
scanf("%c",&ch);
if(k==0)
printf("\nCreate the table first");
for(i=0;i<k;i++)
{
if(ch==tab[i])
j=1;
else
j=0;
if(j==1)
printf("\nCharacter found at %d position",i);
else
printf("\nCharacter not found at %d position",i);}
goto option;
case 5:
printf("\nThe created table is shown below\n");
printf("\nS.no \tchar \tRegno\n");
for(i=0;i<k;i++)
printf("\n%d\t%c\t%d\n",i,tab[i],b[i]);
fflush(stdin);
goto option;
case 6:
exit(0);
}
getch();
}
OUTPUT:
SYMBOL TABLE
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
Ram 25
Raja 35
Variable Value
Dino 97
Raja 35
Ramesh 62
RESULT:
Thus a ‘C’ program for implementation of symbol table is written, executed
and output is verified.
PASS ONE OF PASS TWO ASSEMBLER
EX.NO:
DATE :
AIM:
To write a C program to implement pass one of pass two assembler.
ALGORITHM :
1. Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode
2. Read the source program
3. If the opcode read in the source program is START, the variable location
counter is initialized with the operand value.
4. Else the location counter is initialized to 0.
5. The source program is read line by line until the reach of opcode END.
6. Check whether the opcode read is present in the operation code table.
7. If the opcode is present, then the location counter is incremented by 3.
8. If the opcode read is WORD, the location counter is incremented by3.
9. If the opcode read is RESW, the operand value is multiplied by 3 and then
the location counter is incremented.
10.If the opcode read is RESB, the location counter value is incremented by
operand value.
11.If the opcode read is BYTE, the location counter is auto incremented.
12.The length of the source program is found using the location counter value.
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char opcode[10],mnemonic[3],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("input.dat","r");
fp2=fopen("symbol.dat","w");
fp3=fopen("out.dat","w");
fp4=fopen("optab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);}
else
locctr=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,locctr);
fscanf(fp4,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if(strcmp(opcode,code)==0)
{
locctr+=3;
break;
}
fscanf(fp4,"%s%s",code,mnemonic);
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
locctr+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++locctr;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);}
fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
length=locctr-start;
printf("the length of a program is %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
OUTPUT:
RESULT:
Thus a ‘C’ program for implementation of pass one of pass two assembler is
written, executed and output is verified.
PASS TWO OF PASS TWO ASSEMBLER
EX.NO:
DATE :
AIM:
To write a c program to implement pass two of pass two assembler
ALGORITHM:
RESULT:
Thus a ‘C’ program for implementation of pass 1 of pass 2 assembler is
written, executed and output is verified.
IMPLEMENTATION OF AN ASSEMBLER
EX.NO:
DATE :
AIM:
To write a ‘C’ program for the implementation of an assembler.
ALGORITHM:
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{char temp[20],temp1[20],ins[20],code[5];
int i,j,adder,byte;
FILE *f1,*f2,*f3;
clrscr();
f1=fopen("pro.dat","r");
f2=fopen("opcode1.dat","r");
f3=fopen("out.dat","w");
adder=1000;
while(!feof(f1))
{i=0;
j=0;
fgets(temp,20,f1);
while(temp[i]!='\n')
{if(temp[i]>='A'&&temp[i]<-'Z')
{temp[j]=temp[i];
j++;}
i++;}
temp1[j]='\0';
f2=fopen("opcode1.dat","r");
while(!feof(f2))
{fscanf(f2,"%s%d%s",ins,&byte,code);
printf("\n%s%s\n Byte:%d\n Code:%s\n\n",ins,byte,code);
getch();
fprintf(f3,"%d\t%s\t%s",adder,temp,code);
switch(byte)
{case 1:
{fprintf(f3,"\n");
break;}
case 2:
{fprintf(f3,"\n",temp[i-1],temp[i-2]);
break;}
case 3:
{fprintf(f3,"\n%c%c%c%c\n",temp[i-1],temp[j-1],temp[i-4],temp[i-3]);
break;}}
adder=adder+byte;
break;}
fclose(f2);}
getch();}
OUTPUT:
pro.dat
LDA
MOV A B
STA
opcode1.dat
LDA 2 46
MOV B A 47
STA 2 77
out.dat
1000 LDA
46
1002 MOV A B
46
1004 STA
46
1006 STA
46
RESULT:
Thus the program for the implementation of an assembler is written,
executed and the output is verified.
IMPLEMENTATION OF MACROPROCESSOR
EX.NO:
DATE:
AIM:
To write a ‘C’program for the implementation of a macroprocessor.
ALGORITHM:
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
FILE*inpt,*inpt1,*oupt;
char mac_name[20],mac_val[20],tok_wrd[20],mac_wrd[20];
int replaced=0;
void main()
{clrscr();
inpt=fopen("source.c","r");
inpt1=fopen("mac.c","r");
oupt=fopen("macent.txt","w");
while(!feof(inpt))
{fscanf(inpt,"%s",&tok_wrd);
rewind(inpt1);
while(!feof(inpt1))
{fscanf(inpt1,"%s",&mac_wrd);
if(strcmp(tok_wrd,mac_val)==0)
{fscanf(inpt1,"%s",&mac_val);
fprintf(oupt," ");
printf("%s",mac_val);
replaced=1;}}
if(replaced==0)
{fprintf(oupt,tok_wrd);
fprintf(oupt," ");
printf("\n %s",tok_wrd);}
replaced=0;}
getch();
}
OUTPUT:
SOURCE.C
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
temp=a+b;
temp=a/b;
getch();
}
MAC.C
#define a 10
#define b 20
MACENT.txt
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
temp=10+20;
temp=10/20;
getch();
}
RESULT:
Thus the program for the implementation of a macroprocessor is written,
executed and the output is verified.
ABSOLUTE LOADER:
EX.NO:
DATE:
AIM:
To implement the Absolute Loader using ‘c’ language
ALGORITHM:
lout.dat
H COPYEE 002000 00107A
T 002000 1E 142023 483039 102036 282030 302015 483061 3C2003 00202A
T 00201E 15 2C2036 483061 182033 4C0000 454F46 200003 100000
T 002039 1E 242030 302030 E0305D 30303F D8305D 222030 303057 53A039 2C3
05E 38303F
T 002057 0A 102036 4C0000 F1 201000
T 002071 19 342030 E43079 303064 4FA039 DC3079 2C3036 383064 4C0000
E 002000
loadout.dat
2000 14202348 30391020 36282030 30201548
2010 30613C20 0300202A 2C203648 30611820
2020 334C0000 454F4620 00031000 00xxxxxx
2030 xxxxxx24 20303020 30E0305D 30303FD8
2040 305D2220 30303057 53A0392C 305E3830
2050 3F102036 4C0000F1 201000xx xxxxxxxx
2060 xxxxxxxx xxxxxxxx xxxxxx34 2030E430
2070 79303064 4FA039DC 30792C30 36383064
2080 4C0000
RESULT:
Thus a ‘C’ program for implementation of Absolute loader is written,
executed and output is verified.
IMPLEMENTATION OF A HASHING
EX.NO:
DATE :
AIM:
To write a ‘C’program for the implementation of a hashing
ALGORITHM:
continue(y/n)?
continue(y/n)?
the symbol table is
hash values address label
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 2009 !
8 2010 @
9 0
10 0
RESULT:
Thus the program for the implementation of hashing is written, executed
and the output is verified.
PASS 1 OF DIRECT LINKING LOADER
EX.NO:
DATE :
AIM:
To write a c program to Implement pass two of direct linking loader
ALGORITHM:
CODE.TXT
05
07
1A
1C
65022 0
65023 5
65024
65025 0
65026 7
65027
65028 1
65029
65030 A
65031
65032 1
65033
65034 C
65035
65036
65037
RESULT:
Thus the program for the implementation of pass one of a dynamic
linking loader is written, executed and the output is verified.
PASS 2 OF DIRECT LINKING LOADER
EX.NO:
DATE :
AIM:
To write a c program to Implement pass two of direct linking loader
ALGORITHM:
in.dat
H COPY 2400 15
T 2300 04 04766790
T 2300 04 05587643
E 3310 02 1123
out.dat
2300 04
2301 76
2302 67
2303 90
RESULT:
Thus a ‘C’ progrom for implementation pass two direct linking loader is
written, executed and output is verified.
IMPLEMENTATION OF RELOCATION LOADER
EX.NO:
DATE:
AIM:
To write a c program to Perform the Relocation Loader
ALGORITHM:
1. Start the program
2. Include the necessary header file and variable
3. Open the two file for fp1= relinput.dat and give read fp2= reloutput.dat and
give write
4. Read the content Using while loop perform the loop until character is not
equal to E
5. If the character is H
6. Get the variable add, length, and input
7. Else if the character is T
8. Get the variable address and bitmask
9. And perform the for loop for starting zero to up to len
10.Get the opcode ,addr and assign relocbit to bitmask
11.If relocabit is zero Then actualadd=addr; else
12.Add the addr and star value
13.Finally terminate the program
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
int locctr;
char add[10];
};
struct object_code code[500];
void main()
{
char input[100][16],output[100][16],binary[20],address[20],stloc[4];
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("RLOADIN.DAT","r");
fp2=fopen("RLOADOUT.DAT","w");
rewind(fp1);
rewind(fp2);
printf("\nEnter the location where the program has to be loaded:");
scanf("%s",stloc);
start=atoi(stloc);
location=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;i<(textloc-(tloc+tlen));n++)
{
strcpy(code[inc].add,"xx");
code[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<len;j++)
{
address[num]=output[i][j];
output[i][j]='\o';
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<len;n++)
{
code[inc].add[num++]=output[i][n];
if(num>1)
{
code[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",code[n].locctr);
for(n=0;n<inc;n++)
{
fprintf(fp2,"%s",code[n].add);
i++;
if(i>3)
{fprintf(fp2,"\t");
i=0;
count++;}
if(count>3)
{
fprintf(fp2,"\n%d\t",code[n+1].locctr);
count=0;}}
fclose(fp1);
fclose(fp2);
getch();
}
OUTPUT:
RLOADIN.DAT
RLOADOUT.DAT
AIM:
To implements a simple Text Editor with features like insertion/deletin of a
character, word and sentences.
ALGORITHM:
1.INSERTION:
TEXT EDITOR
SYSTEMSOFTWARE
01:101
2.DELETION:
TEXT EDITOR
01:00
RESULT:
Thus a ‘C’ program for implementation of text editor is written, executed
and output is verified.