SSLABPROGRAM2

55
SYMBOL TABLE EX.NO: DATE : AIM: To write a C program to implement 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. 5. If the symbol is already present, it displays "Duplicate Symbol". 6. Else, insert the symbol and the corresponding address in the symbol table. 7. If the entered choice is 2, the symbols present in the symbol table are displayed. 8. If the entered choice is 3, the symbol to be deleted is searched in the symbol table. 9. If it is not found in the symbol table it displays "Label Not found". Else, the symbol is deleted. 10. If the entered choice is 5, the symbol to be modified is searched in the symbol table. 11. The label or address or both can be modified.

Transcript of SSLABPROGRAM2

Page 1: SSLABPROGRAM2

SYMBOL TABLEEX.NO:DATE :

AIM:To write a C program to implement 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 Table3. Enter the choice for performing the operations in the symbol Table4. If the entered choice is 1, search the symbol table for the symbol to be

inserted. 5. If the symbol is already present, it displays "Duplicate Symbol". 6. Else, insert the symbol and the corresponding address in the symbol table.7. If the entered choice is 2, the symbols present in the symbol table are

displayed.8. If the entered choice is 3, the symbol to be deleted is searched in the symbol

table. 9. If it is not found in the symbol table it displays "Label Not found". Else, the

symbol is deleted.10.If the entered choice is 5, the symbol to be modified is searched in the

symbol table.11. The label or address or both can be modified.

Page 2: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>void main(){int i=0,j=0,k=0,opt,b[200],tab[200];char ch,oz;clrscr();printf("\n Symbol Table Creation:");option:printf("\n1.Create table \n2.Insert character \n3.Modify character \n4.Search character \n5.Display table \n6.Exit");printf("Enter ur choice:\t");fflush(stdin);scanf("%d",&opt);switch(opt){case 1:printf("\nEnter the total no of character to be inserted in a table\t");fflush(stdin);scanf("%d",&k);printf("\nTable created for %d character\t",k);goto option;case 2:printf("\nEnter the character \t");fflush(stdin);scanf("%c",&ch);tab[i]=((int)ch);printf("\nEnter the reference number\t");scanf("%d",&b[i]);i++;goto option;case 3:printf("\nEnter the character to be modify\t");fflush(stdin);scanf("%c",&ch);if(k==0)printf("\nCreate the table first");for(i=0;i<k;i++){if(ch==tab[i])j=1;elsej=0;

Page 3: SSLABPROGRAM2

if(j==1){printf("\nEnter the new character to be modify:\t");fflush(stdin);scanf("%c",&oz);tab[i]=((int)oz);printf("\nCharacter modified");goto option;}}case 4:printf("\nEnter the character to be searched:\n");fflush(stdin);scanf("%c",&ch);if(k==0)printf("\nCreate the table first");for(i=0;i<k;i++){if(ch==tab[i])j=1;elsej=0;if(j==1)printf("\nCharacter found at %d position",i);elseprintf("\nCharacter not found at %d position",i);}goto option;case 5:printf("\nThe created table is shown below\n");printf("\nS.no \tchar \tRegno\n");for(i=0;i<k;i++)printf("\n%d\t%c\t%d\n",i,tab[i],b[i]);fflush(stdin);goto option;case 6:exit(0);}getch();}

Page 4: SSLABPROGRAM2

OUTPUT:

SYMBOL TABLE1.Create2.Insert3.Modify4.Search5.Display6.Exit

Enter your choice:1

Enter the number of entries:2

Enter the variable and value:

Ram 25Raja 35

The table after creationVariable ValueRam 25Raja 35

Enter your choice:2

Enter the variable and value:Ramesh 62

Table after creation isVariable ValueRam 25Raja 35Ramesh 62

Enter your choice:3

Enter the variable to be searched for:Ram

The current value of variable is Ram & 25

Page 5: SSLABPROGRAM2

Enter the new variable and its value:Dino 97

The table after creation:Variable ValueDino 97Raja 35Ramesh 62

Enter your choice:4

Enter the variable to be searched for:DinoVariable=Dino Value=97 Location=1

Enter your choice:5

Variable ValueDino 97Raja 35Ramesh 62

Enter your choice:6

RESULT:Thus a ‘C’ program for implementation of symbol table is written, executed

and output is verified.

Page 6: SSLABPROGRAM2

PASS ONE OF PASS TWO ASSEMBLEREX.NO: DATE :

AIM:To write a C program to implement pass one of pass two assembler.

ALGORITHM :

1. Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode2. Read the source program3. 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 counter is incremented.10.If the opcode read is RESB, the location counter value is incremented by

operand value.11.If the opcode read is BYTE, the location counter is auto incremented.12.The length of the source program is found using the location counter value.

Page 7: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>#include<string.h>void main(){char opcode[10],mnemonic[3],operand[10],label[10],code[10];int locctr,start,length;FILE *fp1,*fp2,*fp3,*fp4;clrscr();fp1=fopen("input.dat","r");fp2=fopen("symbol.dat","w");fp3=fopen("out.dat","w");fp4=fopen("optab.dat","r");fscanf(fp1,"%s%s%s",label,opcode,operand);if(strcmp(opcode,"START")==0){start=atoi(operand);locctr=start;fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);fscanf(fp1,"%s%s%s",label,opcode,operand);}elselocctr=0;while(strcmp(opcode,"END")!=0){fprintf(fp3,"%d\t",locctr);if(strcmp(label,"**")!=0)fprintf(fp2,"%s\t%d\n",label,locctr);fscanf(fp4,"%s%s",code,mnemonic);while(strcmp(code,"END")!=0){if(strcmp(opcode,code)==0){locctr+=3;break;}fscanf(fp4,"%s%s",code,mnemonic);}if(strcmp(opcode,"WORD")==0)locctr+=3;

Page 8: SSLABPROGRAM2

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("the length of a program is %d",length);fclose(fp1);fclose(fp2);fclose(fp3);fclose(fp4);getch();}

Page 9: SSLABPROGRAM2

OUTPUT:

The length of the string is:14

RESULT:Thus a ‘C’ program for implementation of pass one of pass two assembler is

written, executed and output is verified.

Page 10: SSLABPROGRAM2

PASS TWO OF PASS TWO ASSEMBLEREX.NO:DATE :

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

ALGORITHM:

1. Get the intermediate file from pass12. Use the symbol table file and choose it for two pass assembler3. Store all opcode’s assembler directives in a file4. In the intermediate file,compare the opcode with file and get the

corresponding opcode value5. Append this value with its corresponding location counter and store

label,opcode,operand,object code in a new file6. Create a file to store and generate the object program format by using the

object code generated in pass17. Display the new file with its contents as header,text and end records.

Page 11: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>void main(){char symbol[20],opcode[20],mnemonic[20],operand[20],label[20],code[20],character,add[20],objectcode[20];int locctr,flag,flag1,loc;FILE *fp1,*fp2,*fp3,*fp4;clrscr();fp1=fopen("out.dat","r");fp2=fopen("imput.dat","w");fp3=fopen("optab.dat","w");fp4=fopen("symtab.dat","r");fscanf(fp1,"%s%s%s",label,opcode,operand);if(strcmp(opcode,"START")==0){fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);}while(strcmp(opcode,"END")!=0){flag=0;rewind(fp3);fscanf(fp3,"%s%s",mnemonic,code);while(strcmp(mnemonic,"END")!=0){if((strcmp(opcode,mnemonic)==0)&&(strcmp(code,"*")!=0)){flag=1;break;}fscanf(fp3,"%s%s",mnemonic,code);}if(flag==1){flag1=0;rewind(fp4);while(!feof(fp4)){fscanf(fp4,"%s%d",symbol,&loc);

Page 12: SSLABPROGRAM2

if(strcmp(symbol,operand)==0){flag=1;break;}}if(flag1==1){itoa(loc,add,10);strcmp(objectcode,strcat(code,add));}}else if(strcmp(opcode,"BYTE")==0||strcmp(opcode,"WORD")==0){if(operand[0]=='c'||operand[0]=='x'){character=operand[2];itoa(character,add,16);strcmp(objectcode,add);}else{itoa(atoi(operand),add,10);strcmp(objectcode,add);}}elsestrcmp(objectcode,"\0");fprintf(fp2,"%d\t%s\t%s\t%s\t%s\n",locctr,label,opcode,operand,objectcode);fscanf(fp1,"%d%s%s%s\n",&locctr,label,opcode,operand);}fprintf(fp2,"\t%s\t%s\t%s\n",label,opcode,operand);close(fp1);fclose(fp2);fclose(fp3);fclose(fp4);getch();}

Page 13: SSLABPROGRAM2

OUTPUT:input.datMAIN START 2000BEGIN LDA NUM ** STA NUM2 ** LDCH CHAR1 ** STCH CHAR2NUM1 WORD 5NUM2 RESW 1CHAR1 BYTE CACHAR2 RESB 1 ** END BEGINoptab.datADD 18ADDR 90SUB 1CSUBR 94MUL 20MULR 98DIV 24DIVR 9CLDA 00LDB 68LDX 04LDCH 50STA 0CSTB 78STX 10STCH 54TIX 2CJ 3CJSUB 48RSUB 4CJEQ 30JLT 38JGT 34START *END *symtab.datBEGIN 2000NUM1 2006NUM2 2009CHAR1 2012CHAR2 2013

Page 14: SSLABPROGRAM2

out.dat MAIN START 20002000 BEGIN LDA NUM2003 ** STA NUM22006 ** LDCH CHAR12006 ** STCH CHAR22006 NUM1 WORD 52009 NUM2 RESW 12012 CHAR1 BYTE CA2013 CHAR2 RESB 12014 ** END BEGIN

RESULT:Thus a ‘C’ program for implementation of pass 1 of pass 2 assembler is

written, executed and output is verified.

Page 15: SSLABPROGRAM2

IMPLEMENTATION OF AN ASSEMBLEREX.NO:DATE :

AIM: To write a ‘C’ program for the implementation of an assembler.

ALGORITHM: 1. Open and Read the input file 2. If the input line has the opcode “START” do the following

2.1 Find if there is any operand field after “START”, initialize the LC to the operand value 2.2 Otherwise if there is no value in the operand field then LC is set to 0

3. Write the input line to the intermediate file4. Do the following steps until the opcode is END

4.1 Check the Symbol table, if the symbol is not available then enter that symbol into the SYMTAB, along with the memory address in which it is stored. Otherwise, the error message should be displayed 4.2 If there is a opcode4.2.1 If opcode is present in the OPTAB, then increment the LC by 3 andStart writing the location counter, opcode and operand fields of thecorresponding statement to the output file, along with the object code.4.2.2 If opcode is “WORD”, then increment LC by 3;4.2.3 If opcode is “BYTE”, then increment LC by 1;4.2.4 If opcode is “RESW” then increment LC by the integer equivalentof the operand value * 3;4.2.5 If opcode is “RESB”, then increment LC by the integer equivalentof the operand value4.2.6 If there is no symbol/label in the operand field, then the operandaddress is assigned as zero and it is assembled with the object code of theinstruction4.2.7 Write the processed lines in the intermediate file along with theirlocation counters

5. To find the length of the program, Subtract the starting address of the program from the final value of the LC 6. Close all the files and exit

CODING:

Page 16: SSLABPROGRAM2

#include<stdio.h>#include<conio.h>#include<string.h>void main(){char temp[20],temp1[20],ins[20],code[5];int i,j,adder,byte;FILE *f1,*f2,*f3;clrscr();f1=fopen("pro.dat","r");f2=fopen("opcode1.dat","r");f3=fopen("out.dat","w");adder=1000;while(!feof(f1)){i=0;j=0;fgets(temp,20,f1);while(temp[i]!='\n'){if(temp[i]>='A'&&temp[i]<-'Z'){temp[j]=temp[i];j++;}i++;}temp1[j]='\0';f2=fopen("opcode1.dat","r");while(!feof(f2)){fscanf(f2,"%s%d%s",ins,&byte,code);printf("\n%s%s\n Byte:%d\n Code:%s\n\n",ins,byte,code);getch();fprintf(f3,"%d\t%s\t%s",adder,temp,code);switch(byte){case 1:{fprintf(f3,"\n");break;}case 2:{fprintf(f3,"\n",temp[i-1],temp[i-2]);break;}case 3:{fprintf(f3,"\n%c%c%c%c\n",temp[i-1],temp[j-1],temp[i-4],temp[i-3]);break;}}adder=adder+byte;break;}fclose(f2);}getch();}OUTPUT:

Page 17: SSLABPROGRAM2

pro.datLDAMOV A BSTA

opcode1.datLDA 2 46MOV B A 47STA 2 77

out.dat1000 LDA 461002 MOV A B 461004 STA 461006 STA 46

RESULT:Thus the program for the implementation of an assembler is written,

executed and the output is verified.IMPLEMENTATION OF MACROPROCESSOR

Page 18: SSLABPROGRAM2

EX.NO:DATE: AIM:

To write a ‘C’program for the implementation of a macroprocessor.

ALGORITHM:

1. Start the macro processor program2. Include the necessary header files and variable3. Open the three files f1=source.dat with read privilege,f2=mac.dat with write

privilege and f3= macent.dat with write privilege4. Get the variable form f1 file macin.dat for label,opcode,operand5. Read the variable until the opcode is not is equal to zero Then check if the

opcode is equal to Macro 6. if Macro Then Copy macroname=label7. Get the variable label ,opcode ,operand8. In these if condition perform the while loop until opcode is not equal to

MEND Copy the variable9. else if opcode is equal to macro name10. Perform the for loop from 0 to length11. Finally terminate the program

CODING:

Page 19: SSLABPROGRAM2

#include<stdio.h>#include<conio.h>#include<string.h>FILE*inpt,*inpt1,*oupt;char mac_name[20],mac_val[20],tok_wrd[20],mac_wrd[20];int replaced=0;void main(){clrscr();inpt=fopen("source.c","r");inpt1=fopen("mac.c","r");oupt=fopen("macent.txt","w");while(!feof(inpt)){fscanf(inpt,"%s",&tok_wrd);rewind(inpt1);while(!feof(inpt1)){fscanf(inpt1,"%s",&mac_wrd);if(strcmp(tok_wrd,mac_val)==0){fscanf(inpt1,"%s",&mac_val);fprintf(oupt," ");printf("%s",mac_val);replaced=1;}}if(replaced==0){fprintf(oupt,tok_wrd);fprintf(oupt," ");printf("\n %s",tok_wrd);}replaced=0;}getch();}

OUTPUT:

Page 20: SSLABPROGRAM2

SOURCE.C

#include<stdio.h>#include<conio.h>void main(){clrscr();temp=a+b;temp=a/b;getch();}

MAC.C

#define a 10#define b 20

MACENT.txt

#include<stdio.h>#include<conio.h> void main() { clrscr(); temp=10+20; temp=10/20; getch(); }

RESULT:Thus the program for the implementation of a macroprocessor is written,

executed and the output is verified.

Page 21: SSLABPROGRAM2

ABSOLUTE LOADER:EX.NO:DATE:

AIM:To implement the Absolute Loader using ‘c’ language

ALGORITHM:

1. Read the Loader record 2. Verify program name and length3. Read first text record from the input file4. Process the following steps until an end record is reached5. If Object code is in character form convert in to internal hexadecimal

representation6. Move object codes to specified locations in memory7. Write the starting location counter value of a blocks of object code and the

corresponding internal representation to the output file 8. Read next record from the input file9. Go to the address specified in end record10.Close all the files and edit

Page 22: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>struct object_code{int locctr;char byte[5];};struct object_code code[200];void main(){FILE *fp1, *fp2;char input[15];int i, len, n=0, count=0, inc=0, textloc, tlen, tloc=0, num=0, loc;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(fp1, "%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; i<len; i++){code[inc].byte[num++] = input[i];if(num>1){code[inc].locctr=loc;

Page 23: SSLABPROGRAM2

loc++;inc++;num=0;}}}fscanf(fp1, "%s", input);}n=0;i=0;count=0;fprintf(fp2, "%x\t", code[i].locctr);for(i=0; i<inc; i++){fprintf(fp2, "%s", code[i].byte);n++;if(n>3){fprintf(fp2, "\t");n=0;count++;}if(count>3){fprintf(fp2, "\n%x\t", code[i+1].locctr);count=0;}}fclose(fp1);fclose(fp2);getch();}

Page 24: SSLABPROGRAM2

OUTPUT:

lout.datH COPYEE 002000 00107AT 002000 1E 142023 483039 102036 282030 302015 483061 3C2003 00202AT 00201E 15 2C2036 483061 182033 4C0000 454F46 200003 100000T 002039 1E 242030 302030 E0305D 30303F D8305D 222030 303057 53A039 2C305E 38303FT 002057 0A 102036 4C0000 F1 201000T 002071 19 342030 E43079 303064 4FA039 DC3079 2C3036 383064 4C0000E 002000

loadout.dat2000 14202348 30391020 36282030 302015482010 30613C20 0300202A 2C203648 306118202020 334C0000 454F4620 00031000 00xxxxxx2030 xxxxxx24 20303020 30E0305D 30303FD82040 305D2220 30303057 53A0392C 305E38302050 3F102036 4C0000F1 201000xx xxxxxxxx2060 xxxxxxxx xxxxxxxx xxxxxx34 2030E4302070 79303064 4FA039DC 30792C30 363830642080 4C0000

RESULT:Thus a ‘C’ program for implementation of Absolute loader is written,

executed and output is verified.

Page 25: SSLABPROGRAM2

IMPLEMENTATION OF A HASHINGEX.NO:DATE :

AIM: To write a ‘C’program for the implementation of a hashing

ALGORITHM:

1. Start the program2. Declare all the required header files and variables3. Declare the structure SYMB with the variables ADD,LABEL for SY[11]4. Define the main() and the function search()5. Perform the symbol table operations using hashing6. Search the symbol in the symbol table if it is present display the location7. Else display the symbol is not present8. Stop the program

Page 26: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>#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();for(i=0;i<MAX;i++)a[i]=0;do{printf("\n enter your choice 1.create a symbol table, 2.search in the symbol table\n");scanf("%d",&ch);switch(ch){case 1:do{printf("\n enter the address:");scanf("%d",&num);key=create(num);printf("enter the label:");scanf("%s",l);lprob(a,key,num);printf("\n continue(Y/N)?");ans=getch();}while(ans=='Y');display(a);break;case 2:

Page 27: SSLABPROGRAM2

search();break;}}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){if(a[i]!=0)count++;i++;}if(count==MAX){printf("\n hash 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);

Page 28: SSLABPROGRAM2

break;}}}void display(int a[MAX]){FILE *fp;int i;fp=fopen("symbol.txt","w");printf("\n the symbol table is ");printf("\n the hash values address label");for(i=0;i<MAX;i++){printf("\n %d \t%d \t %s",i,sy[i].add,sy[i].label);//printf(fp1,"\n %d%d%s",i,sy[i].add,sy[i].label);}fclose(fp);}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("\n the label is %s ..is present in the symbol table at address %d \n",la, s);elseprintf("\n the label is not present int the symbol table \n");}

Page 29: SSLABPROGRAM2

OUTPUT:

Enter your choice 1. create a symbol table 2. search in the symbol1

Enter the address:2010Enter the label:@

continue(y/n)?

Enter the address:2009Enter the label:!

continue(y/n)?the symbol table ishash values address label0 01 0 2 03 04 05 06 07 2009 !8 2010 @9 010 0

Enter your choice 1. create a symbol table 2. search in the symbol2

Enter the label:@

the label is @ ..is present in the symbol table at address 2010

Enter your choice 1. create a symbol table 2. search in the symbol

RESULT: Thus the program for the implementation of hashing is written, executed and the output is verified.

Page 30: SSLABPROGRAM2

PASS 1 OF DIRECT LINKING LOADEREX.NO:DATE :

AIM:To write a c program to Implement pass two of direct linking loader

ALGORITHM:

1. Enter the location where the program has to be loaded 2. Assign the address got from the user as the first control section address 3. Read the header record of the control section a. From the details of the header read and store the control section length in a variable b. Enter the control section name with its address into the external symbol table 4. 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 until the END record is reached5. Assign the starting address of next control section as the address of the current control section plus the length of the control section 6. Repeat the process from step 3 to 5 until there is no more records

Page 31: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>#include<string.h>void main(){FILE *stream;char ch,prg[500],prg1[500];int i,pos;pos=0;clrscr();stream=fopen("code.txt","r");do{ch=fgetc(stream);prg[pos]=ch;pos++;}while(ch!=EOF);printf("%s\n",prg);for(i=0;i<=pos;i=i+2){printf("%u\t%c\n%u\t%c",&prg[i],prg[i],&prg[i+1],prg[i+1]);printf("\n");}getch();}

Page 32: SSLABPROGRAM2

OUTPUT: CODE.TXT

05071A1C

65022 065023 565024

65025 065026 765027

65028 16502965030 A65031

65032 16503365034 C65035

6503665037

RESULT: Thus the program for the implementation of pass one of a dynamic linking loader is written, executed and the output is verified.

Page 33: SSLABPROGRAM2

PASS 2 OF DIRECT LINKING LOADEREX.NO:DATE :

AIM:To write a c program to Implement pass two of direct linking loader

ALGORITHM:

1. Enter the location where the program has to be loaded 2. Assign the address got from the user as the first control section address 3. Read the header record of the control section i. From the details of the header read and store the control section length in a variable ii. Enter the control section name with its address into the external symbol table 4. 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 until the END record is reached5. Assign the starting address of next control section as the address of the current control section plus the length of the control section 6. Repeat the process from step 3 to 5 until there is no more records

Page 34: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>struct tex{char type;int stadd;int nob;char arr[50];}tt[5];void main(){FILE *f1,*f2;int i=0,j,lc,n,d;clrscr();f1=fopen("in.dat","r");f2=fopen("out.dat","w");while(!feof(f1)){switch(fgetc(f1)){case 'H':break;case 'T':if(i<1){fscanf(f1,"%x%x%s",&tt[i].stadd,&tt[i].nob,&tt[i].arr);lc=tt[i].stadd;tt[i].arr[tt[i].stadd+tt[i].nob]='\0';j=0;while(tt[i].arr[j]!='\0'){fprintf(f2,"%x\t%c%c\n",lc,tt[i].arr[j],tt[i].arr[j+1]);j+=2;lc++;}i++;}else{fscanf(f1,"%x%x%s",&tt[i].stadd,&tt[i].nob,&tt[i].arr);n=tt[i].stadd+tt[i].nob;d=tt[i].stadd-n;for(j=0;j<d;j++){fprintf(f2,"%x\t%c%c ",lc,tt[i].arr[j],tt[i].arr[j+1]);i+=2;lc++;}}case 'E':break;}}fcloseall();getch();}

Page 35: SSLABPROGRAM2

OUTPUT:

in.datH COPY 2400 15T 2300 04 04766790T 2300 04 05587643E 3310 02 1123

out.dat2300 042301 762302 672303 90

RESULT:Thus a ‘C’ progrom for implementation pass two direct linking loader is

written, executed and output is verified.

Page 36: SSLABPROGRAM2

IMPLEMENTATION OF RELOCATION LOADEREX.NO:DATE:

AIM:To write a c program to Perform the Relocation Loader

ALGORITHM:1. Start the program2. Include the necessary header file and variable

3. Open the two file for fp1= relinput.dat and give read fp2= reloutput.dat and give write

4. Read the content Using while loop perform the loop until character is not equal to E

5. If the character is H

6. Get the variable add, length, and input

7. Else if the character is T

8. Get the variable address and bitmask

9. And perform the for loop for starting zero to up to len

10.Get the opcode ,addr and assign relocbit to bitmask

11.If relocabit is zero Then actualadd=addr; else

12.Add the addr and star value

13.Finally terminate the program

Page 37: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>struct object_code{int locctr;char add[10];};struct object_code code[500];void main(){char input[100][16],output[100][16],binary[20],address[20],stloc[4];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("RLOADIN.DAT","r");fp2=fopen("RLOADOUT.DAT","w");rewind(fp1);rewind(fp2);printf("\nEnter the location where the program has to be loaded:");scanf("%s",stloc);start=atoi(stloc);location=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]);

Page 38: SSLABPROGRAM2

}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;i<(textloc-(tloc+tlen));n++){strcpy(code[inc].add,"xx");code[inc++].locctr=location++;}tlen=atoi(output[i-1]);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]='\o';num++;

Page 39: SSLABPROGRAM2

}loc=atoi(address);loc=loc+start;itoa(loc,address,10);strcat(output[i],address);}k++;len=strlen(output[i]);num=0;for(n=0;n<len;n++){code[inc].add[num++]=output[i][n];if(num>1){code[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",code[n].locctr);for(n=0;n<inc;n++){fprintf(fp2,"%s",code[n].add);i++;if(i>3){fprintf(fp2,"\t");i=0;count++;}if(count>3){

Page 40: SSLABPROGRAM2

fprintf(fp2,"\n%d\t",code[n+1].locctr);count=0;}}fclose(fp1);fclose(fp2);getch();}OUTPUT:

RLOADIN.DAT

H COPY 000000 001073T 000000 10 00F 140033 481039 100036 280030

300015 481061 311003 200030211033 200033

T 000011 19 02D 412036 481061 380033 412000454196 100003 200000

T 000031 15 087 140030 430030 141013 301044241064 210030 301057 543039212064 381045

T 000058 05 038 100036 520000 151000 301000T 000065 19 050 340030 141079 301064 503039

152079 220036 381064 43000025

E 000000

RLOADOUT.DAT

3000 14003348 10391000 36280030 300015483016 10613110 03200030 21103320 003341203032 36481061 38003341 20004541 961000033048 20000014 00304300 30141013 301044243064 10642100 30301057 54303921 206438103080 45100036 52000015 10003010 003400303096 14107930 10645030 39152079 220036383112 10644300 0025

Page 41: SSLABPROGRAM2

RESULT:Thus a ‘C’ pogram for implementation of relocation loader is written,

executed and output is verified.TEXT EDITOR

EX.NO:DATE :

AIM:To implements a simple Text Editor with features like insertion/deletin of a

character, word and sentences.

ALGORITHM:

1. Read the integer variable as cur x,cur y2. Assign as cur x->where x and cur y->where y3. Read the Text color and Text background color4. Print the text editor and integer values as cur x, cur y5. Read the character as c6. Using while condition the character c is assign as c<-getch()7. Using switch condition the c will be pass 8. Case loop the curx+1 and cur y9. Using if loop the condition is check10. Read the break condition11. Terminate the program

Page 42: SSLABPROGRAM2

CODING:#include<stdio.h>#include<conio.h>#include<ctype.h>#include<dos.h>int curx,cury;void cur_pos(){curx=wherex();cury=wherey();textcolor(LIGHTRED);textcolor(YELLOW);gotoxy(35,1);cprintf("TEXT EDITOR\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");gotoxy(95,49);cprintf("\n %02d* %02d",curx,cury);gotoxy(curx,cury);//textbackground(BLUE);}void main(){char c;clrscr();cur_pos();while(c!=27){c=getch();switch(c){case 80:gotoxy(curx,cury+1);cur_pos();break;case 77:gotoxy(curx+1,cury);cur_pos();break;case 72:gotoxy(curx,cury-1);cur_pos();break;

Page 43: SSLABPROGRAM2

case 75:gotoxy(curx-1,cury);cur_pos();break;case 32:printf(" ");cur_pos();break;case 13:gotoxy(1,cury+1);cur_pos();break;case 8:printf(" ");gotoxy(curx-1,cury);cur_pos();break;default:textcolor(BLUE);if((c>=97&&c<=122)||(c>48&&c<=57))printf("%c",c);cur_pos();break;}cur_pos();}}

Page 44: SSLABPROGRAM2

OUTPUT:

1.INSERTION:

2.DELETION:

SYSTEMSOFTWARE

TEXT EDITOR

01:101

TEXT EDITOR

01:00

Page 45: SSLABPROGRAM2

RESULT:Thus a ‘C’ program for implementation of text editor is written, executed

and output is verified.