You are on page 1of 4

EX.

NO: 9B DATE:

IMPLEMENTATION OF PASS-II OF A LINKING LOADER


AIM:
To write a c-program that illustrates the functioning of pass-II of the linking loader.

ALGORITHM:
STEP1: Assign the first CSADDR in PROGADDR. STEP2: Assign the executable address to PROGADDR. STEP3: Execute the following steps 4 ,5,6 and 7, till the end of input file is reached. STEP4: Read the header record and from the information of the header record ,store the control section length in CSLTH. STEP5: Do the following until the END record is reached. STEP 5.1: If the subsequent text record read is test record T, the object code is transferred to the specified address with the addition of current value of CSADDR. STEP 5.2: When a modification record M is encountered,the ESTAB is searched to find address of the symbol specified and this address is entered (or) subtracted from the indicated location in memory. STEP5.3: If the symbol is not present in ESTAB,then it must be undefined symbol therefore error message is displayed. STEP6: If address is present in END record, then set the external control section address to CSECT address (or) speciifed address in END record which is starting of the program. STEP7: Add the control section length to current section address to find address of next CSECT. STEP8:Jump to the address given in EXECADDR to start the execution of the loaded program begin set CSADDR to PROGADDR set EXECADDR to PROGADDR while not end of input do begin read next input record {header record} set CSLTH to control section length while record type != 'E' do begin read next input record

if record type='T' then begin if{ object code is in character form,convert into internal representation} move object code from record to location (CSADDR+specified address) end{if'T} elseif record type='M' then begin search ESTAB for modifying symbol name if found then add or subtract symbol value at location {CSADDR+specified address} else set error flag{undefined external symbol} end{if 'M'} end{while!='E'} if an address is specified {in End record} then set EXECADDR to (CSADDR+specified address) add CSLTH to CSADDR end{while not EOF} jump to location given by EXECADDR {to start execution of loaded program} end{pass2}

PROGRAM:
#include<stdio.h> #include<conio.h> #include<string.h> void main() { int progadr,adr,eslen,symadr,i,c,len,len1,k,esadr; char type[1],sym[10],sm[1],sym1[10],essym[10],csname[10]; FILE *fp1,*fp3,*fp4; clrscr(); fp1=fopen("obj.txt","r"); fp3=fopen("estab.txt","r"); fp4=fopen("op.txt","w"); printf("Enter the program Address"); scanf("%d",&progadr); i=progadr; fscanf(fp1,"%s",&type); /*Text Record*/ while(strcmp(type,"E")!=0) { fscanf(fp1,"%s",&type); if(strcmp(type,"T")==0) { fscanf(fp1,"%d%d",&adr,&len); fprintf(fp4,"\n%d\t%d",progadr+adr,len);

} } rewind(fp1); adr=0; /*Modification Record*/ fscanf(fp1,"%s",&type); while(!feof(fp1)) { fscanf(fp1,"%s",&type); if(strcmp(type,"M")==0) { fscanf(fp1,"%d%d%s",&adr,&len1,&sym); c=len1; k=ftell(fp1); rewind(fp1); fscanf(fp1,"%s",&type); while(!feof(fp1)) { fscanf(fp1,"%s",&type); if(strcmp(type,"D")==0) { fscanf(fp1,"%s%d",&sym1,&symadr); if(strcmp(sym,sym1)==0) { fprintf(fp4,"\n%d\t%d",progadr+symadr,len1); fseek(fp1,k+5,0); } else { while(!feof(fp3)) { fscanf(fp3,"%s%s%d%d",&csname,&essym, &esadr,&eslen); if(strcmp(essym,sym)==0) { fprintf(fp4,"\n%d\t%d",esadr+adr,c); } } rewind(fp3); } } } fseek(fp1,k,0); } } printf("\nOperation done"); getch(); }

INPUT: /*CONTENTS OF OBJ.TXT*/ H PROGA 00000063 D LISTA 0040 D ENDA 0054 R LISTB ENDB T 000020 75 T 000054 54 M 000024 5 LISTB E 000020 /*CONTENTS OF ESTAB.TXT*/ PROGA PROGB LISTA ENDA LISTB ENDB R 4000 4040 4054 4063 4060 4070 63 75

OUTPUT: Enter the program address: 4000 /*CONTENTS OF OP.TXT*/ RELOCATED ADDRESS 4020 4054 4084 LENGTH 75 54 5

You might also like