PASS TWO OF A DIRECT-LINKING LOADER.
AIM:
To write a C program for pass two of direct linking loader.
ALGORITHM:
Step 1: Start the program.
Step 2: Obtain input from user.
Step 3: If opcode is START, initialize location counter to the values of operand as zero.
Step 4: Check if there is a label in the table.
Step 5: If so, search in the symbol. If found set error strating symbol already exist else insert a symbol in the symbol table.
Step 6: Search the opcode table for the operand.
Step 7: Depending upon the opcode given increment the value of location counter.
Step 8: Display the input program with address.
Step 9: 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],address[8];
};
struct refer
{
char label[10];
};
struct object
{
int address;
char ocode[8];
};
struct modi
{
char address[10];
int length;
int op;
char label[15];
};
struct csection
{
char text[3];
struct header h;
struct define d[10];
struct refer r[10];
struct object obj[10];
struct modi md[10];
}csec[5];
struct cxttable
{
char label[15];
char address[10];
}et[10];
struct output
{
int address;
char ocode[8];
}op[20],op1[20];
void main()
{
int n,i;
char paddr[10],csaddr[10],exeaddr[10];
int cslth,start,strat1=0,add1;
int e=1,e1=1,j,flag=0,o,x,y,p,w,q,g,flag1=0,f,m,u,add,s1;
int y1,len,l;
char temp[15],temp1[15],str[12],ladd[15];
clrscr();
printf("\nEnter the program strating address\n");
scanf("%s",paddr);
strcpy(csaddr,paddr);
strcpy(csaddr,paddr);
printf("\nEnter the number of entries in the Ext. Symbol table\n");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
printf("\nEnter Label name and Address\n");
scanf("%s%s",et[i].label,et[i].address);
}
printf("Enter the number of control sections\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the Header record of the control section\n");
printf("Enter text name and length\n");
scanf("%s%s%d",csec[i].h.text,csec[i].h.cname,&csec[i].h.clength);
cslth=csec[i].h.clength;
o=1;
p=1;
q=1;
g=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);
p++;
}
if(strcmp(csec[i].text,"R")==0)
{
printf("\nEnter the refer record(label)\n");
scanf("%s",csec[i].r[q].label);
q++;
}
if(strcmp(csec[i].text,"T")==0)
{
printf("Enter the text record(start address& Object code)\n");
scanf("%d%s",&csec[i].obj[o].address,csec[i].obj[o].ocode);
start=csec[i].obj[o].address+atoi(csaddr);
for(x=0;x<6;x+=2)
{
op[e1].address=start;
op[e1].ocode[0]=csec[i].obj[o].ocode[x];
op[e1].ocode[1]=csec[i].obj[o].ocode[x+1];
op[e1].ocode[2]='\0';
start++;
e1++;
}
o++;
}
if(strcmp(csec[i].text,"M")==0)
{
printf("\nEnter the modification record(Saddress,Length,Operation,Label)\n");
scanf("%s%d%d%s",csec[i].md[g].address,&csec[i].md[g].length,&csec[i].md[g].op,csec[i].md[g].label);
flag=0;
strcpy(temp,csec[i].md[g].label);
for(x=1;x<=m;x++)
{
if(strcmp(et[x].label,temp)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
strcpy(ladd,et[x].address);
add=atoi(csec[i].md[g].address)+atoi(csaddr);
for(y=1;y<e1;y++)
{
if(add==op[y].address)
{
y1=y;
len=csec[i].md[g].length;
l=0;
while(len>0)
{
if(len!=1)
{
temp1[l]=op[y].ocode[0];
temp1[l+1]=op[y].ocode[1];
l=l+2;
len=len-2;
}
else
{
temp1[l]=op[y].ocode[0];
l++;
len--;
}
y++;
}
temp1[l]='\0';
if(csec[i].md[g].op==1)
{
s1=atoi(ladd)+atoi(temp1);
}
if(csec[i].md[g].op==2)
{
s1=atoi(temp1)-atoi(ladd);
}
itoa(s1,str,10);
len=strlen(str);
l=0;
u=0;
while(len>0)
{
if(len!=1)
{
op[y1].ocode[l]=str[u];
op[y1].ocode[l+1]=str[u+1];
l=0;
u=u+2;
len=len-2;
}
else
{
op[y1].ocode[l]=str[u];
l=0;
len--;
}
y1++;
}
break;
}
}
}
g++;
}
}
while(strcmp(csec[i].text,"E")!=0);
w=atoi(csaddr);
w=w+cslth;
itoa(w,csaddr,10);
}
getch();
clrscr();
printf("\nExternal Symbol table\n");
printf("\nLabel\t\tAddress\n");
for(i=1;i<=m;i++)
{
printf("%s\t\t%s\n",et[i].label,et[i].address);
}
printf("\n\nLoader Output\n");
printf("\nAddress\t\tObject code\n");
for(i=1;i<e1;i++)
{
printf("%d\t\t%s\n",op[i].address,op[i].ocode);
}
getch();
}
OUTPUT:
Enter the program strating address
1000
Enter the number of entries in the Ext. Symbol table
2