+ All Categories
Home > Documents > CS2308 Lecture Plan

CS2308 Lecture Plan

Date post: 04-Apr-2018
Category:
Upload: pjxraj
View: 215 times
Download: 0 times
Share this document with a friend
56
ARULMIGU MEENAKSHI AMMAN COLLEGE OF ENGINEERING Departmen t of Information Technology LAB MANUAL CS 2308 SYSTEM SOFTWARE LAB V SEMESTER  Mr. BALA SUBRAMANIAN C ASSISTANT PROFESSOR STAFF INCHARGE HOD PRINCIPAL
Transcript
Page 1: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 1/56

ARULMIGU MEENAKSHI AMMAN COLLEGE

OF ENGINEERING

Department of Information Technology

LAB MANUAL

CS 2308 SYSTEM SOFTWARE LABV SEMESTER 

 

Mr. BALA SUBRAMANIAN CASSISTANT PROFESSOR 

STAFF INCHARGE HOD PRINCIPAL

Page 2: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 2/56

CS2308 SYSTEM SOFTWARE LAB 0 0 3 2

  ( Using C)

Implement a symbol table with functions to create, insert, modify, search, and display.

1. Implement pass one of a two pass assembler.

2. Implement pass two of a two pass assembler.

Implement a single pass assembler.

Implement a two pass macro processor Implement a single pass macro processor.

3. Implement an absolute loader.

4. Implement a relocating loader.

5. Implement pass one of a direct-linking loader.

6. Implement pass two of a direct-linking loader.

7. Implement a simple text editor with features like insertion / deletion of acharacter, word, and sentence.

8. Implement a symbol table with suitable hashing

(For loader exercises, output the snap shot of the main memory as it would be, after theloading has taken place)

Page 3: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 3/56

Implement a symbol table with functions to create, insert, modify,

search, and display.

Aim:

To write a C program to implement a Symbol Table

Algorithm:

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

symbol table.

2 Define the structure of the Symbol Table

3 Enter the choice for performing the operations 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 displays “Duplicate Symbol”. Else, insert the symbol

and the corresponding address in the symbol table.

5 If the entered choice is 2, the symbols present in the symbol table are displayed.

6 If the entered choice is 3, the symbol to be deleted is searched in the symbol table.

7 If it is not found in the symbol table it displays “Label Not found”. Else, the symbol

is deleted.

8 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 Code program in c implement symbol table

# include <stdio.h>

# include <conio.h>

# include <alloc.h>

# include <string.h>

# define null 0

int size=0;

void insert();

void del();

int search(char lab[]);

void modify();

void display();

struct symbtab

{

char label[10];

int addr;

struct symtab *next;

};

struct symbtab *first,*last;

Page 4: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 4/56

void main()

{

int op;

int y;

char la[10];

clrscr();

do

{

printf("\nSYMBOL TABLE IMPLEMENTATION\n");

printf("1. INSERT\n");

printf("2. DISPLAY\n");

printf("3. DELETE\n");

printf("4. SEARCH\n");

printf("5. MODIFY\n");

printf("6. END\n");

printf("Enter your option : ");

scanf("%d",&op);

switch(op)

{

case 1:insert();

display();

break;

case 2:

display();

break;

case 3:

del();

display();

break;

case 4:printf("Enter the label to be searched : ");

scanf("%s",la);

y=search(la);

if(y==1)

{

printf("The label is already in the symbol Table");

}

else

{

printf("The label is not found in the symbol table");

}break;

case 5:

modify();

display();

break;

case 6:

break;

}

}

Page 5: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 5/56

Page 6: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 6/56

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("What do you want to modify?\n");

printf("1. Only the label\n");

printf("2. Only the address of a particular label\n");printf("3. Both the label and address\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("Enter the old label\n");

scanf("%s",l);

printf("Enter the new label\n");

scanf("%s",nl);

s=search(l);if(s==0)

{

printf("NO such label");

}

else

{

for(i=0;i<size;i++)

{

if(strcmp(p->label,l)==0)

{

strcpy(p->label,nl);}

p=p->next;

}

}

break;

case 2:

printf("Enter the label whose address is to modified\n");

scanf("%s",l);

printf("Enter the new address\n");

Page 7: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 7/56

scanf("%d",&add);

s=search(l);

if(s==0)

{

printf("NO such label");

}

else

{

for(i=0;i<size;i++)

{

if(strcmp(p->label,l)==0)

{

p->addr=add;

}

p=p->next;

}

}

break;

case 3:

printf("Enter the old label : ");scanf("%s",l);

printf("Enter the new label : ");

scanf("%s",nl);

printf("Enter the new address : ");

scanf("%d",&add);

s=search(l);

if(s==0)

{

printf("NO such label");

}

else{

for(i=0;i<size;i++)

{

if(strcmp(p->label,l)==0)

{

strcpy(p->label,nl);

p->addr=add;

}

p=p->next;

}

}break;

}

}

void del()

{

int a;

char l[10];

struct symbtab *p,*q;

p=first;

Page 8: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 8/56

printf("Enter the label to be deleted\n");

scanf("%s",l);

a=search(l);

if(a==0)

{

printf("Label 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--;

}

}

OUTPUT

1. INSERTION OPERTION OF SYMBOL TABLE

Page 9: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 9/56

2. DISPLAY OPERATION OF SYMBOL TABLE

3. SEARCHING OPERATION OF SYMBOL TABLE

Page 10: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 10/56

4. MODIFICATION OPERATION OF SYMBOL TABLE

5. DELETION OPERATION OF SYMBOL TABLE

Page 11: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 11/56

Page 12: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 12/56

2 Implement pass one of a two pass assembler.

Aim

To implement pass one assembler algorithm of two pass assembler

Algorithm

1 Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode2 Read the source program

3 If the opcode read in the source program is START, the variable location counter is

initialized with the operand value.4 Else the location counter is initialized to 0.

5 The source program is read line by line until the reach of opcode END.

6 Check whether the opcode read is present in the operation code table.7 If the opcode is present, then the location counter is incremented by 3.

8 If the opcode read is WORD, the location counter is incremented by3.

9 If the opcode read is RESW, the operand value is multiplied by 3 and then thelocation

10 Counter is incremented.11 If the opcode read is RESB, the location counter value is incremented by operand

value.12 If the opcode read is BYTE, the location counter is auto incremented.

13 The length of the source program is found using the location counter value.

Source code program in c Implementation of pass one assembler algorithm of 

two pass assembler#include<stdio.h>#include<stdlib.h>

#include<conio.h>

#include<string.h>

void main()

{

void wordcount();

char opcode[10],operand[10],label[10],code[10][10],ch1,ch2,ch3;char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};

int st;

int locctr,length,start,j=0;

FILE *fp1,*fp2,*fp3;

clrscr();

fp1=fopen("INPUT.DAT","r");

fp2=fopen("SYMTAB.DAT","w");

fp3=fopen("OUTPUT.DAT","w");fscanf(fp1,"%s%s%s",label,opcode,operand);

if(strcmp(opcode,"START")==0)

{

start=atoi(operand);

locctr=start;

fprintf(fp3,"%s %s %s\n",label,opcode,operand);

fscanf(fp1,"%s%s%s",label,opcode,operand);

}

else

locctr=0;

while(strcmp(opcode,"END")!=0)

Page 13: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 13/56

{

fprintf(fp3,"%d",locctr);

if(strcmp(label,"**")!=0)

fprintf(fp2,"%s\t%d\n",label,locctr);

strcpy(code[j],mnemonic[j]);

while(strcmp(mnemonic[j],"END")!=0)

{if(strcmp(opcode,mnemonic[j])==0)

{

locctr+=3;

 break;

}

strcpy(code[j],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," %s %s %s\n",label,opcode,operand);fscanf(fp1,"%s%s%s",label,opcode,operand);length=locctr-start;

}

fprintf(fp3,"%d %s %s %s\n",locctr,label,opcode,operand);

fcloseall();

getch();

 printf("\n\n contents of the input file \n\n");

fp1=fopen("INPUT.DAT","r");ch1=fgetc(fp1);

while(ch1!=EOF)

{

 printf("%c",ch1);ch1=fgetc(fp1);

}

getch();

 printf("\n\n contents of the intermediate file\n\n");

fp3=fopen("OUTPUT.DAT","r");

ch2=fgetc(fp3);while(ch2!=EOF)

{

 printf("%c",ch2);

ch2=fgetc(fp3);

}

getch();

 printf("\n\n word count from intermediate file\n");wordcount();

 printf("\n\n length of the intermediate file : %d",length);

getch();

 printf("\n\n contents of the symbol table\n\n");

fp2=fopen("SYMTAB.DAT","r");

ch3=fgetc(fp2);

while(ch3!=EOF)

{

 printf("%c",ch3);ch3=fgetc(fp2);

}

Page 14: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 14/56

fcloseall();

getch();

}

void wordcount()

{

FILE *fp4;char c;

int word=0,l=1;

fp4=fopen("OUTPUT.DAT","r");

c=fgetc(fp4);

while(c!=EOF)

{

if(c==' ')

word+=1;

if(c=='\n')

{word+=1;

 printf("\n number of words in line %d: %d",l,word);

l++;

word=0;

}

c=fgetc(fp4);

}}

OUTPUT OF PASS ONE ASSEMBLER ALGORITHM

Page 15: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 15/56

3 Implement pass two of a two pass assembler.

Aim

To implement pass two algorithm of pass two assembler  

Algorithm

1 Start the program

2 Initialize all the variables

3 Open a file by name

  fp1=fopen("assmlist.dat","w");

fp2=fopen("symtab.dat","r");

fp3=fopen("intermediate.dat","r");

fp4=fopen("optab.dat","r");

4 Read the content of the file

5 If opcode is BYTE

6 if(strcmp(opcode,"BYTE")==0)

  Then

fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);

Else if opcode is WORD

else if(strcmp(opcode,"WORD")==0)

  then

  fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);

Else perform

else if((strcmp(opcode,"RESB")==0)(strcmp(opcode,"RESW")==0))

fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);

if it is not math anything

else

fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",address,label,opcode,operand,code);

7 Finally terminate the of pass two of pass two assembler 

Source code program in c Implementation of Pass Two Assembler Algorithm for

Two Pass Assembler

#include<stdio.h>

#include<conio.h>

Page 16: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 16/56

Page 17: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 17/56

while(strcmp(opcode,mnemonic[j])!=0)

 j++;

if(strcmp(operand,"COPY")==0)

{

fprintf(fp1,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code[j]);

}

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%s%d\n",address,label,opcode,operand,code[j],add);

fprintf(fp4,"^%s%d",code[j],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);

fprintf(fp4,"\nE^00%d",finaddr);

 printf("\n intermediate file is converted into object code");

fcloseall();

 printf("\n\n The content of the the intermediate file:\n\n");

fp3=fopen("output.dat","r");ch1=fgetc(fp3);while(ch1!=EOF)

{

 printf("%c",ch1);

ch1=fgetc(fp3);

}

 printf("\n\n The content of the symbol table:\n\n");

fp2=fopen("sim.dat","r");

ch2=fgetc(fp2);while(ch2!=EOF)

{

 printf("%c",ch2);

ch2=fgetc(fp2);}

 printf("\n\n The content of the output file:\n\n");

fp1=fopen("listfile.dat","r");

ch3=fgetc(fp1);

while(ch3!=EOF)

{ printf("%c",ch3);

ch3=fgetc(fp1);

}

 printf("\n\n Object code file:\n\n");

fp4=fopen("ocode.dat","r");

ch4=fgetc(fp4);

while(ch4!=EOF){

 printf("%c",ch4);

ch4=fgetc(fp4);

}

fcloseall();

getch();

}

Page 18: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 18/56

OUTPUT PASS TWO ASSEMBLER ALGORITHM

Page 19: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 19/56

Implement a single pass assembler.

Aim

To implement the concept of single pass assembler.

Algorithm

1 Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode

2 Read the source program

3 If the opcode read in the source program is START, the variable location counter is

initialized with the operand value.

4 Else the location counter is initialized to 0.

5 The source program is read line by line until the reach of opcode END.

6 Check whether the opcode read is present in the operation code table.7 If the opcode is present, then the location counter is incremented by 3.

8 If the opcode read is WORD, the location counter is incremented by3.

9 If the opcode read is RESW, the operand value is multiplied by 3 and then the location

10 Counter is incremented.

11 If the opcode read is RESB, the location counter value is incremented by operand

value.

12 If the opcode read is BYTE, the location counter is auto incremented.

13 The length of the source program is found using the location counter value and save

the content at the Intermediate file

14 Read the content of the intermediate code file

15 If opcode is BYTE

16 if(strcmp(opcode,"BYTE")==0)

  Then

fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);

Else if opcode is WORD

else if(strcmp(opcode,"WORD")==0)

  then

  fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);

Else perform

else if((strcmp(opcode,"RESB")==0)(strcmp(opcode,"RESW")==0))

fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);

Page 20: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 20/56

if it is not math anything

else

fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",address,label,opcode,operand,code);

17 both passes will get executed of single pass assembler and program will generate

object code file.

Source program in c Implementation of Single Pass Assembler

#include<stdio.h>

#include<conio.h>

#include<string.h>#include<stdlib.h>

void pass1();

void pass2();

void indisp();

void outdisp();

FILE *fp1,*fp2,*fp3,*fp4;

void main(){

clrscr();

 printf("\t\t ****** SINGLE PASS ASSEMBLER ******\n");indisp();

sleep(3);

 pass1();

sleep(3);

 pass2();

sleep(3);

outdisp();

sleep(3);getch();

}void pass1()

{

char opcode[10],operand[10],label[10],code[10][10];

char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};

int locctr,start,length,j=0;

fp1=fopen("INPUT25.DAT","r");fp2=fopen("SYMTAB25.DAT","w");

fp3=fopen("INTER25.DAT","w");

fscanf(fp1,"%s%s%s",label,opcode,operand);

if(strcmp(opcode,"START")==0)

{

start=atoi(operand);

locctr=start;

fprintf(fp3,"%s %s %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[j],mnemonic[j]);

Page 21: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 21/56

while(strcmp(mnemonic[j],"END")!=0)

{

if(strcmp(opcode,mnemonic[j])==0)

{

locctr+=3;

 break;

}strcpy(code[j],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," %s %s %s\n",label,opcode,operand);

fscanf(fp1,"%s%s%s",label,opcode,operand);

}

fprintf(fp3,"%d %s %s %s\n",locctr,label,opcode,operand);

fcloseall();

length=locctr-start; printf("\n\n Symbol Table is generated..........."); printf("\n Intermediate file is generated.......");

 printf("\n Program length of the intermediate file is %d",length);

}

void pass2()

{

char a[10],ad[10],label[10],opcode[10],operand[10],symbol[10];

int st,diff,i,address,add,len,actual_len,finaddr,prevaddr,j=0;

char mnemonic[15][15]={"LDA","STA","LDCH","STCH"};char code[15][15]={"33","44","53","57"};

fp1=fopen("listfile25.dat","w");

fp2=fopen("SYMTAB25.dat","r");

fp3=fopen("INTER25.dat","r");fp4=fopen("ocode25.dat","w");

fscanf(fp3,"%s%s%s",label,opcode,operand);

while(strcmp(opcode,"END")!=0)

{

 prevaddr=address;

fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);}

finaddr=address;

fclose(fp3);

fp3=fopen("INTER25.dat","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);fprintf(fp4," H^%s^00%s^00%d\n",label,operand,finaddr);

fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);

st=address;

diff=prevaddr-st;

fprintf(fp4," T^00%d^%d",address,diff);

}

while(strcmp(opcode,"END")!=0)

{if(strcmp(opcode,"BYTE")==0)

{

Page 22: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 22/56

fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);

len=strlen(operand);

actual_len=len-3;

fprintf(fp4,"^");

for(i=2;i<(actual_len+2);i++)

{

itoa(operand[i],ad,16);fprintf(fp1,"%s",ad);

fprintf(fp4,"%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);fprintf(fp4,"^00000%s",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

{

while(strcmp(opcode,mnemonic[j])!=0) j++;

if(strcmp(operand,"*")==0)

fprintf(fp1,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code[j]);

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%s%d\n",address,label,opcode,operand,code[j],add);

fprintf(fp4,"^%s%d",code[j],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);

fprintf(fp4,"\n E^00%d",st);

 printf("\n Listing file is generated..........");

 printf("\n Object code file is generated..........");fcloseall();

}

void indisp()

{

char ch1;

 printf("\nThe contents of Input file\n");

fp1=fopen("INPUT25.DAT","r");ch1=fgetc(fp1);while(ch1!=EOF)

{

 printf("%c",ch1);

ch1=fgetc(fp1);

}

}

void outdisp()

{char ch4;

 printf("\n\n Object code file\n");

Page 23: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 23/56

fp4=fopen("ocode25.DAT","r");

ch4=fgetc(fp4);

while(ch4!=EOF)

{

 printf("%c",ch4);

ch4=fgetc(fp4);

}fcloseall();

}

OUTPUT

Page 24: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 24/56

Page 25: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 25/56

Source code program in C Implementation of Macro Processor

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>#include<string.h>

FILE *f1,*f2,*f3,*f4,*f5;

void main()

{char lbl[20],opc[20],opr[20],mname[20],arg[20],check[20];

char ch,dlbl[20],dopc[20],dopr[20];

int c;clrscr();

f1=fopen("macin.dat","r");

rewind(f1);f2=fopen("nametab.dat","r");

rewind(f2);

f3=fopen("deftab.dat","r");

f4=fopen("expand.dat","w");f5=fopen("argment.dat","w");

while(!feof(f1))

{l1:

fscanf(f1,"%s %s %s",lbl,opc,opr);

if(strcmp(opc,mname)==0)c=1;

if(strcmp(opc,"MACRO")==0)

{while(strcmp(opc,"MEND")!=0)

{

fscanf(f1,"%s%s%s",lbl,opc,opr);

continue;}

goto l1;

}rewind(f2);

rewind(f3);

fscanf(f2,"%s",mname);if(strcmp(opc,mname)==0)

{

fprintf(f5," %s",opr);rewind(f5);while(!feof(f3))

{

fscanf(f3,"%s%s%s",dlbl,dopc,dopr);if(strcmp(dopc,"MEND")!=0)

{

if(strcmp(dopc,"MACRO")==0){

continue;

Page 26: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 26/56

}

if(strcmp(dopr,"=X'&INDEV'")==0)strcpy(dopr,"=X'F1'");

if(strcmp(dopr,"BUFADR,X")==0)

strcpy(dopr,"BUFFER,X");if(strcmp(dopr,"RECLTH")==0)

strcpy(dopr,"LENGTH");

if(c==1)

{fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr);

c=0;

}fprintf(f4," %s\t%s\t%s\n",dlbl,dopc,dopr);

}

}goto l1;

}

fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr);}

fcloseall(); printf("\n INPUT\n\n Macro Program before expanded \n");

 printf(" ---------------------------------\n");f1=fopen("macin.DAT","r");

ch=fgetc(f1);

while(ch!=EOF){

 printf("%c",ch);

ch=fgetc(f1);}

 printf("\n Definition Table \n");

 printf(" ---------------------------------\n");f2=fopen("deftab.DAT","r");

ch=fgetc(f2);

while(ch!=EOF)

{ printf("%c",ch);

ch=fgetc(f2);

} printf("\n Name Table \n");

 printf(" ---------------------------------\n");

f3=fopen("nametab.DAT","r");

ch=fgetc(f3);while(ch!=EOF)

{

 printf("%c",ch);ch=fgetc(f3);

}

getch();clrscr();

 printf("\n\n OUTPUT\n\n Macro Program after expanded \n");

 printf(" ---------------------------------\n\n");

Page 27: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 27/56

f4=fopen("expand.DAT","r");

ch=fgetc(f4);while(ch!=EOF)

{

 printf("%c",ch);ch=fgetc(f4);

}

 printf("\n Argument Table \n");

 printf(" ---------------------------------\n\n");f5=fopen("argment.DAT","r");

ch=fgetc(f5);

while(ch!=EOF){

 printf("%c",ch);

ch=fgetc(f5);}

fcloseall();

getch();}

OUTPUT

Page 28: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 28/56

Page 29: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 29/56

Page 30: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 30/56

4 Implement an absolute loader

Aim

To implement the algorithm of Absolute loader.

Algorithm for absolute loader

1 Start the program

2 Assign the required variable

3 Open the files

3.1fp1=fopen("input.dat","r");

3.2 fp2=fopen("output.dat","w");

4 Read the content

4.1Using while loop perform the loop until character is not equal to E

5 while(strcmp(input,"E")!=0)

5.1Then compare the character is equal to H

6 If H then

fscanf(fp1,"%d",&start);

Like that get length, and input

Else if the character is T

Then

Then perform the frprintf in fp1 for input file for ,

input[0],inuput[1] for address

input[2],inuput[3] for address+1

input[4],inuput[5] for address+2

Else if it is not H or T

Then perform the frprintf in fp2 for output file for ,

input[0],inuput[1] for address

input[2],inuput[3] for address+1

input[4],inuput[5] for address+2

Page 31: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 31/56

fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);

fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);

fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);

address+=3;

fscanf(fp1,"%s",input);

7 Finally terminate the program

Source code program in c performing Absoluter Loader

#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("FILE1.dat","r");

fp2=fopen("FILE2.dat","w");

fscanf(fp1,"%s",input);

 printf("\n\n******\t\t ABSOLUTE LOADER \t\t******\n");

fprintf(fp2,"\n------------------------------------------------------------\n");

fprintf(fp2,"address\t\t\t content");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)

{

Page 32: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 32/56

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 Output from FILE2.DAT\n\n");

fp2=fopen("FILE2.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;

}

Page 33: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 33/56

}

INPUT FILE

FILE1.DAT

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 38203FT 001077 1C 101036 4C0000 000000 001000 041030 E02079 302064 509039 DC2079 2C1036

E 001000

OUTPUT OF IMPLEMENTATION OF ABSOLUTE LOADER 

Page 34: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 34/56

7 Implement a relocating loader.

Aim

To implement the algorithm of Relocation Loader

Algorithm

1 Start the program

2 Include the necessary header file and variable

3 Open the two file for

3.1 fp1= relinput.dat and give read

3.2 fp2= reloutput.dat and give write

4 Read the content

5 Using while loop perform the loop until character is not equal to E

5.1 while(strcmp(input,"E")!=0)

5.1.1 If the character is H

5.1.2 Get the variable add, length, and input

5.2 Else if the character is T

5.2.1 Get the variable address and bitmask 

5.2.2 And perform the for loop for starting zero to up to len

5.3 Get the opcode ,addr and assign relocbit to bitmask 

6 If relocabit is zero

6.1 Then

6.1.1 actualadd=addr;

6.2 else

7 Add the addr and star value

8 Finally terminate the program

Source code program in c for Relocation Loader

IMPLEMENTATION OF RELOCATING LOADER 

Page 35: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 35/56

SOURCE CODE

# include <stdio.h>

# include <conio.h>

# include <string.h># include <stdlib.h>

# include <math.h>

void convert(char h[12]);

char bitmask[12];char bit[12]={0};

void main()

{char add[6],length[10],input[10],binary[12],relocbit,ch1;

int start,inp,len,i,address,opcode,addr,actualadd,tlen;

FILE *fp1,*fp2;clrscr();

 printf("Enter the actual starting address : ");

scanf("%x",&start);fp1=fopen("rein.dat","r");

fp2=fopen("reout.dat","w");fscanf(fp1,"%s",input);

fprintf(fp2,"Address\t\tContent\n");fprintf(fp2,"----------------------------\n");

while(strcmp(input,"E")!=0)

{if(strcmp(input,"H")==0)

{

fscanf(fp1,"%x",add);fscanf(fp1,"%x",length);

fscanf(fp1,"%s",input);

}if(strcmp(input,"T")==0)

{

fscanf(fp1,"%x",&address);

fscanf(fp1,"%x",&tlen);fscanf(fp1,"%s",bitmask);

address+=start;

convert(bitmask);len=strlen(bit);

if(len>=11)

len=10;

for(i=0;i<len;i++){

fscanf(fp1,"%x",&opcode);

fscanf(fp1,"%x",&addr);relocbit=bit[i];

if(relocbit=='0')

actualadd=addr;else

actualadd=addr+start;

fprintf(fp2,"\n%x\t\t%x%x\n",address,opcode,actualadd);

Page 36: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 36/56

address+=3;

}fscanf(fp1,"%s",input);

}

}fcloseall();

 printf("\n\nOutput from REOUT.DAT\n");

fp2=fopen("REOUT.DAT","r");

ch1=fgetc(fp2);while(ch1!=EOF)

{

 printf("%c",ch1);ch1=fgetc(fp2);

}

fclose(fp2);getch();

}

void convert(char h[12])

{int i,l;

strcpy(bit,"");l=strlen(h);

for(i=0;i<l;i++)

{switch(h[i])

{

case '0':strcat(bit,"0");

 break;

case '1':strcat(bit,"1");

 break;

case '2':

strcat(bit,"10"); break;

case '3':

strcat(bit,"11"); break;

case '4':

strcat(bit,"100");

 break;case '5':

strcat(bit,"101");

 break;case '6':

strcat(bit,"110");

 break;case '7':

strcat(bit,"111");

 break;

Page 37: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 37/56

case '8':

strcat(bit,"1000"); break;

case '9':

strcat(bit,"1001"); break;

case 'A':

strcat(bit,"1010");

 break;case 'B':

strcat(bit,"1011");

 break;case 'C':

strcat(bit,"1100");

 break;case 'D':

strcat(bit,"1101");

 break;case 'E':

strcat(bit,"1110"); break;

case 'F':strcat(bit,"1111");

 break;

}}

}

INPUT

//REIN.DAT

H 001000 00107A

T 001000 1E FFC 14 0033 48 1039 10 0036 28 0030 30 0015 48 1061 3C 0003 20 002A

1C 0039 30 002DT 002500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003

E 000000

OUTPUT

Page 38: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 38/56

Page 39: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 39/56

equal to R

7.1 The copy csnmae=**

7.2 And extsym=input

7.3 Then add address =add+csaddr

7.4 And length is equal to zero

8 And perform the operation (see the Source code)

9 Finally terminate the program

SOURCE PROGRAM IN C IMPLEMENTATION OF PASS 1 DIRECT

LINKING LOADER ALGORITHM

# include <stdio.h>

# include <conio.h>

# include <string.h>

# define MAX 20

struct estab

{

char csname[10];

char extsym[10];

int address;

int length;

}es[MAX];

void main()

{

char input[10],name[10],symbol[10];

int count=0,progaddr,csaddr,add,len;

FILE *fp1,*fp2;

clrscr();

fp1=fopen("linkinput.dat","r");

fp2=fopen("loadmap.dat","w");

printf("Enter the location where the program has to be loaded : ");

scanf("%d",&progaddr);

csaddr=progaddr;

fprintf(fp2,"CS_NAME\tEXT_SYM_NAME\tADDRESS\tLENGTH\n");

fprintf(fp2,"--------------------------------------\n");

fscanf(fp1,"%s",input);

while(strcmp(input,"END")!=0)

{

if(strcmp(input,"H")==0)

{

fscanf(fp1,"%s",name);

strcpy(es[count].csname,name);

strcpy(es[count].extsym,"**");

fscanf(fp1,"%d",&add);

es[count].address=add+csaddr;

fscanf(fp1,"%d",&len);

Page 40: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 40/56

es[count].length=len;

fprintf(fp2,"%s\t%s\t\t%d\t%d\n",es[count].csname,es[count].extsym,es[count].address,es[count].length);

count++;

}

else if(strcmp(input,"D")==0)

{

fscanf(fp1,"%s",input);

while(strcmp(input,"R")!=0)

{

strcpy(es[count].csname,"**");

strcpy(es[count].extsym,input);

fscanf(fp1,"%d",&add);

 // printf("CSADDR = %d",csaddr);

es[count].address=add+csaddr;

es[count].length=0;

fprintf(fp2,"%s\t%s\t\t%d\t%d\n",es[count].csname,es[count].extsym,es[count].address,es[count].length);

count++;

fscanf(fp1,"%s",input);

}

csaddr=csaddr+len;}

else if(strcmp(input,"T")==0)

{

while(strcmp(input,"E")!=0)

fscanf(fp1,"%s",input);

}

fscanf(fp1,"%s",input);

}

fprintf(fp2,"--------------------------------------\n");

fclose(fp1);

fclose(fp2);printf("FINISHED\n");

getch();

}

INPUT FILE :

LINKINPUT.DAT

H PROGA 0000 1000

D LISTA 0040 ENDA 0054

R LISTB ENDB LISTC ENDC

T 0020 141033 465555 678909 568787 345678

T 0054 000014 789087 776555 876666 456666

M 0054 06 +LISTC

E 0000

Page 41: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 41/56

H PROGB 0000 2000

D LISTB 0040 ENDB 0054

R LISTA ENDA LISTC ENDC

T 0020 141033 465555 678909 568787 345678

T 0054 000000 789087 776555 876666 456666

M 0054 06 +ENDA

M 0054 06 -LISTA

M 0054 06 +LISTC

E 0000

H PROGC 0000 3000

D LISTC 0040 ENDC 0054

R LISTA ENDA LISTC ENDB

T 0020 141033 465555 678909 568787 345678

T 0054 000020 789087 776555 876666 456666

M 0054 06 +ENDA

M 0054 06 -LISTA

M 0054 06 +PROGC

E 0000

END

OUTPUT:

Enter the location where the program has to be loaded : 5000

LOADMAP.DAT

CS_NAME EXT_SYM_NAME ADDRESS LENGTH

------------------------------------------------

PROGA ** 5000 1000

** LISTA 5040 0

** ENDA 5054 0

PROGB ** 6000 2000

** LISTB 6040 0

** ENDB 6054 0

PROGC ** 8000 3000

** LISTC 8040 0

** ENDC 8054 0

Page 42: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 42/56

9 Implement pass two of a direct-linking loader.

Aim: To write a C-program to implement pass-2 assembler ie object code creation.

Algorithm:

1. Read first line from the intermediate file.

2. Check to see if the opcode from the first line read is “START”. If so then write

label, opcode and operand field values of corresponding statement directly to final

output files.

3. Start the following processing for other lines in intermediate file if it is not a

comment line until an “END” statement is reached.

4. Start writing labels LOCCTR opcode and operand fields of corresponding

statement to the output file along with object code. The object code is found by

assembling each statement opcode machine equivalent with label address.

5. If there is no symbol or label in the operand field, then the operand address is

assigned as zero and it is assembled with object code of instruction

6. If OPCODE is BYTE, WORD, RESB etc are convert constants to oblect code

close operand file and exit.

SOURCE CODE

#include<stdio.h>

#include<conio.h>#include<string.h>

#include<stdlib.h>

struct exttable{

char csect[20];

char sname[20];

int padd,plen;}estab[20];

struct objectcode{

unsigned char code[15];

int add;}obcode[500];

void main()

{

Page 43: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 43/56

FILE *fp1,*fp2,*fp3;

int i,j,x,y,pstart,exeloc,start,textloc,loc,textlen,length,location,st,s;int n=0,num=0,inc=0,count=0,record=0,mloc[30],mlen[30];

signed long int newadd;

char operation,lbl[10],input[10],label[50][10],opr[30],ch,*add1,address[10];clrscr();

fp1=fopen("GANinP.dat","r");

fp2=fopen("L1OUT25.dat","r");

fp3=fopen("L2OUT1.dat","w");while(!feof(fp2))

{

fscanf(fp2,"%s %s %x %x",estab[num].csect,estab[num].sname,&estab[num].padd,&estab[num].plen);

num++;

}exeloc=estab[0].padd;

loc=exeloc;

start=loc;st=start;

while(!feof(fp1)){

fscanf(fp1,"%s",input);if(strcmp(input,"H")==0)

{

fscanf(fp1,"%s",input);for(i=0;i<num;i++)

if(strcmp(input,estab[i].csect)==0)

{ pstart=estab[i].padd;

 break;

}while(strcmp(input,"T")!=0)

fscanf(fp1,"%s",input);

}

do{

if(strcmp(input,"T")==0)

{fscanf(fp1,"%x",&textloc);

textloc=textloc+pstart;

for(i=0;i<(textloc-loc);i++)

{strcpy(obcode[inc].code,"..");

obcode[inc++].add=start++;

}

fscanf(fp1,"%x",&textlen);

loc=textloc+textlen;}

else if(strcmp(input,"M")==0)

{

Page 44: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 44/56

fscanf(fp1,"%x",&mloc[record]);

mloc[record]=mloc[record]+pstart;fscanf(fp1,"%x",&mlen[record]);

fscanf(fp1,"%s",label[record++]);

}else

{

length=strlen(input);

x=0;for(i=0;i<length;i++)

{

obcode[inc].code[x++]=input[i];if(x>1)

{

obcode[inc++].add=start++;x=0;

}

}}

fscanf(fp1,"%s",input);}while(strcmp(input,"E")!=0);

if(strcmp(input,"E")==0)

fscanf(fp1,"%s",input);

}for(n=0;n<record;n++)

{

operation=label[n][0];length=strlen(label[n]);

for(i=1;i<length;i++)

{lbl[i-1]=label[n][i];

}

lbl[length-1]='\0';

length=0;strcpy(address,"\0");

location=mloc[n]-exeloc;

loc=location;count=0;

while(length<mlen[n])

{

strcat(address,obcode[location++].code);count++;

length+=2;

}for(i=0;i<num;i++)

{

if(strcmp(lbl,estab[i].csect)==0) break;

if(strcmp(lbl,estab[i].sname)==0)

 break;

Page 45: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 45/56

}

switch(operation){

case '+':

newadd=strtol(address,&add1,16)+(long int)estab[i].padd; break;

case '-':

newadd=strtol(address,&add1,16)-(long int)estab[i].padd;

 break;}

ltoa(newadd,address,16);

x=0;y=0;

while(count>0)

{obcode[loc].code[x++]=address[y++];

if(x>1)

{x=0;

loc++;count--;

}}

}

count=0;n=0;

s=st-16;

fprintf(fp3,"%x\t",s);for(i=1;i<=16;i++)

{

fprintf(fp3,"xx");if(i==4||i==8||i==12)

{

fprintf(fp3,"\t");

}}

fprintf(fp3,"\n\n%x\t",obcode[0].add);

for(i=0;i<inc;i++){

fprintf(fp3,"%s",obcode[i].code);

n++;

if(n>3){

fprintf(fp3,"\t");

n=0;count++;

}

if(count>3){

fprintf(fp3,"\n\n%x\t",obcode[i+1].add);

count=0;

Page 46: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 46/56

}

}fcloseall();

 printf("\n Output from L2OUT1.DAT\n\n");

 printf("\n Address\t\t\t Contents\n\n"); printf("\n-----------------------------------------------------------\n\n");

fp3=fopen("L2OUT1.dat","r");

ch=fgetc(fp3);

while(ch!=EOF){

 printf("%c",ch);

ch=fgetc(fp3);}

fclose(fp3);

getch();}

INPUT

//L2IN25.DAT

H PROGA 000000 000063D LISTA 000040 ENDA 000054

R LISTB ENDB LISTC ENDC

T 000020 0A 03201D 77100004 050014T 000054 0F 100014 000008 004051 000004 100000

M 000024 05 +LISTB

M 000054 06 +LISTCM 000060 06 +LISTB

M 000060 06 -LISTA

E 000020

H PROGB 000000 00007F

D LISTB 000060 ENDB 000070

R LISTA ENDA LISTC ENDCT 000036 0B 03100000 772027 05100000

T 000070 0F 100000 000008 004051 000004 100060

M 000037 05 +LISTA

M 00003E 05 +ENDAM 00003E 05 -LISTA

M 000070 06 +ENDA

M 000070 06 -LISTAM 000070 06 +LISTC

M 00007C 06 +PROGB

M 00007C 06 -LISTAE 000000

Page 47: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 47/56

H PROGC 000000 0000051

D LISTC 000030 ENDC 000042R LISTA ENDA LISTB ENDB

T 000018 0C 03100000 77100004 05100000

T 000042 0F 100030 000008 004051 000004 100000M 000019 05 +LISTA

M 00001D 05 +LISTB

M 000021 05 +ENDA

M 000021 05 -LISTAM 000042 06 +ENDA

M 000042 06 -LISTA

M 000042 06 +PROGCM 00004E 06 +LISTB

M 00004E 06 -LISTA

E 000000

//L1OUT25.DAT

PROGA ** 4000 63

** LISTA 4040** ENDA 4054

PROGB ** 4063 7F** LISTB 40C3

** ENDB 40D3

PROGC ** 40E2 51** LISTC 4112

** ENDC 4124

Page 48: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 48/56

OUTPUT

 

Page 49: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 49/56

10 Implement a simple text editor with features like insertion /

deletion of a character, word, and sentence

Aim: To write a C Program fro text editor using files.

Algorithm:

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

2. If choice 1 , call new.

3. If choice 2, call open.

4. else, exit.

5 new()

Get the file name and open it in write mode.

Get the text from the user to write it.

6 open()

Get the file name from user.

If present then open it in append mode.

Display options modify, search, back and get choice.

If choice 1, call modify.

If choice 2, call search.

Exit

7 modify()

Get the text to add the existing file.

Write it into the file and save the file.

8 Search()

Get the string to search.

If it is present then print word found.

Else, print word not found.

Source code program in C Implementation of simple Text Editor

# include <stdio.h>

# include <conio.h>

# include <ctype.h>

# include <dos.h>

# include <iostream.h>

Page 50: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 50/56

# include <fstream.h>

char filename[15];

char buff[1000];

int curx,cury,count;

void cur_pos()

{

curx=wherex();

cury=wherey();

textcolor(12);

textbackground(9);

gotoxy(35,1);

cout<<"\n";

cout<<"##############################################\n";

cout<<"\n";

cout<<"\t\tTEXT EDITOR\n";

cout<<"##############################################\n";

cout<<"\n Type your text and then press ESC key\n";

gotoxy(100,500);

cprintf("%2d%2d",cury,curx);

gotoxy(curx,cury);}

void main()

{

char ch,c;

ofstream outfile;

ifstream infile;

clrscr();

cur_pos();

curx=wherex();

cury=wherey();while(c!=27)

{

c=getch();

switch(c)

{

case 80: //down arrow

gotoxy(curx,cury+1);

break;

case 77: //right side

gotoxy(curx+1,cury);

break;case 72: //up arrow

gotoxy(curx,cury-1);

break;

case 75: //left side

gotoxy(curx-1,cury);

break;

case 32: //space

printf(" ");

buff[count++]=' ';

Page 51: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 51/56

break;

case 13: //new line

gotoxy(1,cury+1);

buff[count++]='\n';

break;

default:

textcolor(13);

if((c>=65 && c<=122) (c>48 && c<57))

cprintf("%c",c);

break;

}

buff[count++]=c;

cur_pos();

}

cprintf("\n\nDo you want to save? (y/n)");

scanf("%c",&c);

if((c=='y')(c=='Y'))

{

cprintf("\n\nEnter the file name with extension in 8 characters only");

scanf("%s",filename);outfile.open(filename,ios::out);

outfile<<buff;

outfile.close();

cout<<"\nDo you want to open? (y/n) \n";

ch=getch();

if((ch=='y')(ch=='Y'))

{

cprintf("\n\nEnter the file name to open");

scanf("%s",filename);

infile.open(filename,ios::in);

infile.get(buff,count,'*');gotoxy(90,1);

printf("%s",buff);

getch();

infile.close();

}

}

}

OUTPUT:

CREATE FILE:

OPEN FILE

Page 52: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 52/56

EDIT FILE:

Page 53: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 53/56

11. Symbol Table Using Hashing

Aim

To Implement a Symbol Table using Hashing Function.

/* IMPLEMENTATION OF SYMBOL TABLE USING HASHING */

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

#define MAX 10

char l[10];

struct symb{

int add;

char label[10];

Page 54: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 54/56

}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();for(i=0;i<MAX;i++)sy[i].add=-1;

do

{

 printf("\nEnter your choice:1.create a symbol table\n2.exit");

scanf("%d",&ch);

switch(ch)

{

case 1:

do

{

 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:exit(0);

}}while(ch<=2);

getch();

}

int create(int num)

{

int key;

key=num%10;

return key;

}

void lprob(int a[MAX],int key,int num)

{int flag,i,count=0;void display(int a[]);

flag=0;

if(sy[key].add==-1)

{

a[key]=num;

sy[key].add=num;

strcpy(sy[key].label,l);

}

else

Page 55: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 55/56

{

i=0;

while(i<MAX)

{

if(sy[i].add!=-1)

count++;

i++;

}

if(count==MAX)

{ printf("\nHash table is full");display(a);

getch();

exit(1);

}

for(i=key+1;i<MAX;i++)

if(sy[i].add==-1)

{

a[i]=num;

flag=1;

sy[i].add=num;

strcpy(sy[i].label,l); break;

}

for(i=0;i<key&&flag==0;i++)

if(sy[i].add==0)

{

a[i]=num;

flag=1;sy[key].add=num;

strcpy(sy[key].label,l);

 break;

}}}

void display(int a[MAX]){

FILE *fp;

int i;

fp=fopen("symbol.txt","w");

 printf("\nThe symbol Table is");

 printf("\nHashvalues\taddress 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);}

// OUTPUT

Enter your choice1.create a symbol table

2.exit

1

Page 56: CS2308 Lecture Plan

7/30/2019 CS2308 Lecture Plan

http://slidepdf.com/reader/full/cs2308-lecture-plan 56/56

Enter the address :0

Enter the label:a

continue(y/n)?y

Enter the address :4Enter the label:f 

continue(y/n)?y

Enter the address :5Enter the label:g

continue(y/n)?yEnter the address :5

Enter the label:k 

continue(y/n)?nThe symbol Table is

Hashvalues address label

0 0 a1 -1

2 -13 -1

4 4 f 5 5 g

6 5 k 

7 -18 -1

9 -1

Enter your choice1.create a symbol table

2.exit

2


Recommended