Sunday 25 September 2011


                                PASS ONE OF A DIRECT-LINKING LOADER     

AIM:
 To write a C program to implement pass one of direct linking loader.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the program address and set the control section address.
Step 3: Read the header record of control section and set the cslth to control section length.
Step 4: Search the control section name and insert if it is not found.
Step 5: If the record is define insert each symbol in the record.
Step 6: Then add cslth to control section address.
Step 7: Stop the program.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct header
{
char text[3];
char cname[8];
int clength;
};
struct define
{
char label[10];
char address[8];
};
struct refer
{
char label[10];
};
struct object
{
char ocode[8];
};
struct csection
{
char text[3];
struct header h;
struct define d[10];
struct refer r[10];
struct object obj[10];
}csec[5];
struct exttable
{
 char label[15];
char address[10];
}et[10];
void main()
{
int n,i;
char paddr[10],csaddr[10];
int cslth;
int e=1,j,flag=0,x,y,p,w,q,o,flag1=0,f;
char temp[15];
clrscr();
printf("enter the program starting address:\n");
scanf("%s",paddr);
strcpy(csaddr,paddr);
printf("enter the number of control section");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the Header record of the Control section:\n");
printf("\nEnter text name and length");
scanf("%s%s%d",csec[i].h.text,csec[i].h.cname,&csec[i].h.clength);
strcpy(temp,csec[i].h.cname);
cslth=csec[i].h.clength;
flag=0;
for(j=1;j<=e;j++)
{
if(strcmp(et[j].label,temp)==0)
{
flag=1;
break;
}

}
if(flag==1)
{
printf("\nControl section name already exist\n");
continue;
}
else
{
strcpy(et[e].label,temp);
strcpy(et[e].address,csaddr);
e++;
o=1;
p=1;
q=1;
do
{
printf("\nEnter the text field of the record:\n");
scanf("%s",csec[i].text);
if(strcmp(csec[i].text,"D")==0)
{
printf("\nEnter the Define Record(Label,Address):\n");
scanf("%s%s",csec[i].d[p].label,csec[i].d[p].address);
strcpy(temp,csec[i].d[p].label);
flag1=0;
for(j=1;j<=e;j++)
{
if(strcmp(et[j].label,temp)==0)
{
flag1=1;
break;
}
}
if(flag1==1)
{
printf("\nLabel alresdy exists\n");
continue;
}
else
{
strcpy(et[e].label,temp);
f=atoi(csaddr)+atoi(csec[i].d[p].address);
itoa(f,et[e].address,10);
e++;
}
p++;
}
if(strcmp(csec[i].text,"T")==0)
{
printf("\nEnter the Text record (Object code):\n");
scanf("%s",csec[i].obj[o].ocode);
o++;
}
}
while(strcmp(csec[i].text,"E")!=0);
}
w=atoi(csaddr);
w=w+cslth;
itoa(w,csaddr,10);
}
printf("\nExternal Symbol Table\n");
printf("\nLabel\t\tAddress\n");
for(i=1;i<=e;i++)
{
printf("%s\t\t%s\n",et[i].label,et[i].address);
}
getch();
}

OUTPUT:

enter the program starting address:
1000
enter the number of control section2

Enter the Header record of the Control section:

Enter text name and lengthH
PROGA
100

Enter the text field of the record:
D

Enter the Define Record(Label,Address):
LISTB
0060

Enter the text field of the record:
D

Enter the Define Record(Label,Address):
ENDB
0070

Enter the text field of the record:
R

Enter the text field of the record:
ENDA

Enter the text field of the record:
T

Enter the Text record (Object code):
102030

Enter the text field of the record:
E

Enter the Header record of the Control section:

Enter text name and lengthH
PROGB
100

Enter the text field of the record:
D

Enter the Define Record(Label,Address):
LISTC
0030

Enter the text field of the record:
D

Enter the Define Record(Label,Address):
ENDC
0042

Enter the text field of the record:
E

External Symbol Table

Label           Address
PROGA           1000
LISTB           1060
ENDB            1070
PROGB           1100
LISTC           1130
ENDC            1142

Result
Thus above program executed and output verified.

0 comments:

Post a Comment