SYSTEM SOFTWARE LABORATORY LIST OF EXPERIMENTS 1. Design of an Editor: Design of a Line or Screen Editor using C Language. 2. Design of an Assembler. 3. Simulation of Loaders. 4. Interprocess Communication. 5. Token Separation and Symbol Table Manipulation. 6. Construction of Parsing Table. 2
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SYSTEM SOFTWARE LABORATORY
LIST OF EXPERIMENTS
1. Design of an Editor: Design of a Line or Screen Editor using C Language.
2. Design of an Assembler.
3. Simulation of Loaders.
4. Interprocess Communication.
5. Token Separation and Symbol Table Manipulation.
6. Construction of Parsing Table.
2
INDEX
EXP.NO.
NAME OF EXPERIMENTPAGE
NO.
1 TEXT EDITOR 03
ASSEMBLER
2a ONE PASS ASSEMBLER 10
2b PASS ONE OF A TWO PASS ASSEMBLER 16
2c PASS TWO OF A TWO PASS ASSEMBLER 20
LOADER
3a RELOCATION LOADER 25
3b ABSOLUTE LOADER 30
3c PASS ONE OF A DIRECT LINKING LOADER 34
3d PASS TWO OF A DIRECT LINKING LOADER 39
INTERPROCESS COMMUNICATION
4a ONE PASS MACROPROCESSOR 45
4b TWO PASS MACROPROCESSOR 49
TOKENS AND SYMBOL TABLE
5a TOKEN SEPERATION 55
5b SYMBOL TABLE MANIPULATION 60
6 SHIFT REDUCE PARSING TABLE CONSTRUCTION 66
3
EX.NO-1 TEXT EDITOR
AIM:
To write a C program to create a text editor.
ALGORITHM:
STEP 1: Provide blank screen for the user to type the document
STEP 2: Instruct the user to enter the text using the editor
STEP 3: Display the entered character on the screen
STEP 4: Characters displayed on the screen are stored in a character array
STEP 5: Specify if the content has to be saved or not.
STEP 6: If SAVE option is selected
STEP 6a: Get the data file name from the user
STEP 6b: Open the file in write mode
STEP 6c: Move the content of the character array to the file and close the file
STEP 7: If OPEN option is selected
STEP 7a: Open the data file in the read mode
STEP 7b: Read one character at a time from the file and move it to the
character array
STEP 7c: Repeat the above steps until the end of file is reached
STEP 8: Display the characters from the character array on the screen
STEP 9: Finally close the text editor and exit.
4
PROGRAM:
#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>FILE *fp1;char buff[2000],filename[12];int cnt;void newfile(){char c,ch;clrscr();printf("\n *************************");printf("\n NEW FILE CREATION");printf("\n *************************");printf("\n TYPE TEXT \n TO TERMINATE PRESS TAB+ENTER KEYS");cnt=0;while((c=getchar())!='\t')buff[cnt++]=c;buff[cnt]='\0';printf("\n *****WARNING*****");printf("\n\t FILE IS NOT SAVED,DO YOU WANT TO SAVE?(Y/N)");ch=getch();if(ch=='Y'||ch=='y'){printf("\n ENTER THE NAME FOR FILE:");loop:scanf("%s",filename);fp1=fopen(filename,"r");if(fp1==NULL){fp1=fopen(filename,"w");fprintf(fp1,"%s",buff);}else{printf("\n FILE ALREADY EXISTS,DO YOU WANT TO OVERWRITE?(y/n)");ch=getch();if(ch=='Y'||ch=='y'){fp1=fopen(filename,"w");fprintf(fp1,"%s",buff);}else
5
{printf("\n ENTER THE NEW FILE NAME:");goto loop;}}fclose(fp1);}}void openfile(){char c;clrscr();printf("\n ***********************");printf("\n OPEN AND DISPLAY FILE");printf("\n ***********************");printf("\n ENTER THE FILENAME:");loop:scanf("%s",filename);fp1=fopen(filename,"r");if(fp1==NULL){printf("\n FILE DOES NOT EXISTS");printf("\n ENTER THE CORRECT FILENAME:");goto loop;}else{printf("\n********FILE CONTENT******** \n");while(!feof(fp1)){c=getc(fp1);putchar(c);}getch();}fclose(fp1);}void main(){char ch;while(1){clrscr();printf("\n *******************");printf("\n TEXT EDITOR ");printf("\n ********************");
6
printf("\n\tNEW(N)\t\tOPEN(O)\t\tEXIT(E)");printf("\n\n ENTER THE OPTION YOU NEED TO PERFORM:\n");ch=getch();switch(ch){case'N':case'n':newfile();break;case'O':case'o':openfile();break;case'E':case'e':exit(1);break;}}}
7
OUTPUT:
******************* TEXT EDITOR ******************** NEW(N) OPEN(O) EXIT(E)
ENTER THE OPTION YOU NEED TO PERFORM: N
************************* NEW FILE CREATION ************************* TYPE TEXT TO TERMINATE PRESS TAB+ENTER KEYSHITHIS IS A SAMPLE FILE.
*****WARNING***** FILE IS NOT SAVED,DO YOU WANT TO SAVE?(Y/N): yENTER THE NAME FOR FILE:samp
******************* TEXT EDITOR ******************** NEW(N) OPEN(O) EXIT(E)
ENTER THE OPTION YOU NEED TO PERFORM: O
*********************** OPEN AND DISPLAY FILE *********************** ENTER THE FILE NAME: samp********FILE CONTENT********HITHIS IS A SAMPLE FILE. ******************* TEXT EDITOR ******************** NEW(N) OPEN(O) EXIT(E)
8
ENTER THE OPTION YOU NEED TO PERFORM: E
RESULT:
Thus the program to create a text editor was executed successfully.
9
EX.NO-2a ONE PASS ASSEMBLER
AIM:
To write a C program to implement a one pass assembler.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Initialize all the variables.
STEP 3: Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode.
STEP 4: Read the source program.
STEP 5: If the op code read in the source program is START, the line is written to the
output file.
STEP 6: The source program is read line by line until the reach of opcode END.
STEP 7: Check whether the op code read is present in the operation code table.
STEP 8: If the op code is present, then the address is incremented by 3 and if the
operand’s address is known then load it.
STEP 9: Else maintain the address in the linked list for which the operand is required.
STEP 10: If the op code read is WORD, the location counter is incremented by 3.
STEP 11: If the op code read is RESW, the operand value is multiplied by 3 and then the
location counter is incremented.
STEP 12: If the op code read is RESB, the location counter value is incremented by
operand value.
STEP 13: If the op code read is BYTE, the location counter is auto incremented.
STEP 14: Write the object code with address to the output file.
STEP 15: Finally terminate the one pass assembler.
10
PROGRAM:
#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#define MAX 20
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(“\n Length of the program is %d”,length);fclose(fp1);fclose(fp2);fclose(fp3);fclose(fp4);getch();}
18
INPUT FILES:
“INPUT.DAT”
MAIN START 5000BEGIN LDA NUM1** STA NUM2** STCH CHAR1** STCH CHAR2NUM1 WORD 5NUM2 RESW 1CHAR1 BYTE 5CHAR2 RESB 1** END BEGIN
“SYMBOL.DAT”
BEGIN 5000NUM1 5012NUM2 5015CHAR1 5018CHAR2 5019
“OUT.DAT”
MAIN START 5000BEGIN LDA NUM1** STA NUM2** STCH CHAR1** STCH CHAR2NUM1 WORD 5NUM2 RESW 1CHAR1 BYTE 5CHAR2 RESB 1
OUTPUT
Length of the program is 20
RESULT:
Thus the program to implement one pass of a two pass assembler was executed
successfully.
19
EX.NO-2c PASS TWO OF TWO PASS ASSEMBLER
AIM:
To write a C program to implement PASS TWO of a two pass assembler.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Initialize all the variables.
STEP 3: Open the output file of pass one of two pass assembler.
STEP 4: Read the content of the file.
STEP 5: If the opcode is START assign the program address as specified in it.
STEP 6: Read the next line of the program.
STEP 7: According to the opcode execute the corresponding instruction and refer
SYMBOL TABLE for the address of the operand to write to the file.
STEP 8: If op code is BYTE then write address, label, opcode, operand into a file
STEP 9: Else if op code is WORD then write address, label, op code, operand into a file
STEP 10: Else if op code is “RESB” or “RESW” write address, label, op code,
operand into a file.
STEP 11: If it is not match anything then write address, label, op code, operand into a file
STEP 12: Finally terminate pass two of two pass assembler.
** START 2000 2000 ** LDA FIVE 2003 ** STA ALPHA 2006 ** LDCH CHARZ 2009 ** STCH C12012 ALPHA RESW 12015 FIVE WORD 52018 CHARZ BYTE C’Z’
23
2019 C1 RESB 12020 ** END **“SYMTAB.DAT”
ALPHA 2012FIVE 2015CHARZ 2018C1 2021
OUTPUT FILE:
“TWOOUT.DAT”
** START 20002000 ** LDA FIVE 0020152003 ** STA ALPHA 0C20122006 ** LDCH CHARZ 5020182009 ** STCH C1 5420212012 ALPHA RESW 12015 FIVE WORD 5 52018 CHARZ BYTE C’Z’ 5a2019 C1 RESB 1
** END **
RESULT:
Thus the program to implement two pass of a two pass assembler was executed
successfully.
24
EX.NO-3a RELOCATION LOADER
AIM:
To write a C program to implement relocation loader.
ALGORITHM:
STEP 1: Start the program
STEP 2: Include the necessary header file and variable
STEP 3: Open the two file for read and write.
STEP 4: Read the content
STEP 5: Using while loop perform the loop until character is not equal to E
STEP 6: If the character is H, Get the variable add, length, and input
STEP 7: Else if the character is T,
STEP 7a: Get the variable address and bitmask and perform for loop for
starting zero to up to length.
STEP 7b: Get the op code ,address and assign relocation bit to bitmask
STEP 8: If relocation bit is zero Then Assign actual address to address; else Add the
address and star value.
STEP 9: Finally terminate the program.
25
PROGRAM:
#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>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(“rin.dat”,”r”);fp2=fopen(“rout.dat”,”w”);printf(“Enter the location where the program has to be loaded:”);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]);
Thus the program to implement absolute loader was executed successfully.
EX.NO-3c PASS ONE OF A DIRECT LINKING LOADER
AIM:
To write a C program to implement pass one of a direct linking loader.
ALGORITHM:
STEP 1: Enter the location where the program has to be loaded.
STEP 2: Assign the address got from the user as the first control section address.
STEP 3: Read the header record of the control Section.
STEP 4: From the details of the header read store the control section length in variable.
STEP 5: Enter the control Section name with its address into the external symbol table.
STEP 6: For each symbol in the subsequent ‘D’ records the symbol must be entered into
the symbol table along with its address, added along with the corresponding
control section address until an end record is reached.
STEP 7: Assign the starting address of next control section as the address of the current
control section plus the length of the control section.
STEP 8: Repeat the process from step 6 to 8 until there are no more input.
34
PROGRAM:
#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>struct estab{char csect[10];char sym_name[10];int add,len;}table[10];void main(){char in[10];int i,cnt=0,start,len,loc;FILE *fp1,*fp2;clrscr();fp1=fopen(“link1in.dat”,”r”);fp2=fopen(“link1out.dat”,”w”);printf(“\n Enter the location where the program has to be loaded”);scanf(“%x”,&start);fprintf(fp2,”CSECT\Tsymname\Taddress\Tlength\n”);rewind(fp1);while(!feof(fp1)){fscanf(fp1,”%s”,in);if(strcmp(in,”H”)==0){fscanf(fp1,”%s”,in);strcpy(table[cnt].csect,in);strcpy(table[cnt].sym_name,”**”);fscanf(fp1,”%s”,in);table[cnt].add=atoi(in)+start;fscanf(fp1,”%x”,&len);table[cnt++].len=len;fscanf(fp1,”%s”,in);}if(strcmp(in,”D”)==0){fscanf(fp1,”%s%x”,in,&loc);while(strcmp(in,”R”)!=0){
Thus the program to implement pass one of a direct linking loader was executed
successfully.
EX.NO-3d PASS TWO OF A DIRECT LINKING LOADER
AIM:
To write a C program to implement pass two of a direct linking loader.
ALGORITHM:
STEP 1: Assign the control section address in a variable, CSADR.
STEP 2: Read the Header record.
STEP 3: From the information available in the header record read, store the control
section length in a variable.
STEP 4: Do the following process until an ‘end’ record is reached
STEP 5: 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.
STEP 6: If the sequent 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 the add, or subtract the corresponding symbol address found with the
value starting at the location (CSADD plus the specified address in the
modification record).
STEP 7: Add the control section length to the current control section address to the
address of the next control section, and repeat the entire process until there is
no more input.
39
PROGRAM:
#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>struct objectcode{char byte[15];int locctr;};struct objectcode code[500];void main(){int loc,length,tlen=0,textloc,inc=0,i,j,reloc,maddr,tloc;int len,num=0,k,newval,value,addr,start,x;char input[20],newloc[20],oper[5],symbol[20],modval[20],in[20];FILE *fp1,*fp2,*fp3;clrscr();fp1=fopen(“INP3EX9.DAT”,”r”);fp2=fopen(“INP4EX9.DAT”,”r”);fp3=fopen(“OUTPUT.DAT”,”w”);printf(“\n ENTER THE LOCATION TO BE LOADED:”);scanf(“%d”,&start);fscanf(fp1,”%s”,input);while(!feof(fp1)){if(strcmp(input,”H”)==0){fscanf(fp1,”%s”,input);fscanf(fp1,”%s”,input);loc=atoi(input)+start;tloc=loc;fscanf(fp1,”%d”,&length);fscanf(fp1,”%s”,input);}if(strcmp(input,”D”)==0){do{fscanf(fp1,”%s”,input);
insert();break;case 3:modify();break;case 4:printf(“\n ENTER THE VARIABLE TO BE SEARCHED:”);scanf(“%s”,v);result=search(v,n);if(result==0)printf(“\n VARIABLE IS NOT PRESENT”);elseprintf(“\n VARIABLE IS PRESENT IN LOCATION ‘%d’ AND ITS VALUE IS ‘%d’”,result,tb[result].value);break;case 5:display();break;default:exit(0);}}}void create(){printf(“\n ENTER THE NUMBER OF ENTRIES:”);scanf(“%d”,&n);printf(“\n ENTER THE ENTRIES:”);for(i=1;i<=n;i++){scanf(“%s%d”,tb[i].var,&tb[i].value);for(j=1;j<I;j++){if(strcmp(tb[j].var,tb[i].var)==0){printf(“\n VARIABLE ALREADY EXITS \n ENTER ANOTHER VARIABLE:”);scanf(“%s%d”,tb[i].var,&tb[i].value);}}}}void insert(){if(n>=20)printf(“\n TABLE IS FULL SO CANT INSERT:”);else{n++;printf(“\n ENTER THE VARIABLE AND VALUE:”);scanf(“%s%d”,tb[n].var,&tb[n].value);for(i=1;i<n;i++)
62
{if(strcmp(tb[i].var,tb[n].var)==0){printf(“\n VARIABLE ALREADY EXITS \n ENTER ANOTHER VARIABLE:”);scanf(“%s%d”,tb[n].var,&tb[n].value);}}}}void modify(){char v[20];int r;printf(“\n ENTER THE VARIABLE TO MODIFY:”);scanf(“%s”,v);r=search(v,n);if(r==0)printf(“\n ELEMENT IS NOT PRESENT IN TABLE:”);else{printf(“\n ENTER THE NEW VARIABLE AND VALUE:”);scanf(“%s%d”,tb[i].var,&tb[i].value);for(j=1;j<=n;j++){if(j!=i){if(strcmp(tb[j].var,tb[i].var)==0){printf(“\n VARIABLE ALREADY EXITS \n ENTER ANOTHER VARIABLE:”);scanf(“%s%d”,tb[i].var,&tb[i].value);}}}}}int search(char v[],int n){int flag=0;for(i=1;i<=n;i++){if(strcmp(tb[i].var,v)==0){flag=1;break;}}if(flag==1)return I;elsereturn 0;}void display(){printf(“\n THE ELEMENTS IN THE TABLE ARE:”);
OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:1 ENTER THE NUMBER OF ENTRIES:2 ENTER THE ENTRIES:x 2 y 3
OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:2 ENTER THE VARIABLE AND VALUE: z 4
OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:5 THE ELEMENTS IN THE TABLE ARE: x 2 y 3 z 4
OPTIONS: 1.CREATION
64
2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:3 ENTER THE VARIABLE TO MODIFY:x ENTER THE NEW VARIABLE AND VALUE:w 5
OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:5 THE ELEMENTS IN THE TABLE ARE: w 5 y 3 z 4
OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:4 ENTER THE VARIABLE TO BE SEARCHED:y VARIABLE IS PRESENT IN LOCATION ‘2’ AND ITS VALUE IS ‘3’
OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:6
65
RESULT:
Thus the program for symbol table creation was executed and the output was
verified.
EX.NO-6 SHIFT REDUCE PARSING TABLE CONSTRUCTION
AIM:
To write a c program for the implementation of parsing table.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Include the needed header file.
STEP 3: Declare the variables.
STEP 4: Check whether the expression is accepted or not.
STEP 5: The input symbol passed into stack one by one.
STEP 6: This statement is changed into E.
STEP 7: Calculate into E->E*E, E->E+E the E->i performed.
STEP 8: The grammar is checked when $ symbol is occurred.
STEP 9: Then the expression grammar is accepted.
STEP 10: Print the result.
STEP 11: Stop the program.
66
PROGRAM:
#include<stdio.h>#include<conio.h>#include<string.h>#include<types.h>#include<process.h>void sta();char st[100],exp[100];int q,top=0,i,l,n,j;void main(){clrscr();st[++top]='$';i=0;printf("\n enter the input");gets(exp);l=strlen(exp);l--;printf("\n stack\tinput\t action\n");n=top;for(j=0;j<=n;j++)printf("%c",st[j]);printf("\t%s",exp);do{printf("shift");st[++top]=exp[i];l--;exp[i]='\0';
sta();if(st[top]=='i'){printf("reduce by E->i");st[top]='E';sta();}i++;
67
}while(l>0);do{if(st[top--]=='+'){printf("reduce by E->E+E");st[++top]='\0';sta();}else if(st[top--]=='*'){printf("reduce by E->E*E");