+ All Categories
Home > Documents > CS2308 System Software Lab « VECCSE2

CS2308 System Software Lab « VECCSE2

Date post: 18-Apr-2015
Category:
Upload: prema-selvam
View: 30 times
Download: 1 times
Share this document with a friend
45
7/29/12 CS2308 System Software Lab « VECCSE2 1/45 webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste… This is Google's cache of http://veccse2.wordpress.com/2010/10/04/system-software-lab/ . It is a snapshot of the page as it appeared on 11 Jul 2012 07:07:28 GMT. The current page could have changed in the meantime. Learn more T ext-only version VECCSE2 Computer Science and Engineering, Valliammai Engineering College.. CS2308 System Software Lab 04 OCT 2010 Leave a Comment by Surya Narayanan R in CSE 1. IMPLENTATION OF A SYMBOL TABLE Aim: To write a C program to implement Symbol Table Algorithm: Start the program for performing insert, display, delete, search and modify option in symbol 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 #include #include
Transcript
Page 1: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

1/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

This is Google's cache of http://veccse2.wordpress.com/2010/10/04/system-software-lab/. It is a snapshotof the page as it appeared on 11 Jul 2012 07:07:28 GMT. The current page could have changed in themeantime. Learn more

Text-only version

VECCSE2

Computer Science and Engineering, Valliammai Engineering College..

CS2308 System Software Lab

04 OCT 2010 Leave a Comment

by Surya Narayanan R in CSE

1. IMPLENTATION OF A SYMBOL TABLE

Aim:To write a C program to implement Symbol Table

Algorithm:

Start the program for performing insert, display, delete, search and modify option in symbol table

Define the structure of the Symbol TableEnter the choice for performing the operations in the symbol TableIf the entered choice is 1, search the symbol table for the symbol to be inserted. If the symbol is alreadypresent, 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 orboth can be modified.

Source Program:

#include#include#include

Page 2: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

2/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

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();

Page 3: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

3/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

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=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();

Page 4: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

4/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

create();

}void search()

{char 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..");}

OUTPUT:

Enter the program:

LABEL OPCODE OPERAND

COPY start 1000

first stl retaddrcloop jsub rdrec

$ LDA LENGTHRETADDR RESW 1

LENGTH RESW 1$ END FIRST

The assembly Language program is

0 COPY start 1000

1 first stl retaddr2 cloop jsub rdrec

3 $ LDA LENGTH4 RETADDR RESW 1

5 LENGTH RESW 16 $ END FIRST

1.Create

2.Modify3.Insert

4.Search5.Exit

Enter Your Choice1

Page 5: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

5/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

COPY 1000FIRST 1000

CLOOP 1003RETADR 1009

LENGTH 1012

Do you want to continue(Y orN):Y1.Create

2.Modify

3.Insert4.Search

5.ExitEnter Your Choice2

Enter the line number to be modified:4RETARD RESW 2

The assembly Language program is

0 COPY start 1000

1 first stl retaddr2 cloop jsub rdrec

3 $ LDA LENGTH4 RETADDR RESW 2

5 LENGTH RESW 1

6 $ END FIRST

COPY 1000FIRST 1000

CLOOP 1003RETADR 1009

LENGTH 1015Symbol table modified..

Do you want to continue(Y orN):Y

1.Create2.Modify

3.Insert4.Search

5.Exit

Enter Your Choice3Enter the line number to be inserted:3

Enter the statement:$ COMP ZERO

The assembly Language program is

0 COPY start 1000

1 first stl retaddr

Page 6: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

6/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

2 cloop jsub rdrec

3 $ COMP ZERO4 $ LDA LENGTH

5 RETADDR RESW 26 LENGTH RESW 1

7 $ END FIRST

COPY 1000

FIRST 1000CLOOP 1003

RETADR 1012LENGTH 1018

Insertion Operation performed..Do you want to continue(Y orN):Y

1.Create

2.Modify3.Insert

4.Search5.Exit

Enter Your Choice4Enter the label to be searched: Length

The label is found at line no: 6

Do you want to continue(Y orN):N

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:Open the files fp1 and fp4 in read mode and fp2 and fp3 in write modeRead the source program

If the opcode read in the source program is START, the variable location counter is initialized with theoperand value.Else the location counter is initialized to 0.The source program is read line by line until the reach of opcode END.

Check whether the opcode read is present in the operation code table.If the opcode is present, then the location counter is incremented by 3.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 locationcounter 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:

Page 7: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

7/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

#include#include#include

#includestruct 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))

Page 8: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

8/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{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);

elseaddr+=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);

Page 9: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

9/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

}

printf("%x(HEX)",(addr-addr1));getch();}

OUTPUT:

Enter the Assembly code:

Null start 1000Null lda alphaNull sub one

Null sta betaone word 1alpha resw 1beta resw 1

null end null

Symbol Table:

one 1009alpha 1012beta 1015

Intermediate file:

1000 — start 1000

1000 — lda alpha1003 — sub one1006 — sta beta

1009 one word 11012 alpha resw 11015 beta resw 11018 — end —

Program length :12(Hex)

3.IMPLEMENTATION OF PASS TWO OF A TWO PASS ASSEMBLER

Aim :To write a c program to implement pass two of a two pass assembler

Algorithm:Get the intermediate file from pass1Use the symbol table file and choose it for two pass assemblerStore all opcode’s assembler directives in a file

In the intermediate file,compare the opcode with file and get the corresponding opcode valueAppend this value with its corresponding location counter and store label,opcode,operand,object code in a

Page 10: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

10/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

new f ileCreate a file to store and generate the object program format by using the object code generated in pass1Display the new file with its contents as header,text and end records.

Source Program:#include#include#include

FILE *fptr1,*fptr2,*fptr3,*fptr4,*fp;char address[10],label[10],opcode[10],operand[10];char op[10],val[10],objcode[10];

char sym[10],adr[10];char hexeq[10],dash[10],temp[10],c;int start,end,recstart,recend;

int no,i,j,k,len,l,receln[30];int intohex(char integer[]){int n;

n=0;while(1){

n++;itoa(n,hexeq,16);if(strcmpi(integer,hexeq)==0){

return n;}}

}void recordlen(){fptr4=fopen(“c:/object.txt”,”r”);

i=0;j=0;while(!feof(fptr4))

{fscanf(fptr4,”%s%s%s%s%s”,address,label,opcode,operand,objcode);if(strcmpi(opcode,”START”)==0){

recstart=intohex(operand);}else if(strcmpi(opcode,”START”)!=0 && strcmpi(opcode,”END”)!=0)

{if(strcmp(objcode,dash)!=0){if(i==10)

Page 11: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

11/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{recend=intohex(address);receln[j]=recend-recstart;

recstart=recend;i=0;j++;

}}elsei–;

i++;}else if(strcmpi(opcode,”END”)==0)

{recend=intohex(address);receln[j]=recend-recstart;}

}fclose(fptr4);}

void main(){fptr1=fopen(“c:/intermed.txt”,”r”);fptr2=fopen(“c:/symtab.txt”,”r”);

fptr3=fopen(“c:/optab.txt”,”r”);fptr4=fopen(“c:/object.txt”,”w”);strcpy(dash,”-”);

strcpy(objcode,NULL);clrscr();while(!feof(fptr1)){

fscanf(fptr1,”%s%s%s%s”,address,label,opcode,operand);if(strcmpi(opcode,”START”)==0){

start=intohex(address);fprintf(fptr4,”\n%s\t%s\t%s\t%s\t%s”,address,label,opcode,operand,dash);continue;}

else if((strcmpi(opcode,”RESW”)==0)||(strcmpi(opcode,”RESB”))==0){fprintf(fptr4,”\n%s\t%s\t%s\t%s\t%s”,address,label,opcode,operand,dash);

continue;}else if(strcmpi(opcode,”WORD”)==0){

Page 12: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

12/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

strcpy(objcode,NULL);strcpy(temp,NULL);

no=atoi(operand);itoa(no,hexeq,16);strcat(objcode,hexeq);

len=6-strlen(objcode);for(i=0;i<len;i++){strcat(temp,"0");}strcat(temp,objcode);

strcpy(objcode,temp);strupr(objcode);}else if(strcmpi(opcode,"BYTE")==0){fp=fopen("c:/hex.txt","w");if(operand[0]=='C')

{for(i=2;i<strlen(operand)-1;i++){fprintf(fp,"%X",operand[i]);}fclose(fp);fp=fopen("c:/hex.txt","r");

fscanf(fp,"%s",hexeq);strcat(objcode,hexeq);fclose(fp);}else{for(i=0;i<strlen(operand)-1;i++)

{objcode[i-2]=operand[i];}objcode[i-2]='';}}else if(strcmpi(opcode,"END")==0)

{end=intohex(address);fprintf(fptr4,"\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,dash);break;}else

Page 13: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

13/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{while(!feof(fptr3)){fscanf(fptr3,"%s%s",op,val);if(strcmp(op,opcode)==0){

strcat(objcode,val);break;}}}while(!feof(fptr2)){

fscanf(fptr2,"%s%s",sym,adr);if(strcmp(sym,operand)==0){strcat(objcode,adr);break;}}

fprintf(fptr4,"\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,objcode);strcpy(objcode,NULL);rewind(fptr2);rewind(fptr3);}fclose(fptr1);fclose(fptr2);

fclose(fptr3);fclose(fptr4);fptr4=fopen("c:/object.txt","r");printf("\nFILE WITH OBJECT CODE\n");printf("————————\n\n");printf("LOC\tLABEL\tOPCODE\tOPERAND\tOBJCODE\n");printf("—\t—-\t——-\t———-\t———-");

while(!feof(fptr4)){fscanf(fptr4,"%s%s%s%s%s",address,label,opcode,operand,objcode);printf("\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,objcode);}fclose(fptr4);getch();

recordlen();fptr4=fopen("c:/object.txt","r");fptr1=fopen("c:/objprog.txt","w");i=0;

Page 14: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

14/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

j=0;while(!feof(fptr4))

{fscanf(fptr4,"%s%s%s%s%s",address,label,opcode,operand,objcode);if(strcmpi(opcode,"START")==0){len=end-start;itoa(len,hexeq,16);l=6-strlen(hexeq);

strcpy(temp,NULL);for(k=0;k<l;k++){strcat(temp,"0");}strcat(temp,hexeq);strcat(hexeq,temp);

strupr(hexeq);printf("\n\nprogram length=%s\n",hexeq);getch();recstart=start;fprintf(fptr1,"H%s%s%s\n",label,address,hexeq);fprintf(fptr1,"T 00%X%X",recstart,receln[j]);}

else if(strcmpi(opcode,"END")!=0){if(strcmpi(objcode,dash)!=0){if(i==10){j++;

fprintf(fptr1,"\nT");recstart=intohex(address);fprintf(fptr1,"00%X%X%s",recstart,receln[j],objcode);i=0;}else{

fprintf(fptr1,"%s",objcode);}}else if(strcmpi(objcode,dash)==0){i–;}

i++;

Page 15: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

15/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

}else if(strcmpi(opcode,"END")==0){fprintf(fptr1,"\nE 00%X",start);}

}fclose(fptr1);fclose(fptr4);fptr1=fopen("c:/objprog.txt","r");printf("\n OBJECT PROGRAM\n");printf("——————–\n\n");c=getc(fptr1);

while(c!=EOF){printf("%c",c);c=getc(fptr1);}fclose(fptr1);getch();

}

INPUT FILE:INTERMED.DAT** START 20002000 ** LDA FIVE2003 ** STA ALPHA

2006 ** LDCH CHARZ2009 ** STCH C12012 ALPHA RESW 12015 FIVE WORD 52018 CHARZ BYTE C'EOF'2019 C1 RESB 12020 ** END **

OPTAB.DATLDA 33STA 44LDCH 53STCH 57END *

SYMTAB.DATALPHA 2012FIVE 2015CHARZ 2018C1 2019

Page 16: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

16/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

LOGIN: Sumathi

OUTPUT FILE:

ASSMLIST.DAT

** START 20002000 ** LDA FIVE 332015

2003 ** STA ALPHA 4420122006 ** LDCH CHARZ 5320182009 ** STCH C1 5720192012 ALPHA RESW 12015 FIVE WORD 5 0000052018 CHARZ BYTE C'EOF' 454f462019 C1 RESB 1

2020 ** END **

4.IMPLEMENTATION OF SINGLE PASS ASSEMBLER

Aim:To implement a single pass assembler using ‘c’ languageAlgorithm:

Object code is generated in the programIf symbol is not yet defined,the operand address is omittedThe symbol is entered in the symbol table with ‘$’ pinWhen the definition is encountered, program address is inserted in a list associated with the symbolcontinue the process fill the end of program fill all the forward references

Source program:

#include#include#include#include#define MAX 10struct input{

char label[10],opcode[10],operand[10],mnem[10];int loc;}table[MAX];struct symtab{char sym[10];int f,val,ref;

}symtbl[MAX];void main(){int f1,i=1,j=1,flag,locctr,x;

Page 17: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

17/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

char add[10],code[10],mnemcode[10];FILE *fp1,*fp2,*fp3;clrscr();

fp1=fopen(“input.dat”,”r”);fp2=fopen(“optab.dat”,”r”);fp3=fopen(“spout.dat”,”w”);fscanf(fp1,”%s%s%s”,table[i].label,table[i].opcode,table[i].operand);if(strcmp(table[i].opcode,”START”)==0){locctr=atoi(table[i].operand);

i++;fscanf(fp1,”%s%s%s”,table[i].label,table[i].opcode,table[i].operand);}elselocctr=0;while(strcmp(table[i].opcode,”END”)!=0){

if(strcmp(table[i].label,”$”)!=0){for(x=1;x<j;x++){f1=0;if((strcmp(symtbl[x].sym,table[i].label)==0) && (symtbl[x].f==1)){

symtbl[x].val=locctr;symtbl[x].f=0;table[symtbl[x].ref].loc=locctr;f1=1;break;}}

if(f1==0){strcpy(symtbl[j].sym,table[i].label);symtbl[j].val=locctr;symtbl[j].f=0;j++;}

}fscanf(fp2,"%s%s",code,mnemcode);while(strcmp(code,"END")!=0){if(strcmp(table[i].opcode,code)==0){strcpy(table[i].mnem,mnemcode);

Page 18: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

18/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

locctr+=3;for(x=1;x<=j;x++);{flag=0;if(strcmp(table[i].operand,symtbl[x].sym)==0){

flag=1;if(symtbl[x].f==0)table[i].loc=symtbl[x].val;break;}}if(flag!=1)

{strcpy(symtbl[j].sym,table[i].operand);symtbl[j].f=1;symtbl[j].ref=i;j++;}

}fscanf(fp2,"%s%s",code,mnemcode);}rewind(fp2);if(strcmp(table[i].opcode,"WORD")==0){locctr+=3;

strcpy(table[i].mnem,'');table[i].loc=atoi(table[i].operand);}else if(strcmp(table[i].opcode,"RESW")==0){locctr+=(3*(atoi(table[i].operand)));strcpy(table[i].mnem,'');

table[i].loc=atoi('');}else if(strcmp(table[i].opcode,"RESB")==0){locctr+=(atoi(table[i].operand));strcpy(table[i].mnem,'');table[i].loc=atoi('');

}else if(strcmp(table[i].opcode,"BYTE")==0){++locctr;if((table[i].operand[0]=='C')||(table[i].operand[0]=='X'))table[i].loc=(int)table[i].operand[2];

Page 19: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

19/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

else

table[i].loc=locctr;}i++;fscanf(fp1,"%s%s%s",table[i].label,table[i].opcode,table[i].operand);}for(x=1;x<=i;x++)fprintf(fp3,"%s\t%s\t%s\t%s\n",table[x].label,table[x].opcode,table[x].operand,strcat(table[x].mnem,ito

a(table[x].loc,add,10)));for(x=1;x<j;x++)printf("%s\t%d\n",symtbl[x],symtbl[x].val);getch();}

INPUT FILE:

INPUT.DAT$ START 1000$ JSUB CLOOP$ JSUB DLOOPALPHA WORD 23BETA RESW 3GAMMA BYTE C'Z'

DELTA RESB 7CLOOP LDA ALPHADLOOP STA DELTA$ LDCH GAMMA$ STCH DELTA$ END $

OPTAB.DAT

START $JSUB 48LDA 14STA 03LDCH 53STCH 57

END $

LOGIN: sumathi

OUTPUT FILE:

SPOUT.DAT

$ START 1000 0$ JSUB CLOOP 481026$ JSUB DLOOP 481029

Page 20: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

20/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

ALPHA WORD 23 23BETA RESW 3 0GAMMA BYTE C’Z’ 90DELTA RESB 7 0

CLOOP LDA ALPHA 141006DLOOP STA DELTA 0$ LDCH GAMMA 531018$ STCH DELTA 571019$ END $ 0

OUTPUT:

CLOOP 1026DLOOP 1029ALPHA 1006BETA 1009GAMMA 1018DELTA 1019

5. IMPLEMENTATION OF MACRO PROCESSOR

Aim:To write a c program to implement macro processorAlgorithm:Start the macro processor programInclude the necessary header files and variable

Open the three filesf1=macin.dat with read privilegef2=macout.dat with write privilegef3= deftab.dat with write privilegeGet the variable form f1 file macin.dat for label,opcode,operandRead the variable until the opcode is not is equal to zeroThen check if the opcode is equal to Macro if Macro

ThenCopy macroname=labelGet the variable label ,opcode ,operandIn these if condition perform the while loop until opcode is not equal to MENDCopy the variabled[lines].lab=labeld[lines].opc=opcode

d[lines].oper=operandand increase lines++;close while loop and if condtionelse if opcode is equal to macro namePerform the for loop from 0 to lengthfprint for d[i].lab,d[i].opc,d[i].operelse if it is not match

Page 21: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

21/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);Finally terminate the program

Source program :#include#include#include

#includevoid main(){char n,n1,c1,i;char fn[10][10],ilab[20],iopd[20],m[20][3],oper[20],opd[20];FILE *fp1,*fp2,*p[5];clrscr();

n=0;fp1=fopen(“z:macin.dat”,”r”);while(!feof(fp1)){fscanf(fp1,”%s\t%s\t%s”,ilab,iopd,oper);if(strcmp(iopd,”macro”)==0)

n++;}printf(“no of macros=%d\n”,n);n1=n;printf(“\n enter the text filename \n”);for(i=0;i<n;i++){

scanf("%s",fn[i]);p[i]=fopen(fn[i],"w");}n=0;rewind(fp1);while(!feof(fp1)){

fscanf(fp1,"%s%s%s",ilab,iopd,oper);if(strcmp(iopd,"macro")==0){strcpy(m[n],oper);fscanf(fp1,"%s%s%s",ilab,iopd,oper);while(strcmp(iopd,"mend")!=0){

fprintf(p[n],"%s%s%s\n",ilab,iopd,oper);fscanf(fp1,"%s%s%s",ilab,iopd,oper);}fclose(p[n]);n++;

Page 22: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

22/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

}}

for(i=0;i<n1;i++)p[i]=fopen(fn[i],"r");fp2=fopen("z:outm.dat","w");rewind(fp1);fscanf(fp1,"%s%s%s",ilab,iopd,oper);while(!feof(fp1)){

if(strcmp(iopd,"call")==0){for(i=0;i<n1;i++){if(strcmp(m[i],oper)==0){rewind(p[i]);

fscanf(p[i],"%s%s%s",ilab,iopd,oper);while(!feof(p[i])){fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);c1=1;fscanf(p[i],"%s%s%s",ilab,iopd,oper);}

break;}}}if(c1!=1)fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);c1=0;

fscanf(fp1,"%s%s%s",ilab,iopd,oper);}fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);}

INPUT FILE:MACIN.DAT

macro m1move a,bmend –macro m2lda bmend –start 1000

lda acall m1

Page 23: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

23/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

call m2add a,b

OUTPUT FILE:

OUTM.DAT** macro m1** move a , b** mend –** macro m2** lda b** mend –** start 1000** lda a** move a , b

** lda b** add a , b** add a , b

M1.DAT:** move a , b

M2.DAT:** lda b

6. IMPLEMENTATION OF ABSOLUTE LOADER

Aim:To implement the Absolute Loader using ‘c’ languageAlgorithm:

Read the Loader recordVerify program name and lengthRead first text record from the input fileProcess the following steps until an end record is reachedIf Object code is in character form convert in to internal hexadecimal representationMove object codes to specified locations in memoryWrite the starting location counter value of a blocks of object code and the corresponding internalrepresentation to the output fileRead next record from the input fileGo to the address specified in end recordClose all the files and editSource program:#include#include

#include#includestruct obj_code

Page 24: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

24/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{int locctr;char byte[5];}code[200];void main(){FILE *fp1,*fp2;char input[15];int i,len,textloc,tlen,loc;int n=0,count=0,inc=0,tloc=0,num=0;clrscr();

fp1=fopen(“lout.dat”,”r”);fp2=fopen(“loadout.dat”,”w”);rewind(fp1);rewind(fp2);fscanf(fp1,”%s”,input);if(strcmp(input,”H”)==0){for(i=0;i<4;i++){if(i==1)fscanf(fp1,"%x",&loc);elsefscanf(fp2,"%s",input);}

}tloc=loc;while(strcmp(input,"E")!=0){if(strcmp(input,"T")==0){fscanf(fp1,"%x",&textloc);for(i=0;i<(textloc-(tloc+tlen));i++){strcpy(code[inc].byte,"xx");code[inc++].locctr=loc++;}fscanf(fp1,"%x",&tlen);

tloc=textloc;}else{len=strlen(input);for(i=0;i1){

Page 25: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

25/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

code[inc].locctr=loc;loc++;inc++;num=0;}}

}fscanf(fp1,”%s”,input);}n=0;i=0;count=0;fprintf(fp2,”%x\t”,code[i].locctr);for(i=0;i3){fprintf(fp2,”\t”);n=0;count++;}if(count>3)

{fprintf(fp2,”\n%x\t”,code[i+1].locctr);count=0;}}getch();}

INPUT FILE :

LOUT.DAT:

H COPy 002000 00107AT 002000 1E 142033 483039 102036 282030 302015483061 3C2003 00202A 0C2039 00202D

T 00201E 15 2C2036 483061 182033 4C0000 454F46200003 100000T 002039 1E 242030 302030 E0305D 30303F D8305D282030 303057 53A039 2C305E 38303FT 002057 A 102036 4C0000 F1 201000T 002071 19 342030 E03079 303064 4FA039 DC3079E 002000

OUTPUT FILE:

LOADOUT.DAT

Page 26: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

26/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

2000 14203348 30391020 36282030 302015482010 30613C20 0300202A 0C203900 202D2C20

2020 36483061 1820334C 0000454F 462000032030 100000XX XXXXXXXX XX242030 302030E02040 305D3030 3FD8305D 28203030 305753A02050 392C305E 38303F10 20364C00 00F120102060 00XXXXXX XXXXXXXX XXXXXXXX XXXXXXXX2070 XX342030 E0307930 30644FA0 39DC30792080 2C203638 30644C00 0015

7. IMPLEMENTATION OF RELOCATION LOADER

AimTo write a c program to Perform the Relocation LoaderAlgorithmStart the program

Include the necessary header file and variableOpen the two file forfp1= relinput.dat and give readfp2= reloutput.dat and give writeRead the contentUsing while loop perform the loop until character is not equal to Ewhile(strcmp(input,”E”)!=0)If the character is HGet the variable add, length, and inputElse if the character is TGet the variable address and bitmaskAnd perform the for loop for starting zero to up to lenGet the opcode ,addr and assign relocbit to bitmaskIf relocabit is zero

Thenactualadd=addr;elseAdd the addr and star valueFinally terminate the program

Source program:#include#include#include#includestruct obj_code{int locctr;

char add[10];}obcode[300];

Page 27: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

27/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

void main(){char input[100][16],output[100][16],binary[20],address[20],stloc[10];int len,bitmask,loc,tloc,tlen,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0;FILE *fp1,*fp2;clrscr();fp1=fopen(“linput.dat”,”r”);fp2=fopen(“loadout.dat”,”w”);printf(“\n ENTER THE LOCATION WHERE THE PROGRAM HAS TO BE LOADED:\n”);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]);}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;n<(textloc-(tloc+tlen));n++){strcpy(obcode[inc].add,"yy");obcode[inc++].locctr=location++;}tlen=atoi(output[i-1]);

Page 28: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

28/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

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]='';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;n1){obcode[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”,obcode[n].locctr);for(n=0;n<inc;n++)

Page 29: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

29/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{fprintf(fp2,"%s",obcode[n].add);i++;

if(i3){fprintf(fp2,”\n%d\t”,obcode[n+1].locctr);count=0;}}getch();}

INPUT FILE:

LINPUT.DAT:H COPY 000000 001073T 000000 10 15 140033 481039 100036 280030 300015 481061

311003200030 211033 200033T 000011 19 045 412036 481061 380033 412000 454196 100003200000T 000031 15 135 140030 430030 141013 301044 241064 210030301057543039 212064 381045T 000058 05 056 100036 520000 151T 000065 19 080 340030 141079 301064 503039 152079 220036381064430000 25E 000000

OUTPUT FILE:

LOADOUT.DAT:

1000 14103348 20391010 36281030 300015481016 10613110 03200030 21103320 0033yy411032 30364810 61381033 41300045 419610101048 03200000 yy141030 43003014 101330101004 44241004 21105050 20575440 392120041080 381045yy yyyyyyyy yyyyyyyy yyyyyy101096 10365210 00152000 301000yy yy3410301112 14107930 20645030 39152079 220036381128 10644300 0025

8. IMPLEMENTATION OF PASS1 OF THE DIRECT LINKING LOADER

Aim

Page 30: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

30/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

To write a c program to Implement pass two of direct linking loaderAlgorithm :Star the program for linking loaderAssign the necessary variable and the header variableOpen the two fileIn fp1 for link input file for read privilegeAnd fp2 for load map file for write privilegeRead the character until the input is not equal to ENDAnd the check the character in if condition input is H thenGet the name from fp1Copy the name between csname=nameAnd extsym=**

Else if the character is DThen get the input variableIn these if condition perform the while loop the read character until the input is not equal to RThe copy csnmae=**And extsym=inputThen add address =add+csaddrAnd length is equal to zeroAnd perform the operationFinally terminate the program

Source Program:

#include#include#include

#includechar sub[10];struct estab{char ctrlname[10];char sym[10];int addr,length;}estab[10];void substr(char*inp,int i,int j){int k,x=0;char str[10];for(k=i;k<=j;k++)

{if(inp[k]!=' ')sub[x++]=inp[k];sub[x]='';}

Page 31: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

31/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

}void main(){int n,i,j,paddr,csaddr,ctrlen,esptr=0;char str1[16],str[20][80];clrscr();printf("enter the program address:\n");scanf("%d",&paddr);

csaddr=paddr;strcpy(str[0],"HPROGA 00000000063");strcpy(str[1],"DLISTA 000040ENDA 00054");strcpy(str[2],"T00101218031036482061081033");strcpy(str[3],"M00100606+COPY");strcpy(str[4],"E001000");strcpy(str[5],"HPROGB 00000000073");strcpy(str[6],"DLISTB 000040ENDB 00054");strcpy(str[7],"T00101218031036482061081033");strcpy(str[8],"E001000");strcpy(str[9],"HPROGC 00000000053");strcpy(str[6],"DLISTC 000040ENDC 00054");strcpy(str[11],"E001000");strcpy(str[12],"END");

n=13;for(i=0;i<=n;i++){if(str[i][0]=='H'){substr(str[i],1,6);strcpy(estab[esptr].ctrlname,sub);strcpy(estab[esptr].sym,"_");estab[esptr].addr=csaddr;substr(str[i],13,18);strcpy(str1,sub);ctrlen=estab[esptr++].length=atoi(str1);while(str[i][0]!='E'){

if(str[i][0]=='D'){for(j=1;j<strlen(str[i]);j=j+12){strcpy(estab[esptr].ctrlname,"_");substr(str[i],j,j+5);strcpy(estab[esptr].sym,sub);substr(str[i],j+6,j+11);estab[esptr].addr=atoi(sub)+csaddr;

Page 32: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

32/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

estab[esptr++].length=0;}}i++;

}csaddr=csaddr+ctrlen;}}printf("\n PASS1 output for direct linking loader:\n");printf("\n CSECTION\tSYMBOL\tADDRESS\TLENGTH\n");for(i=0;i<esptr;i++)printf("\n%s\t%s\t%d\t%d\n",estab[i].ctrlname,estab[i].sym,estab[i].addr,estab[i].length);getch();}

OUTPUT:

enter the program address:

1000

PASS1 output for direct linking loader:

CSECTION SYMBOL ADDRESS LENGTH

PROGA _ 1000 63

_ LISTA 1040 0

_ ENDA0 1054 0

PROGB _ 1063 73

_ LISTC 1103 0

_ ENDC0 1117 0

PROGC _ 1136 53

9. IMPLEMENTATION OF PASS2 OF THE DIRECT LINKING LOADER

AimTo write a c program to Implement pass two of direct linking loaderAlgorithm :Star the program for linking loaderAssign the necessary variable and the header variableOpen the two fileIn fp1 for link input file for read privilegeAnd fp2 for load map file for write privilege

Page 33: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

33/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

Read the character until the input is not equal to ENDAnd the check the character in if condition input is H thenGet the name from fp1Copy the name between csname=nameAnd extsym=**Else if the character is DThen get the input variableIn these if condition perform the while loop the read character until the input is not equal to RThe copy csnmae=**And extsym=inputThen add address =add+csaddrAnd length is equal to zeroAnd perform the operation

Finally terminate the program

SourceProgram:

#include#include#include#includechar sub[10];//int i=0;struct loader{int loc;char value[10];}

ldr[50];struct estab{char ctrlname[10];char sym[10];int addr,length;}estab[10];void substr(char *inp,int i,int j){int k,x=0;for(k=i;k<=j;k++){

if(inp[k]!=' ')sub[x++]=inp[k];sub[x]='';}}

Page 34: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

34/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

void main(){int x1,i,i1,j,x,n,csddr,ctrlen,start,end,mlength,mstart,a,ct,paddr,length;int esptr=0,cnt=0,pstart,plength;char str1[10],str[20][80],name[10];clrscr();strcpy(estab[0].ctrlname,"PROGA");strcpy(estab[0].sym,"-");estab[0].addr=4000;

estab[0].length=63;strcpy(estab[1].ctrlname,"-");strcpy(estab[1].sym,"LISTA");estab[1].addr=4040;estab[1].length=0;strcpy(estab[2].ctrlname,"-");strcpy(estab[2].sym,"ENDA");estab[2].addr=4054;estab[2].length=0;esptr=3;strcpy(str[0],"HPROGA 0010000000453");strcpy(str[1],"DLISTA 000040ENDA 000054");strcpy(str[2],"T00100012156789094203");strcpy(str[3],"T0010001218031036482061081033");

strcpy(str[4],"T00103606101036");strcpy(str[5],"T00104806300005");strcpy(str[6],"M00100604+PROGA");strcpy(str[7],"M00101404+LISTA");strcpy(str[8],"M00103604+ENDA");strcpy(str[9],"E001000");strcpy(str[10],"END");substr(str[0],1,6);strcpy(name,sub);printf("\n program name:%s \n",name);substr(str[0],7,12);strcpy(str1,sub);pstart=atoi(str1);printf("\n start location:%d\n",pstart);

substr(str[0],13,18);strcpy(str1,sub);plength=atoi(str1);printf("\n proglen %d\n",plength);n=11;substr(str[2],1,6);strcpy(str1,sub);for(i=1;i<n;i++)

Page 35: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

35/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{if(str[i][0]!='E'){if(str[i][0]=='T'){

substr(str[i],1,6);strcpy(str1,sub);start=atoi(str1);if(end<start)for(j=end;j<start;j=j+2){ldr[cnt].loc=j;strcpy(ldr[cnt++].value,"xx");}substr(str[i],7,8);strcpy(str1,sub);length=atoi(str1);end=start+length;

for(j=9;j<strlen(str[i]);j=j+2){substr(str[i],j,j+1);strcpy(str1,sub);ldr[cnt].loc=start;strcpy(ldr[cnt++].value,str1);start=start+2;}}else if(str[i][0]=='M'){substr(str[i],1,6);mstart=atoi(sub);substr(str[i],7,8);

mlength=atoi(sub);substr(str[i],10,strlen(str[i]));strcpy(name,sub);for(i1=0;i1<esptr;i1++){if((strcmp(estab[i1].sym,name))==0||(strcmp(estab[i1].ctrlname,name)==0)){for(a=0;a<cnt;a++){if(ldr[a].loc==mstart){ct=0;while(ct<mlength)

Page 36: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

36/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{itoa(estab[i].addr,str1,10);substr(str1,ct,ct+1);itoa(atoi(ldr[a].value)+atoi(sub),str1,10);strcpy(ldr[a].value,str1);ct+=2;a+=1;}}}}}}}

}for(i=0;i<cnt;i++)printf("\n%d\t%s",ldr[i].loc,ldr[i].value);getch();}

OUTPUT:

program name:PROGA

start location:1000

proglen 45

1000 15

1002 671004 891006 91008 421010 031000 181002 031004 101006 361008 481010 201012 611014 81016 10

1018 331012 xx1014 0

Page 37: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

37/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

10.IMPLEMENTATION OF A SIMPLE TEXT EDITOR

Aim:To write a c program for a simple text editorAlgorithm:To display a menu using switch caseEnter the choiceIf the choice is the appropriate one and perform the next processEnter the file name and edit the textSelect the next choice and open the file to view the contents

Then select the next choice to copy and paste the text

Source program:#include#include#include#includevoid newfile();void openfile();void copytext();void pastetext();void main(){int i;

clrscr();do{printf(“\n1.newfile\n2.openfile\n3.copytext\n4.pastetext\n5.exit”);scanf(“%d”,&i);printf(“enter ur choice”);switch(i){case 1:newfile();break;case 2:openfile();break;

case 3:copytext();break;case 4:pastetext();break;}}while(i<=4);

Page 38: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

38/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

getch();}void newfile(){FILE *f1;

char name[15],c;clrscr();printf("give name of file which has to be created");scanf("%s",name);f1=fopen(name,"w");printf("enter text to be written into file & press 'ctrl-z' to terminate");while((c=getchar())!=EOF)putc(c,f1);fclose(f1);}void openfile(){char name[15],c;FILE *f2;

printf("enter the name of file to open");scanf("%s",name);f2=fopen(name,"r");if(f2==NULL){printf("file does not exit");}else{printf("content of file are:");while((c=getc(f2))!=EOF)printf("%c",c);}fclose(f2);

getch();}void copytext(){FILE *f1,*f2;char f[5],oldword[50],fromfile[50];printf("enter file name");scanf("%s",f);f1=fopen(f,"r");f2=fopen("clipboard.txt","w");printf("enter string to be searched");scanf("%s",oldword);

Page 39: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

39/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

if(f1==NULL)

{printf("\n file does not exist");}else{fscanf(f1,"%s",fromfile);while(!feof(f1)){if(strcmp(oldword,fromfile)==0)printf(f2,"%s",oldword);fscanf(f1,"%s",fromfile);}fclose(f1);fclose(f2);

}getch();}void pastetext(){char c[15],b;FILE *f1,*f2;printf("enter file name");scanf("%s",c);f1=fopen("clipboard.txt","r");f2=fopen(c,"a+");if(f1==NULL){printf("\n files does not exist");

}while(!feof(f1)){b=getc(f1);fprintf(f2,"%c",b);}fclose(f2);fclose(f1);}

OUTPUT:1.newfile2.openfile3.copytext

4.pastetext5.exit

Page 40: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

40/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

enter ur choice:1give name of file which has to be cerated:file1enter text to be written into file & press 'ctrl-z' to terminate:system soft^Z

1.newfile2.openfile3.copytext4.pastetext5.exit

enter ur choice:2enter the name of file to open:file1content of file are:system soft

1.newfile2.openfile3.copytext4.pastetext5.exit

enter ur choice:3enter file name:file1enter string to be searched:system

1.newfile2.openfile3.copytext4.pastetext5.exit

enter ur choice:2enter the name of file to open:clipboard.txtcontent of file are:system

1.newfile2.openfile3.copytext4.pastetext

5.exit

enter ur choice:4enter file name:file2content of file are:system

1.newfile

Page 41: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

41/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

2.openfile3.copytext4.pastetext5.exit

enter ur choice:5

Ex: No: 11

Implementation of symbol table with suitable hashing

#include#include#include#include#define MAX 11char l[10];struct symb{int add;char label[10];

}sy[11];void search();void main(){int a[MAX],num,key,i,ch;char ans;int create(int);void lprob(int [],int,int);void display(int []);clrscr();printf(“—————————————————–\n”);printf(“Implementation of Symbol Table with Suitable Hashing\n”);printf(“—————————————————–\n”);

for(i=0;i<MAX;i++)a[i]=0;do{

printf("\n\n 1.create a symbol table \n 2.search in the symbol table\n 3.Exit");printf("\n Enter your choice:");scanf("%d",&ch);switch(ch){case 1:do

Page 42: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

42/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

{printf("\nEnter the address:");

scanf("%d",&num);key=create(num);printf("Enter The Label:");scanf("%s",l);lprob(a,key,num);printf("\nContinue(y/n)?");ans=getche();}while(ans=='y');display(a);break;case 2:search();break;

case 3:exit(0);}}while(ch<=2);getch();}

int create(int num){int key;key=num%11;return key;}

void lprob(int a[MAX],int key,int num){int flag,i,count=0;void display(int a[]);flag=0;if(a[key]==0){a[key]=num;sy[key].add=num;strcpy(sy[key].label,l);}else{i=0;

while(i<MAX){

Page 43: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

43/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

if(a[i]!=0)count++;i++;}if(count==MAX){printf("\nHash table is full");display(a);getch();exit(1);}

for(i=key+1;i<MAX;i++)if(a[i]==0){a[i]=num;flag=1;sy[key].add=num;strcpy(sy[key].label,l);break;}for(i=0;i<key && flag==0;i++)if(a[i]==0){a[i]=num;flag=1;

sy[key].add=num;strcpy(sy[key].label,l);break;}}}void display(){FILE *fp;int i;fp=fopen("symbol.txt","w");printf("\nThe Symbol Table is");printf("\nhashvalues address label");for(i=0;i<MAX;i++)

{printf("\n%d\t %d\t %s",i,sy[i].add,sy[i].label);fprintf(fp,"\n%d %d %s",i,sy[i].add,sy[i].label);}fclose(fp);}

Page 44: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

44/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

void search(){FILE *fp1;char la[10];int set=0,s;int j,i;printf("Enter the Label: ");

scanf("%s",la);fp1=fopen("symbol.txt","r");for(i=0;i<MAX;i++){fscanf(fp1,"%d%d",&j,&sy[i].add);if(sy[i].add!=0)fscanf(fp1,"%s",sy[i].label);}for(i=0;i<MAX;i++){if(sy[i].add!=0){if(strcmp(sy[i].label,la)==0){

set=1;s=sy[i].add;}}}if(set==1)printf("\nThe label –%s– is present in the symbol table at address:%d\n",la,s);elseprintf("\nThe label is not present in the symbol table\n");}

Output:

—————————————————–

Implementation of Symbol Table with Suitable Hashing—————————————————–

1.create a symbol table2.search in the symbol table3.ExitEnter your choice:1

Enter the address:1000Enter The Label:vec

Page 45: CS2308 System Software Lab « VECCSE2

7/29/12 CS2308 System Software Lab « VECCSE2

45/45webcache.googleusercontent.com/search?q=cache:http://veccse2.wordpress.com/2010/10/04/syste…

Continue(y/n)?yEnter the address:2000

Enter The Label:cse

Continue(y/n)?nThe Symbol Table ishashvalues address label0 01 02 03 04 05 06 07 08 0

9 2000 cse10 1000 vec

1.create a symbol table2.search in the symbol table3.ExitEnter your choice:2

Enter the Label: cse

The label –cse– is present in the symbol table at address:2000

1.create a symbol table2.search in the symbol table

3.ExitEnter your choice:

Blog at WordPress.com. • Theme: Koi by N.Design.


Recommended