C program to implement pass two of direct linking loader

AIM:

To write a C program to implement pass two of direct linking loader.

ALGORITHM:
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJfShSmc4uzJ32B_Cnh6KDE7EeSGsUpqwnmXpsZMSrMSp0wjgo7gGWZu_JIDHC_zUiWwWkPBhm3Pz_vS0k-tjHPGbd4J7sxNkEVv5q-YbMtId0PtXLQqi2n_k4Dee10jKN6F-IOkz4Wqgg/s1600/c+and+c%252B%252B+meansofmine.jpg
STEP 1: Start the program execution.
STEP 2: Find the control section length from the header record.
STEP 3: In the text record, character form is converted to machine representation.
STEP 4: Move the object code from record to memory location.
STEP 5: In the modification record, search for symbol to be modified from external table.
Found symbol address is added or subtracted with corresponding symbol address
and value at starting location.
STEP 6: Stop the program execution.

PROGRAM:

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct ext_table
{ char csect[10];
char sname[10];
int padd;
int plen;
} estab[20];
struct object_code
{ char code[15];
int add;
}
obcode[500];
void main()
{
FILE *fp1,*fp2,*fp3;
int
i,j,n=0,num=0,inc=0,count=0,record=0,pstart,exeloc,start,textloc,loc,mloc[30],textlen,ml
en[30],length,location,x,y;
long int newadd;
char *add1,operation,lbl[10],input[10],label[30][10],address[10];
clrscr();
fp1=fopen("link1.c","r");
fp2=fopen("link2.c","r");
fp3=fopen("link3.c","w");
rewind(fp1);
rewind(fp2);
rewind(fp3);
while(!feof(fp2))
{
fscanf(fp2,"%s%s%d
%d",estab[num].csect,estab[num].sname,&estab[num].padd,&estab[num].plen);
num++;
} exeloc=estab[0].padd;
loc=exeloc;
start=loc;
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,"%d",&textloc);
textloc=textloc+pstart;
for(i=0;i<(textloc-loc);i++)
{s
trcpy(obcode[inc].code,"xx");
obcode[inc++].add=start++;
}
fscanf(fp1,"%d",&textlen);
loc=textloc+textlen;
} else if(strcmp(input,"M")==0)
{
fscanf(fp1,"%d",&mloc[record]);
mloc[record]=mloc[record]+pstart;
fscanf(fp1,"%d",&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])
{s
trcat(address,obcode[location++].code);
count++;
length+=2;
}
for(i=0;i<num;i++)
if(strcmp(lbl,estab[i].sname)==0)
break;
switch(operation)
{ case '+':newadd=strtol(address,&add1,10)+(long int)estab[i].padd;
break;
case '-':newadd=strtol(address,&add1,10)-(long int)estab[i].padd;
break;
} ltoa(newadd,address,10);
x=0;
y=0;
while(count>0)
{
obcode[loc].code[x++]=address[y++];
if(x>1)
{
x=0;
loc++;
count--;
}}} count=0;
n=0;
fprintf(fp3,"%d\t",obcode[0].add);
for(i=0;i<inc;i++)
{
fprintf(fp3,"%s",obcode[i].code);
n++;
if(n>3)
{
fprintf(fp3,"%s",obcode[i].code);
n++;
if(n>3)
fprintf(fp3,"\t");
n=0;
count++;
} if(count>3)
{
fprintf(fp3,"\n%d\t",obcode[i+1].add);
count=0;
}}
getch();
}

1 comments:

Anonymous said...

Hello u there,

can you send me the output for this program

Post a Comment