You are on page 1of 32

Ex.

No:01
AIM:
IMPLEMENTATION OF SYMBOL TABLE
 To write a "C" program for the implementation of symbol table with functions to create,
DATE:

AIM:

To write a “C” program for the implementation of symbol table with function to create,
insert, modify, search and display.

ALGORITHM:

1. Start the program for performing inert, display, delete, search and modify option in
symbol table.
2. Define the structure of the symbol table.
3. Enter the choice for performing the operation in the symbol table.
4. IF the entered choice is 1, Search the symbol table for the symbol to be inserted. If the
symbol is already present, it display “Duplicate symbol”. Else, insert the symbol and
the corresponding address in the symbol table.
5. If the choice is 2, the symbol present in the table is displayed.
6. If the choice is 3, the symbol table to be deleted is searched in the symbol table. If it is
not found in the symbol table is displays “Label not found”. Else the symbol is deleted.
7. If the choice is 5, the symbol table to be modified is searched in the symbol table. The
label or address or both can be modified

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
int size=0;
void Insert();
void Display();
void Delete();
int Search(char lab[]);
void Modify();
struct SymbTab
{
 char label[10],symbol[10];
 int addr;
struct SymbTab *next;};
struct SymbTab *first,*last;
void main()
{
 int op,y;
 char la[10];
 clrscr();
 do
 {
  printf("\n\tSYMBOL TABLE IMPLEMENTATION\n");
  printf("\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.MODIFY\n\t6.END\n");
  printf("\n\tEnter your option : ");
  scanf("%d",&op);
  switch(op)
 {
   case 1:
      Insert();
      break;
   case 2:
      Display();
      break;
   case 3:
      Delete();
      break;
   case 4:
      printf("\n\tEnter the label to be searched : ");
      scanf("%s",la);
      y=Search(la);
      printf("\n\tSearch Result:");
      if(y==1)
    printf("\n\tThe label is present in the symbol table\n");
      else
    printf("\n\tThe label is not present in the symbol table\n");
      break;
   case 5:
      Modify();
      break;
   case 6:
      exit(0);
 }
 }while(op<6);
 getch();
}
void Insert()
{
  int n;
  char l[10];
  printf("\n\tEnter the label : ");
  scanf("%s",l);
  n=Search(l);
  if(n==1)
   printf("\n\tThe label exists already in the symbol table\n\tDuplicate can't be inserted");
  else
   {
    struct SymbTab *p;
    p=malloc(sizeof(struct SymbTab));
    strcpy(p->label,l);
    printf("\n\tEnter the symbol : ");
    scanf("%s",p->symbol);
    printf("\n\tEnter the address : ");
    scanf("%d",&p->addr);
    p->next=NULL;
    if(size==0)
     {
      first=p;
      last=p;
     }
    else
     {
      last->next=p;
      last=p;
     }
    size++;
   }
   printf("\n\tLabel inserted\n");
}
void Display()
{
  int i;
  struct SymbTab *p;
  p=first;
  printf("\n\tLABEL\t\tSYMBOL\t\tADDRESS\n");
  for(i=0;i<size;i++)
   {
    printf("\t%s\t\t%s\t\t%d\n",p->label,p->symbol,p->addr);
    p=p->next;
   }
}
int Search(char lab[])
{
 int i,flag=0;
 struct SymbTab *p;
 p=first;
  for(i=0;i<size;i++)
   {
    if(strcmp(p->label,lab)==0)
     flag=1;
    p=p->next;
   }
 return flag;
}
void Modify()
{
  char l[10],nl[10];
  int add,choice,i,s;
  struct SymbTab *p;
  p=first;
  printf("\n\tWhat do you want to modify?\n");
  printf("\n\t1.Only the label\n\t2.Only the address\n\t3.Both the label and address\n");
  printf("\tEnter your choice : ");
  scanf("%d",&choice);
  switch(choice)
   {
     case 1:
       printf("\n\tEnter the old label : ");
       scanf("%s",l);
       s=Search(l);
       if(s==0)
     printf("\n\tLabel not found\n");
       else
    {
     printf("\n\tEnter the new label : ");
     scanf("%s",nl);
     for(i=0;i<size;i++)
      {
       if(strcmp(p->label,l)==0)
         strcpy(p->label,nl);
       p=p->next;
      }
     printf("\n\tAfter Modification:\n");
     Display();
    }
    break;
    case 2:
       printf("\n\tEnter the label where the address is to be modified : ");
       scanf("%s",l);
       s=Search(l);
       if(s==0)
     printf("\n\tLabel not found\n");
       else
    {
     printf("\n\tEnter the new address : ");
     scanf("%d",&add);
     for(i=0;i<size;i++)
      {
       if(strcmp(p->label,l)==0)
        p->addr=add;
       p=p->next;
      }
     printf("\n\tAfter Modification:\n");
     Display();
    }
    break;
    case 3:
       printf("\n\tEnter the old label : ");
       scanf("%s",l);
       s=Search(l);
       if(s==0)
     printf("\n\tLabel not found\n");
       else
    {
     printf("\n\tEnter the new label : ");
     scanf("%s",nl);
     printf("\n\tEnter the new address : ");
     scanf("%d",&add);
     for(i=0;i<size;i++)
      {
       if(strcmp(p->label,l)==0)
        {
         strcpy(p->label,nl);
         p->addr=add;
        }
       p=p->next;
      }
     printf("\n\tAfter Modification:\n");
     Display();
    }
    break;
    }
}
void Delete()
{
  int a;
  char l[10];
  struct SymbTab *p,*q;
  p=first;
  printf("\n\tEnter the label to be deleted : ");
  scanf("%s",l);
  a=Search(l);
  if(a==0)
    printf("\n\tLabel 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--;
    printf("\n\tAfter Deletion:\n");
    Display();
   }}

OUTPUT:

SYMBOL TABLE IDENTIFICATION:

1.insert
2.display
3.delete
4.search
5.modify
6.end
Enter your option:1
Enter the label :plus
Enter the symbol:+
Enter the address:100
Label inserted

1.insert
2.display
3.delete
4.search
5.modify
6.end
Enter your option:1
Enter the label :minus
Enter the symbol:-
Enter the address:200
Label inserted

1.insert
2.display
3.delete
4.search
5.modify
6.end
Enter your option:2
Label           symbol address
Plus                  + 100
Minus                - 200

1.insert
2.display
3.delete
4.search
5.modify
6.end
Enter your option:3
Enter the label to be deleted:minus
After deletion:
Label           symbol address
Plus                  + 100
1.insert
2.display
3.delete
4.search
5.modify
6.end
Enter your option:6

RESULT:

The Implementation of Symbol Table program is successfully compiled and executed.


Ex. No:02
AIM:
IMPLEMENTATION OF PASS ONE OF TWO PASS
ASSEMBLER
DATE:

AIM:

To write a "C" program for the implementation of pass one of a two pass assembler.

ALGORITHM:

1.Start the program execution

2.Include all header files that are required

3.Include main function, declare all variables needed

4.Initialize two input files and two output files

5.Get input.dat and optab.dat from user

6.The input data consists of mnemonics and opcode

7.Design the program to create an intermediate file from input files

8.Result are stored in output files sym.dat and out.dat

9.Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char opcode[10],operand[10],label[10],code[10][10],ch; char mnemonic[10]
[10]={"START","LDA","STA","LDCH","STCH","END"};
 int locctr,start,len,i=0,j=0;
 FILE *fp1,*fp2,*fp3;
 clrscr();
 fp1=fopen("imput.txt","r");
 fp2=fopen("symtab.txt","w");
 fp3=fopen("out.txt","w");
 fscanf(fp1,"%s%s%s",label,opcode,operand);
 if(strcmp(opcode,"START")==0)
 {
   start=atoi(operand);
   locctr=start;
   fprintf(fp3,"%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",locctr);
   if(strcmp(label,"**")!=0)
     fprintf(fp2,"%s\t%d\n",label,locctr);
   strcpy(code[i],mnemonic[j]);
   while(strcmp(mnemonic[j],"END")!=0)
    {
     if(strcmp(opcode,mnemonic[j])==0)
     {
      locctr+=3;
      break;
     }
     strcpy(code[i],mnemonic[j]);
     j++;
    }
   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,"\t%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);
  fcloseall();
  printf("\n\nThe contents of Input Table :\n\n");
  fp1=fopen("INPUT.DAT","r");
  ch=fgetc(fp1);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp1);
   }
  printf("\n\nThe contents of Output Table :\n\n\t");
  fp3=fopen("OUT.DAT","r");
  ch=fgetc(fp3);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp3);
   }
  len=locctr-start;
  printf("\nThe length of the program is %d.\n\n",len);
  printf("\n\nThe contents of Symbol Table :\n\n");
  fp2=fopen("SYMTAB.DAT","r");
  ch=fgetc(fp2);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp2);
   }
  fcloseall();
  getch();
}

INPUT FILE: 

input.txt
** START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C'Z'
C1 RESB 1
** END **

optab.txt

START
LDA 
STA
LDCH
STCH
END
OUTPUT:
Out.txt

** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C’Z’
2019 C1 RESB 1
2020 ** END **

RESULT:

The Implementation of pass one of two pass assembler program is compiled and executed
successfully.
Ex. No:03
AIM:
IMPLEMENTATION OF PASS TWO OF TWO PASS
ASSEMBLER
DATE:

AIM:

To write a "C" program for the implementation of pass two of a two pass assembler.

ALGORITHM:

1.Start the program execution

2.Include all header files that are required

3.Include main function, declare all variables needed

4.Initialize two input files and two output files

5.Get input.dat and optab.dat from user

6.The input data consists of mnemonics and opcode

7.Design the program to create an intermediate file from input files

8.Result are stored in output files sym.dat and out.dat

9.Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char a[10],ad[10],label[10],opcode[10],operand[10],mnemonic[10],symbol[10];
int i,address,code,add,len,actual_len; FILE
*fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("assmlist.txt","w");

fp2=fopen("symtab.txt","r");
fp3=fopen("intermediate.txt","r");
fp4=fopen("optab.txt","r"); fscanf(fp3,"%s%s
%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{ fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"BYTE")==0)
{ fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
len=strlen(operand);
actual_len=len-3;
for(i=2;i<(actual_len+2);i++)
{ itoa(operand[i],ad,16);
fprintf(fp1,"%s",ad);
}
fprintf(fp1,"\n");
}
else if(strcmp(opcode,"WORD")==0)
{ len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
}
else if((strcmp(opcode,"RESB")==0)(strcmp(opcode,"RESW")==0))
{
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
}
else
{ rewind(fp4); fscanf(fp4,"%s
%d",mnemonic,&code);
while(strcmp(opcode,mnemonic)!=0)
fscanf(fp4,"%s%d",mnemonic,&code);
if(strcmp(operand,"**")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",address,label,opcode,operand,code);
}
else
{
rewind(fp2);

fscanf(fp2,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
{
fscanf(fp2,"%s%d",symbol,&add);
}
fprintf(fp1,"%d\t%s\t%s\t%s\t%d%d\n",address,label,opcode,operand,code,add);
}
}
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
} fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
printf("Finished");
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4); getch();
}

INPUT FILES:

INTERMEDIATE.txt
** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C'EOF'
2019 C1 RESB 1
2020 ** END **

OPTAB.txt
 LDA 33
STA 44
LDCH 53
STCH 57
END *

SYMTAB.txt
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019

OUTPUT:

ASSMLIST.txt
** START 2000
2000 ** LDA FIVE 332015
2003 ** STA ALPHA 442012
2006 ** LDCH CHARZ 532018
2009 ** STCH C1 572019
2012 ALPHA RESW 1
2015 FIVE WORD 5 000005
2018 CHARZ BYTE C'EOF' 454f46
2019 C1 RESB 1
2020 ** END **

RESULT:
The Implementation of pass two of two pass assembler program is successfully complied
and executed successfully.
Ex. No:04
AIM:
IMPLEMENTAION OF ABSOLUTE LOADER

DATE:

AIM:
To write a "C" program for the implementation of an Absolute Loader.

ALGORITHM:

1.Start the program


2.Assign the required variable
3.Open the files fp1=fopen(“input2.dat”,r”);fp2=fopen(“out2.dat”,w);
4.Read the content
5.Using while loop perform the loop until character is not equal to E
6.Then compare whether the character is equal to H
7.if H then get the starting address, length and input
8.Else if the character is T then store the string as the three address in the ouyput file with
the input[0],input[1] for address input[2], input [3] for address+1 input[4], input[5] for
address+2
9.Else if it is not H or T then perform the following fprintf in fp2 for output file for,
input[0], input[1] for address input[2], input[3] for address+1 input[4], input[5] for
address+2
10. Increment the address value by3
11. Read the next input string and repeat from step 4
12. Finally terminate the proram

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
char input[10],label[10],ch1,ch2;
int addr, w=0, start, ptaddr, l, length=0, end, count=0, k, taddr, address, i=0;
FILE *fp1,*fp2;
void check();
void main()
{  clrscr();
  fp1=fopen("INPUT.txt","r");
  fp2=fopen("OUTPUT.txt","w");
  fscanf(fp1,"%s",input);
  printf("\n\n\t\t\t\tABSOLUTE LOADER\n");
  fprintf(fp2,"\n-------------------------------------------------------\n");
  fprintf(fp2,"MEMORY ADDRESS\t\t\tCONTENTS");
  fprintf(fp2,"\n-------------------------------------------------------\n");
  while(strcmp(input,"E")!=0)
 {
   if(strcmp(input,"H")==0)
   {
    fscanf(fp1,"%s %x %x %s",label,&start,&end,input);
    address=start;
   }
   else if(strcmp(input,"T")==0)
   {
    l=length;
   ptaddr=addr;
    fscanf(fp1,"%x %x %s",&taddr,&length,input);
    addr=taddr;
    if(w==0)
    {
     ptaddr=address;
     w=1;
    }
    for(k=0;k<(taddr-(ptaddr+l));k++)
    {
     address=address+1;
     fprintf(fp2,"xx");
     count++;
     if(count==4)
     {
      fprintf(fp2,"  ");
      i++;
      if(i==4)
      {
       fprintf(fp2,"\n\n%x\t\t",address);
       i=0;
      }
      count=0;
     }
    }
    if(taddr==start)
     fprintf(fp2,"\n\n%x\t\t",taddr);
    fprintf(fp2,"%c%c",input[0],input[1]);
    check();
    fprintf(fp2,"%c%c",input[2],input[3]);
    check();
    fprintf(fp2,"%c%c",input[4],input[5]);
    check();
    fscanf(fp1,"%s",input);
   }
   else
   {
    fprintf(fp2,"%c%c",input[0],input[1]);
    check();
    fprintf(fp2,"%c%c",input[2],input[3]);
    check();
    fprintf(fp2,"%c%c",input[4],input[5]);
    check();
    fscanf(fp1,"%s",input);
   }
 }
  fprintf(fp2,"\n-------------------------------------------------------\n");
  fcloseall();
  printf("\n\n The contents of output file:\n\n");
  fp2=fopen("OUTPUT.DAT","r");
  ch2=fgetc(fp2);
  while(ch2!=EOF)
 {
   printf("%c",ch2);
   ch2=fgetc(fp2);
 }
  fcloseall();
  getch();
}
void check()
{
  count++;
  address++;
  taddr=taddr+1;
  if(count==4)
 {
   fprintf(fp2,"  ");
   i++;
   if(i==4)
   {
    fprintf(fp2,"\n\n%x\t\t",taddr);
    i=0;
   }
   count=0;
 }
}

INPUT FILE:

INPUT.txt
H COPY 001000 00107A
T 001000 1E 141033 482039 001036 281030 301015 482061 3C1003 00102A 0C1039 00102D
T 00101E 15 0C1036 482061 081033 4C0000 454F46 000003 000000
T 001047 1E 041030 001030 E0205D 30203F D8205D 281030 302057 549039 2C205E 38203F
T 001077 1C 101036 4C0000 000000 001000 041030 E02079 302064 509039 DC2079 2C1036
E 001000

OUTPUT:

INPUT.txt

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:

The Implementation of Absolute Loader program is successful compiled and executed.


Ex. No:05
IM:
IMPLEMENTAION OF TEXT EDITOR

DATE:

AIM:

To write a "C" program to implement "Text Editor" with features like insertion and
deletion.

ALGORITHM:

1.Display options new, open and exit and get choice.


2.If choice is 1 , call Create() function.
3.If choice is 2, call Display() function.
4.If choice is 3, call Append() function.
5.If choice is 4, call Delete() function.
6.If choice is 5, call Display() function.
7.Create()
1. Get the file name and open it in write mode.
     2. Get the text from the user to write it.
8.Display()
     1 Get the file name from user.
   2 Check whether the file is present or not.
     3 If present then display the contents of the file.
9.Append()
    1 Get the file name from user.
    2 Check whether the file is present or not.
    3 If present then append the file by getting the text to add with the existing file.
10. Delete()
    1 Get the file name from user.
    2 Check whether the file is present or not.
    3 If present then delete the existing file.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
int i,j,ec,fg,ec2;
char fn[20],e,c;
FILE *fp1,*fp2,*fp;
void Create();
void Append();
void Delete();
void Display();
void main()
{
 do {
  clrscr();
  printf("\n\t\t***** TEXT EDITOR *****");
  printf("\n\n\tMENU:\n\t-----\n ");
  printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.DELETE\n\t5.EXIT\n");
  printf("\n\tEnter your choice: ");
  scanf("%d",&ec);
  switch(ec)
 {
   case 1:
     Create();
     break;
   case 2:
     Display();
     break;
   case 3:
     Append();
     break;
   case 4:
     Delete();
     break;
   case 5:
     exit(0);
 }
 }while(1);
}
void Create()
{
 fp1=fopen("temp.txt","w");
 printf("\n\tEnter the text and press '.' to save\n\n\t");
 while(1)
 {
  c=getchar();
  fputc(c,fp1);
  if(c == '.')
 {
   fclose(fp1);
   printf("\n\tEnter then new filename: ");
   scanf("%s",fn);
   fp1=fopen("temp.txt","r");
   fp2=fopen(fn,"w");
   while(!feof(fp1))
   {
    c=getc(fp1);
    putc(c,fp2);
   }
   fclose(fp2);
   break;
  }}
}
void Display()
{
  printf("\n\tEnter the file name: ");
  scanf("%s",fn);
  fp1=fopen(fn,"r");
  if(fp1==NULL)
 {
   printf("\n\tFile not found!");
   goto end1;
 }
  while(!feof(fp1))
 {
   c=getc(fp1);
   printf("%c",c);
 }
end1:
  fclose(fp1);
  printf("\n\n\tPress any key to continue...");
  getch();
}
void Delete()
{
  printf("\n\tEnter the file name: ");
  scanf("%s",fn);
  fp1=fopen(fn,"r");
  if(fp1==NULL)
 {
   printf("\n\tFile not found!");
   goto end2;
 }
  fclose(fp1);
  if(remove(fn)==0)
 {
   printf("\n\n\tFile has been deleted successfully!");
   goto end2;
 }
  else
   printf("\n\tError!\n");
end2: printf("\n\n\tPress any key to continue...");
  getch();
}
void Append()
{
  printf("\n\tEnter the file name: ");
  scanf("%s",fn);
  fp1=fopen(fn,"r");
  if(fp1==NULL)
 {
   printf("\n\tFile not found!");
   goto end3;
 }
  while(!feof(fp1))s
 {
   c=getc(fp1);
   printf("%c",c);
 }
  fclose(fp1);
  printf("\n\tType the text and press 'Ctrl+S' to append.\n");
  fp1=fopen(fn,"a");
  while(1)
 {
   c=getch();
   if(c==19)
    goto end3;
   if(c==13)
   {
    c='\n';
    printf("\n\t");
    fputc(c,fp1);
   }
   else
   {
    printf("%c",c);
    fputc(c,fp1);
   }
 }
end3: fclose(fp1);
  getch();
}

OUTPUT:

Menu:
1.create
2.display
3.append
4.delete
5.exit
Enter your choice:1
Enter the text and press ’.’ to save
Hi.
Enter the new filename:out

Menu:
1.create
2.display
3.append
4.delete
5.exit
Enter your choice:2
Enter the filename:out
Hi

Menu:
1.create
2.display
3.append
4.delete
5.exit
Enter your choice:4
Enter the filename:out
File has been deleted successfully.

Menu:
1.create
2.display
3.append
4.delete
5.exit
Enter your choice:5

RESULT:

The Implementation of Text Editor program is compiled and executed successfully.

You might also like