Sunday 25 September 2011


                                                 RELOCATING LOADER

AIM:
To write a C program for implementing relocating loader.
ALGORITHM:
Step 1: Start the program
Step 2: Read the header record with address.
Step 3: Read the text record.
Step 4: Assemble object code by half byte per address.
Step 5: Read the record address and modify the address length.
Step 6: Assemble object code with existing object code.
Step 7: Add the relocation address with the output address.
Step 8: Print the address and object code in half byte.
Step 9: Stop the program.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct objectcode
{
char text[5];
char ocode[10];
}obj[10];
struct modify
{
int length;
char address[5];
char saddress[5];
}m[10];
struct output
{
int address;
char o1,o2;
}op[30];
void main()
{
int start=0,i1,i,k,x,j,n,z,flag,l,y,len;
clrscr();
printf("\nEnter the relocating address\n");
scanf("%d",&i1);
printf("\nEnter the Object program");
i=0;
do
{
i++;
printf("\nEnter the object code\n");
scanf("%s",obj[i].text);
scanf("%s",obj[i].ocode);
}
while(strcmp(obj[i].text,"E")!=0);
printf("\nEnter the number of modification records:");
scanf("%d",&n);
printf("\nEnter the modification record\n");
for(j=1;j<=n;j++)
{
printf("\nStarting address\tlength\tAddress to be changed\n");
scanf("%s%d%s",m[j].saddress,&m[j].length,m[j].address);
}
i1=atoi(m[j-1].address);
z=1;
for(k=1;k<i;k++)
{
if(strcmp(obj[k].text,"H")==0)
continue;
for(x=0;x<6;x+=2)
{
op[z].address=start;
op[z].o1=obj[k].ocode[x];
op[z].o2=obj[k].ocode[x+1];
start++;
z++;
}
}
printf("\nBefore Relocation\n");
printf("\nAddress\tObject code\n");
for(k=1;k<z;k++)
{
printf("%d\t%c%c\n",op[k].address,op[k].o1,op[k].o2);
}
for(k=1;k<=n;k++)
{
flag=0;
for(x=1;x<z;x++)
{
if(atoi(m[k].saddress)==op[x].address)
{
flag=1;
break;
}
}
if(flag==1)
{
l=0;
len=m[k].length;
while(len>0)
{
if(len!=1)
{
op[x].o1=m[k].address[l];
op[x].o2=m[k].address[l+1];
l+=2;
len-=2;
}
else
{
op[x].o1=m[k].address[l];
l++;
len--;
}
x++;
}
}
}
printf("\nAfter Relocation\n");
printf("\nAddress\tObject code\n");
for(k=1;k<z;k++)
{
printf("%d\t%c%c\n",i1,op[k].o1,op[k].o2);
i1++;
}
getch();
}
OUTPUT:

Enter the relocating address
1000

Enter the Object program
Enter the object code
T       102030

Enter the object code
T       102030

Enter the object code
E       -

Enter the number of modification records:2

Enter the modification record

Starting address        length  Address to be changed
0       2       40

Starting address        length  Address to be changed
3       4       1000

Before Relocation

Address Object code
0       10
1       20
2       30
3       10
4       20
5       30

After Relocation

Address Object code
1000    40
1001    20
1002    30
1003    10
1004    00
1005    30

Result
Thus above program executed and output verified.

0 comments:

Post a Comment