Start the program for performing insert, display, delete, search and modify option insymbol table
Define the structure of the Symbol Table
Enter the choice for performing the operations in the symbol Table
If the entered choice is 1, search the symbol table for the symbol to be inserted.
If the symbol is already present, it displays "Duplicate Symbol". Else, insert the symbol and the corresponding address in the symbol table.
If the entered choice is 2, the symbols present in the symbol table are displayed.
If the entered choice is 3, the symbol to be deleted is searched in the symbol table.
If it is not found in the symbol table it displays "Label Not found". Else, the symbol is deleted.
If the entered choice is 5, the symbol to be modified is searched in the symbol table. The label or address or both can be modified.
Source Program:
#include<stdio.h> #include<conio.h> #include<string.h> int line_no,j=0; void create(); void insert(); void modify(); void search(); void display(); void input(); struct al { char label[10]; char opcode[10]; char operand[10]; }a[20]; struct sym { char symb[10]; int addr; }s[20]; void main() { int op; char ch[5]; clrscr(); input(); display(); do { printf("1.Create\n2.Modify\n3.Insert\n4.Search\n5.Exit\n"); printf("Enter Your Choice"); scanf("%d",&op); switch(op) { case 1: create(); break; case 2: modify(); printf("Symbol table Modified.."); break; case 3: insert(); printf("Insertion operation Performed.."); break; case 4: search(); break; case 5: exit(0); }printf("\n Do you want to continue(Y orN):"); scanf("%s",ch); }while(strcmp(ch,"Y")==0); getch(); } void input() { int i=0; printf("Enter the program:\n\n"); printf("LABEL\tOPCODE\tOPERAND\n"); do { scanf("%s\t%s\t%s",a[i].label,a[i].opcode,a[i].operand); i++; }while(strcmp(a[i-1].opcode,"END")); line_no =i; } void display() { int i; printf("The assembly Language program is \n"); for(i=0;i<line_no;i++) { printf("\n%d\t%s\t%s\t%s\n",i,a[i].label,a[i].opcode,a[i].operand); } } void create() { int locctr,i; for(i=0;i<line_no;i++) { if(strcmp(a[i].opcode,"START")==0) locctr=atoi(a[i].operand); else if(strcmp(a[i-1].opcode,"START")==0); else { if(strcmp(a[i-1].opcode,"WORD")==0) locctr=locctr+3; else if(strcmp(a[i-1].opcode,"RESW")==0) locctr = locctr+3*atoi(a[i-1].operand); else if(strcmp(a[i-1].opcode,"BYTE")==0) locctr = locctr+strlen(a[i-1].operand); else locctr = locctr+3; } if(strcmp(a[i].label,"$")!=0) { strcpy(s[j].symb,a[i].label); s[j].addr = locctr; printf("\n%s\t%d\n",s[j].symb,s[j].addr); }j++; } } void modify() { int ln; printf("Enter the line number to be modified:"); scanf("%d",&ln); scanf("%s\t%s\t%s",a[ln].label,a[ln].opcode,a[ln].operand); display(); create(); } void insert() { int i,ln; printf("Enter the line number to be inserted:"); scanf("%d",&ln); for(i=line_no;i>=ln;i--) { strcpy(a[i+1].label,a[i].label); strcpy(a[i+1].opcode,a[i].opcode); strcpy(a[i+1].operand,a[i].operand); } line_no++; printf("Enter the statement:\n"); scanf("%s\t%s\t%s",a[ln].label,a[ln].opcode,a[ln].operand); display(); create(); } void search() { har sh[10]; int i,t=0; printf("Enter the label to be searched"); scanf("%s",sh); for(i=0;i<line_no;i++) { if(strcmp(a[i].label,sh)==0) printf("The label is found at libne no:%d\n",i); t=1; }if(t==0)printf("Labelnot found.."); }
Result:
Thus a C program to implement Symbol Table
2.IMPLENTATION OF PASS ONE OF A PASS TWO ASSEMBLER
Aim:
To write a C program to implement PASS ONE of a two pass assembler.
Algorithm: Step1: Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode Step2: Read the source program Step3: If the opcode read in the source program is START, the variable location counter isinitialized with the operand value. Else the location counter is initialized to 0. Step4: The source program is read line by line until the reach of opcode END. Step5: Check whether the opcode read is present in the operation code table. Step6: If the opcode is present, then the location counter is incremented by 3. Step7: If the opcode read is WORD, the location counter is incremented by3. If the opcode read is RESW, the operand value is multiplied by 3 and then the location counter is incremented. If the opcode read is RESB, the location counter value is incremented by operand value. If the opcode read is BYTE, the location counter is auto incremented. The length of the source program is found using the location counter value. source Program: #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct symb { char lbl[100]; int addr,addr1; }st[100]; int k=0; FILE *f1,*f2,*f3; void main() { char lbl[100],opc[100],opr[100]; int addr,i,addr1; clrscr(); j: f1=fopen("pgm.txt","W"); printf("Enter the Assembly Code:"); while(1) { scanf("%s",lbl); scanf("%s",opc); scanf("%s",opr); if(stricmp(lbl,"null")==0) strcpy(lbl,"--"); if(stricmp(opr,"null")==0); strcpy(opr,"--"); fprintf(f1,"%s\t%s\t%s\n",lbl,opc,opr); if(stricmp(opc,"end")==0) { break; } }fclose(f1); fopen("pgm.txt","r"); fscanf(f1,"%s\t%s\t%s\n",lbl,opc,opr); f2 = fopen("symb.txt","W"); f3=fopen("passone.txt","W"); if(stricmp(opc,"start")==0) addr=atoi(opr); else addr =0; fprintf(f3,"%d\t%s\t%s\t%s\n",addr,lbl,opc,opr); addr1=addr; while(!feof(f1)) {fscanf(f1,"%s\t%s\t%s\n",lbl,opc,opr); fprintf(f3,"%d\t%s\t%s\t%s\n",addr,lbl,opc,opr); if(stricmp(lbl,"--")!=0) { for(i=0;i<k;i++) { if(stricmp(st[i].lbl,lbl)==0) { printf("lable Duplication Error\n"); goto j; } }strcpy(st[k].lbl,lbl); st[k].addr=addr; k++; }if(stricmp(opc,"resb")==0) addr+=atoi(opr); else if(stricmp(opc,"resw")==0) addr+=(3*(atoi(opr))); else if(stricmp(opc,"byte")==0) addr+=((strlen(opc))-3); else addr+=3; }for(i=0;i<k;i++) {fprintf(f2,"%s\t%d\n",st[i].lbl,st[i].addr); }fclose(f2); fclose(f3); f2=fopen("symb.txt","r"); f3=fopen("passone.txt","r"); printf("Symbol table:\n\n"); while(!feof(f2)) {fscanf(f2,"%s\t%d\n",lbl,&addr); printf("%s\t%d\n",lbl,addr); }printf("intermediate file:\n\n"); while(!feof(f3)) {fscanf(f3,"%d\t%s\t%s\t%s\n",&addr,lbl,opc,opr); printf("%d\t5s\t$s\t%s\n",addr,lbl,opc,opr); }printf("%x(HEX)",(addr-addr1)); getch(); }