Professional Documents
Culture Documents
Symbol Table Creation: CAPE INSTITUTE OF TECHNOLOGY-Department of IT - System Software Lab (CS 57)
Symbol Table Creation: CAPE INSTITUTE OF TECHNOLOGY-Department of IT - System Software Lab (CS 57)
Symbol Table Creation: CAPE INSTITUTE OF TECHNOLOGY-Department of IT - System Software Lab (CS 57)
NO : DATE :
Aim:
Algorithm:
1) Start a new program. 2) Declare all the required variables. 3) Get the input expression in an array.
4) Check the array using, if condition to see whether it has any keywords,
constants, identifiers, operators or special characters.
6) Keywords are stored in an array initially and the keyword in the input
expression can be checked by comparing the strings using strcmp() function.
Page37
PROGRAM: (C Implementation)
/*SYMBOL TABLE CREATION */ #include<stdio.h> #include<conio.h> #include<string.h> #include<ctype.h> void main() { char in[50],dig[50],id[50]; int i=0,j=0,k,l=0; clrscr(); printf("Enter the Expression:\t"); gets(in); printf("\n*************************************************************************"); printf("\nKeyword\tIdentifier\tConstants\tOperators\tSpecialCharacters\n"); printf("\n*************************************************************************"); while(in[i]!='\0') { if(isalpha(in[i])) { j=0; while((isalpha(in[i]))||(isdigit(in[i]))) { id[j]=in[i]; i++; j++; } id[j]='\0'; if(strcmp(id,"char")==0||strcmp(id,"int")==0||strcmp(id,"float")==0||strcmp(id,"if" )==0||strcmp(id,"then")==0||strcmp(id,"while")==0||strcmp(id,"do")==0|| strcmp(id,"for")==0||strcmp(id,"switch")==0||strcmp(id,"case")==0) { printf("\n"); for(l=0;l<j;l++) printf("%c",id[l]); } else { printf("\t"); for(l=0;l<j;l++) printf("%c",id[l]); } } else if(isdigit(in[i]))
Page37
{ k=0; while(isdigit(in[i])) { dig[k]=in[i]; i++; k++; } printf("\n\t\t\t"); for(l=0;l<k;l++) printf("%c",dig[l]); } else if(in[i]=='+'||in[i]=='-'||in[i]=='*'||in[i]=='/'||in[i]=='<'||in[i]=='>'||in[i]=='=') { printf("\t\t\t\t\t%c",in[i]); i++; } else if(in[i]==';'||in[i]==':'||in[i]=='.'||in[i]=='('||in[i]==')'||in[i]=='{'||in[i]=='}') { printf("\t\t\t\t\t\t\t%c",in[i]); i++; } else i++; printf("\n"); printf("-------------------------------------------------------------------------"); getch(); } }
Page37
Result:
Page37
EX.NO : DATE :
Aim:
Algorithm:
1) Start a new program. 2) Declare all the required variables. 3) Design a menu to create a symbol table and perform operations such as insert,
modify, search and display.
4) Create a symbol table with fields as LABEL, OPCODE, OPERAND and value
(Address) using create option.
5) Entries are added to the symbol table while it is created itself. 6) Append new contents to the table with the constraint that there are no null and
duplicate labels.
7) Modify and search the labels using the respective options. 8) Use display option to display the contents of the table. 9) End the program. 10) Compile and run the program.
Page37
PROGRAM: (C Implementation)
/*SYMBOL TABLE - FUNCTIONS */
#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void create(void); void insert(void); void search1(void); void modify(void); void display_table(void); void display_source(void); typedef struct { int value; char label[100]; char opcode[100]; char operand[100]; }symtab; char lab[100],opc[100],ope[100],null[10],mod[10],search[100]; int val,add,i=0,j,k,l,m,g,choice,cont,found,op; symtab s[100]; void main() { do { clrscr(); printf("\n "); printf("\n\t\t\tSYMBOL TABLE IMPLEMENTATION"); printf("\n1.Create symbol table"); printf("\n2.Insert a source statement"); printf("\n3.Search a label"); printf("\n4.Modify a source statement"); printf("\n5.Display the symbol table"); printf("\n6.Display the source statements"); printf("\n\nEnter your choice:\t"); scanf("%d",&choice); switch(choice) { case 1: create();
Page37
break; case 2: insert(); break; case 3: search1(); break; case 4: modify(); break; case 5: display_table(); break; case 6: display_source(); break; default: printf("\nNOT A VALID CHOICE"); } printf("\n Do you want to continue (0/1):\t"); scanf("%d",&cont); }while(cont==1); getch(); } void create() { null[0]='\0'; printf("\nEnter the source statements and enter '$' to finish"); A:printf("\nLABEL\tOPCODE\tOPERAND\n"); while(1) { scanf("%s",lab); if(lab[0]=='$') { printf("\nEntering of source statements terminated\n"); printf("No of source statements entered=%d\n",i); break; } else if(lab[0]!='-') strcpy(s[i].label,lab); else { strcpy(s[i].label,null); } scanf("%s",opc); strcpy(s[i].opcode,opc);
Page37
if(i>0) { scanf("%s",ope); strcpy(s[i].operand,ope); } if(i==0) { scanf("%x",&op); s[0].value=s[1].value=val=op; } m=i; for(j=0;j<m;j++) { if(strcmp(s[j].label,null)!=0) { if(strcmp(s[j].label,s[m].label)==0) { printf("\nLABEL ALREADY EXISTS.ENTER UNIQUE LABELS\n"); goto A; } } } i++; val=val+3; s[i+1].value=val; } } void insert() { k=i; printf("\nInsert the source statements and enter '$' to finish"); C:printf("\nLABEL\tOPCODE\tOPERAND\n"); while(1) { scanf("%s",lab); if(lab[0]=='$') { printf("\nEntering of source statements terminated\n"); printf("Total no of source statements =%d\n",k); break; } if(lab[0]!='-') strcpy(s[k].label,lab); else { strcpy(s[k].label,null); }
Page37
scanf("%s",opc); strcpy(s[k].opcode,opc); scanf("%s",ope); strcpy(s[k].operand,ope); m=k; for(j=0;j<m;j++) { if(strcmp(s[j].label,null)!=0) { if(strcmp(s[j].label,s[m].label)==0) { printf("\nLABEL ALREADY EXISTS.ENTER UNIQUE LABELS\n"); goto C; } } } k++; val=val+3; s[k+1].value=val; } i=k; } void search1() { printf("\nEnter the label to search:"); scanf("%s",search); found=0; for(j=0;j<i;j++) { if(strcmp(s[j].label,search)==0) { printf("The label is found in statement %d.\n",j+1); printf("The label address is: %x\n",s[j].value); found++; } } if(found==0) printf("The label is not found.\n"); } void modify() { printf("\n There are %d lines in the source program.",i); printf("\n Enter the line to be modified:\t"); scanf("%d",&l); printf("\n The label present in the line is: %s",s[l-1].label); B:printf("\nEnter the new label:\t");
Page37
scanf("%s",mod); for(j=0;j<i;j++) { if(strcmp(s[j].label,null)!=0) { if (strcmp(s[j].label,mod)==0) { printf("\nLABEL ALREADY EXISTS.ENTER UNIQUE LABELS\n"); goto B; } } } if(mod[0]!='-') { s[l-1].label[0]='\0'; strcpy(s[l-1].label,mod); } else { s[l-1].label[0]='\0'; strcpy(s[l-1].label,null); } printf("\n New label accepted.\n"); } void display_table() { printf("\nSYMBOL TABLE:\n"); printf("LABEL\tVALUE\n"); for(j=0;j<i;j++) { if(s[j].label[0]!=NULL) { printf("%s",s[j].label);printf("\t"); printf("%x",s[j].value); printf("\n"); } } } void display_source() { printf("\nSOURCE STATEMENTS:\n"); printf("LOC\tLABEL\tOPCODE\tOPERAND\n"); printf("%x",s[0].value);printf("\t"); printf("%s",s[0].label);printf("\t"); printf("%s",s[0].opcode);printf("\t");
Page37
Result:
Page37
EX.NO : DATE :
Aim:
Algorithm:
1) Start the program. 2) Declare all the required variables. 3) Get the number of lines in the source program from the user and store it in a variable. 4) Enter the source program and store it in arrays until the end of the source program. 5) Search for symbols in the Symbol Table .If the particular symbol is not present insert it. 6) Search for opcodes in the Opcode Table.
7) If it is a word then, add 3 to the location counter. 8) If it is a RESW then, add 3*#(operand). 9) If it is a RESB then, add the #(operand) to the location counter. 10) If it is a BYTE then, add the length of the constant. 11) Print the symbol its value and address in the symbol table.
12) Stop the program. 13) Compile & run it.
Page37
PROGRAM: (C Implementation)
/* Pass1 of two pass assembler */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char loc[25][25],stmt[25][25],sym[25][25]; int i,n; clrscr(); printf("\nEnter the no of lines in SIC program: \t"); scanf("%d",&n); printf("\nEnter the SIC program(Location Source Statement): \n"); printf("-------------------------------------------------\n"); for(i=0;i<n;i++) scanf("\n%s%s%s",loc[i],stmt[i],sym[i]); printf("\n\n\n\nSYMBOL TABLE"); printf("\n------------"); printf("\n\nSymbol\tValue\n"); printf("-------------"); for(i=0;i<n;i++) { if(strcmp(stmt[i],"LDA")!=0&&strcmp(stmt[i],"LDX")!=0&&strcmp(stmt[i], "TIX")!=0&&strcmp(stmt[i],"LDCH")!=0&&strcmp(stmt[i],"STCH")!=0&&strcmp (stmt[i],"STL")!=0&&strcmp(stmt[i],"STA")!=0) printf("\n%s\t%s",stmt[i],loc[i]); } getch(); }
Page37
Result:
EX.NO : DATE :
Aim:
CAPE INSTITUTE OF TECHNOLOGY- Department of IT - System Software Lab (CS 57)
Page37
Algorithm:
1) Start the program. 2) Declare all the required variables.
3) Get the number of lines in SYMTAB, OPTAB and the SIC program from the user
and store it in a variable.
4) Enter the symbols, values, instruction, opcode and SIC program and store it in
arrays until the end of the source program.
5) Compare the symbols with symbol table and find its value using strcmp(). 6)
7) Compare instructions with opcode table and assign opcodes using strcmp(). Concatenate the opcode with values using strcat(). 8) Display the Object code. 9) Stop the program. 10) Compile & run it.
PROGRAM: (C Implementation)
/* PASS2 OF TWO PASS ASSEMBLER */ #include<stdio.h>
Page37
#include<conio.h> #include<string.h> void main() { char a[100][100],a1[100][100],a2[100][100],a3[100][100],a4[100][100], a5[100][100],a6[100][100],a7[100][100]; int m,n,i,b,j; clrscr(); printf("\n\nEnter the no. of lines in SYMTAB:\t"); scanf("%d",&b); printf("\nEnter the Symbols and Value:\n"); printf("---------------------------\n"); for(i=0;i<b;i++) scanf("%s%s",a[i],a1[i]); printf("\n\nEnter the no. of lines in OPTAB:\t"); scanf("%d",&n); printf("\nEnter the Instruction and Opcode:\n"); printf("--------------------------------\n"); for(i=0;i<n;i++) scanf("%s%s",a2[i],a3[i]); printf("\n\nEnter the no. of lines in the program:\t"); scanf("%d",&m); printf("\nEnter the SIC program:\n"); printf("---------------------\n"); for(i=0;i<m;i++) scanf("%s%s%s",a4[i],a5[i],a6[i]); printf("\n-----------------------------------\n"); printf("\t SIC PROGRAM\tOBJECT CODE\n"); printf("----------------------------------"); for(i=0;i<m;i++) { for(j=0;j<n;j++) if(strcmp(a5[i],a2[j])==0) strcpy(a7[i],a3[j]); for(j=0;j<b;j++) if(strcmp(a[j],a6[i])==0) strcat(a7[i],a1[j]); printf("\n%s\t%s\t%s\t%s",a4[i],a5[i],a6[i],a7[i]); } getch(); }
Result:
Page37
EX.NO : DATE :
Aim:
Algorithm:
1) Start the program.
2) Get the number of lines in the source program from the user and store it in a
variable. 3) Enter the source program and store it in arrays, until the end of the source program. 4) Get the number of instructions and assign the opcodes for instructions. 5) Compare the symbols with symbol table and find its value using strcmp(). 6) 7) 8) 9) Compare instructions with opcode table and assign opcodes using strcmp(). Concatenate the opcode with values using strcat(). Search the opcode table and assign the opcodes for the instructions. Search the symbol table and assign the values for corresponding symbols. 10) Display the Object code. 11) Stop the program. 12) Compile & run it.
Page37
PROGRAM: (C Implementation)
/* SINGLE PASS ASSEMBLER */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char add[100][100],sym[100][100],oper[100][100],obj[100][100], symt[100][100],symv[100][100],opert[100][100],operv[100][100]; int i,j,m,n,b=0; clrscr(); printf("\nEnter the no of lines in the SIC program:\t"); scanf("%d",&m); printf("\n\n\nEnter the SIC Program:\n"); printf("---------------------\n"); for(i=0;i<m;i++) scanf("%s%s%s",add[i],oper[i],sym[i]); for(i=0;i<m;i++) { if(strcmp(oper[i],"LDA")!=0&&strcmp(oper[i],"LDX")!=0&&strcmp(oper[i],"TI X")!=0&&strcmp(oper[i],"STL")!=0) { strcpy(symt[b],oper[i]); strcpy(symv[b],add[i]); b=b+1; } } printf("\n\nEnter the no of instructions:\t"); scanf("%d",&n); printf("\n\n\nEnter the OPCODE TABLE:\n"); printf("-----------------------\n"); for(i=0;i<n;i++) scanf("%s%s",opert[i],operv[i]); printf("\n\n\n\n\tSIC program \tOBJECT CODE\n"); printf("-----------------------------------"); for(i=0;i<m;i++) { if(strcmp(oper[i],"LDA")==0||strcmp(oper[i],"LDX")==0||strcmp(oper[i],"TIX") ==0||strcmp(oper[i],"STL")==0) { for(j=0;j<n;j++) if(strcmp(oper[i],opert[j])==0) strcpy(obj[i],operv[j]); for(j=0;j<b;j++) if(strcmp(sym[i],symt[j])==0) strcat(obj[i],symv[j]);
Page37
Page37
Result:
Page37
EX.NO : DATE :
ABSOLUTE LOADER
Aim:
Algorithm:
1) Start the program. 2) Declare all the required variables. 3) Get the number of lines in the source program from the user and store it in a variable. 4) Enter the source program and store it in arrays until the end of the source program.
5) Check the source program for text records. If the text record T is met, then
print its address and memory content separately. 6) Stop the program. 7) Compile and run it.
Page37
PROGRAM: (C Implementation)
/* ABSOLUTE LOADER */
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[25][25],a1[25][25],a2[25][25],a3[25][25],a4[25][25],a5[25][25]; int b,i; clrscr(); printf("\n\n-----------ABSOLUTE LOADER--------------"); printf("\nEnter the number of lines in the program:\t"); scanf("%d",&b); printf("\nEnter the program:\n"); printf("-----------------\n"); for(i=0;i<b;i++) scanf("%s%s%s%s%s%s",a[i],a1[i],a2[i],a3[i],a4[i],a5[i]); printf("\nAddress\tMemory Content\n"); printf("----------------------"); for(i=0;i<b;i++) { if(strcmp(a[i],"T")==0) { printf("\n%s\t%s%s%s",a1[i],a3[i],a4[i],a5[i]); } } getch(); }
Page37
Result:
Page37
EX.NO : DATE :
BOOTSTRAP LOADER
Aim:
Algorithm:
1) Start the program. 2) Declare all the required variables.
3) Get the number of lines of bytes in the object program from the user and store
it in a variable.
4) Enter the OS object code and store it in arrays until the end of the object code. 5) Initialize the starting address to 80(16).
6) Display the address and their contents until a byte 04 is reached. 7) Stop the program.
Page37
PROGRAM: (C Implementation)
/* BOOTSTRAP LOADER */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { int a[100]; int b,i,address=80; clrscr(); printf("\nEnter the no of bytes in the program:\t"); scanf("%d",&b); printf("\n\nEnter the OS Object code:\n"); for(i=0;i<b;i++) { scanf("%d",&a[i]); } printf("-----------------------"); printf("\nAddress\t\tContent\n"); printf("-----------------------"); for(i=0;a[i]!=04;i++) { printf("\n%d\t\t%d",address,a[i]); address++; } getch(); }
Page37
Result:
Page37
EX.NO : DATE :
Aim:
Algorithm:
1. Start the program. 2. Read the address in which the program is to be loaded. 3. Check the record type. If it is a header record print the control section name and address in which it starts. 4. If it is a define record, print the symbol name and the corresponding address added with the control section address. 5. If the record is end, add the base address and the control section length. 6. The added value is set as control section address. 7. Continue the process till the end of file is reached. 8. Stop the program. 9. Compile and run it.
Page37
PROGRAM: (C Implementation)
/* PASS1 OF DIRECT LINKING LOADER */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[25][25],a1[25][25],a2[25][25],a3[25][25],a4[25][25]; int b,i; clrscr(); printf("\nEnter the no of Lines in the Object program:\t"); scanf("%d",&b); printf("\n\nEnter the Object program:\n"); printf("------------------------\n"); for(i=0;i<b;i++) scanf("%s%s%s%s%s",a[i],a1[i],a2[i],a3[i],a4[i]); printf("\n\n---------------------\n"); printf("EXTERNAL SYMBOL TABLE"); printf("\n---------------------"); for(i=0;i<b;i++) if(strcmp(a[i],"D")==0) { printf("\n%s\t%s\t\n%s\t%s",a1[i],a2[i],a3[i],a4[i]); } getch(); }
Page37
Page37
Result:
EX.NO : DATE :
MACROPROCESSOR
Aim:
Algorithm:
1) Start the program. 2) Declare all the required variables globally. 3) Declare the methods named macrodefine() and macroprocessor(). 4) Get the number of choice from the user as 1, 2 or 3 and with in switch-case statements call the corresponding methods for processing. 5) If it is a third choice that is Exit, then exit from the loop. 6) Within the macrodefine() method get the macro name, number of lines in the macro and the definition of macro until the number of lines mentioned. 7) Within the macroprocessor() method get the source program from the user and if there is a word with the macro name given, replace it with the corresponding macro definition. 8) Print the source program with the macro definition as an output. 9) Stop the program. 10) Compile and run it.
Page37
CAPE INSTITUTE OF TECHNOLOGY- Department of IT - System Software Lab (CS 57)
PROGRAM: (C Implementation)
/* Macroprocessor */
#include<stdio.h> #include<conio.h> #include<string.h> char namtab[100],deftab[100][100],pgm[100][100]; int len,i,j; void main() { int ch; void macrodefine(); void macroprocessor(); clrscr(); do { printf("\n******* Macro Processor *******\n"); printf("\n\t1.Macro Define"); printf("\n\t2.Macro Processor"); printf("\n\tEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1:macrodefine(); break; case 2:macroprocessor(); break; case 3:exit(0); break; } }while(ch<=3); getch(); } void macrodefine() { printf("\n\nEnter the Macro name:\t"); scanf("%s",namtab); printf("\n\nEnter the no.of lines in the Macro %s:\t",namtab); scanf("%d",&len); printf("\n\nEnter the statements in the Macro %s:\n",namtab); printf("-------------------------------------\n"); for(i=0;i<len;i++) { scanf("%s",deftab[i]); }
Page37
} void macroprocessor() { int n; printf("\n\nEnter the number of lines in the SIC program:\t"); scanf("%d",&n); printf("\n\nEnter the SIC program:\n"); printf("---------------------\n"); for(i=0;i<n;i++) { scanf("%s",pgm[i]); } printf("\n-------Output--------"); for(j=0;j<n;j++) { if(strcmp(pgm[j],namtab)==0) { printf("\n%s\n",namtab); for(i=0;i<len;i++) { printf("\n%s\n",deftab[i]); } } else printf("\n%s\n",pgm[j]); } }
Page37
Result:
Page37
CAPE INSTITUTE OF TECHNOLOGY- Department of IT - System Software Lab (CS 57)
EX.NO : DATE :
TEXT EDITOR
Aim:
Algorithm:
1) Start the program. 2) With in the doWhile and switch-case statements create menu for read method, write method and exit method.
5) If the user choice is 2, then call the read () method, which will read the
contents in the already created file.
/*
TEXT EDITOR
*/
Page37
PROGRAM: (C Implementation)
#include<stdio.h> #include<conio.h> void main() { void read(); void write(); int ch; do { clrscr(); printf("\n**** TEXT EDITOR ****\n"); printf("\n-------- MENU -------\n\n"); printf("\n\t1.Write"); printf("\n\t2.Read"); printf("\n\t3.Exit"); printf("\n\nEnter your choice:\t"); scanf("%d",&ch); switch(ch) { case 1:write(); break; case 2:read(); break; case 3:exit(0); break; } }while(ch<=3); getch(); } void read() { char e[50]; int n; FILE *h1; clrscr(); h1=fopen("h.dat","r"); printf("\n\nThe String in the file is :\n"); while(!feof(h1)) { fscanf(h1,"%s",&e); printf("%s\n",e); } fclose(h1); getch(); }
Page37
void write() { char e[50]; int n,i; FILE *h1; clrscr(); h1=fopen("h.dat","w"); printf("\n\n\n Enter the number of strings:\t"); scanf("%d",&n); printf("\n\n Enter the strings:\t"); for(i=0;i<n;i++) { scanf("%s",e); fprintf(h1,"\n%s",e); } fclose(h1); }
Page37
Result:
Page37