Académique Documents
Professionnel Documents
Culture Documents
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct obj_code
{
int locctr;
char add[10];
} opcode[300];
void main()
{
char input[100][10], output[100][10];
char binary[20], address[20], stloc[10];
int len, bitmask, loc, tloc, textloc, location, j, k;
int i=0, count=0, start, n, tlen=0, num=0,inc=0;
FILE *fp1,*fp2;
clrscr();
printf("\t\t\tRELOCATING LOADER\n");
printf("\t\t\t------------------\n\n");
fp1 = fopen("relodin.txt","r");
fp2 = fopen("reloadout.txt","w");
printf("Enter the location where the program has to be loaded: ");
scanf("%s", stloc);
start = atoi(stloc);
location = start;
tloc = start;
fscanf(fp1,"%s",input[i]);
while(strcmp(input[i],"T")!=0)
{
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
itoa(start,output[2],10);
while(strcmp(input[i],"E")!=0)
{
strcpy(output[i],input[i]);
if(strcmp(input[i],"T")==0)
{
for(j=0;j<3;j++)
{
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
bitmask=atoi(output[i]);
itoa(bitmask,binary,2);
textloc=atoi(output[i-2]);
textloc = textloc+start;
itoa(textloc,output[i-2],10);
for(n=0;n<(textloc-(tloc+tlen));n++)
{
strcpy(opcode[inc].add,"xx");
opcode[inc++].locctr=location++;
}
tlen=atoi(output[i-1]);
tloc=textloc;
k=0;
}
else
{
if(binary[k]=='1')
{
num=0;
len=strlen(output[i]);
for(j=2;j<len;j++)
{
address[num]=output[i][j];
output[i][j]='\0';
num++;
}
loc=atoi(address);
loc=loc+start;
itoa(loc,address,10);
strcat(output[i],address);
}
k++;
len=strlen(output[i]);
num=0;
for(n=0;n<len;n++)
{
opcode[inc].add[num++]=output[i][n];
if(num>1)
{
opcode[inc++].locctr=location++;
num=0;
}
}
}
i++;
fscanf(fp1,"%s",input[i]);
}
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
loc = atoi(input[i]);
loc = loc+start;
strcpy(output[i],itoa(loc,address,10));
count = 0;
i = 0;
n = 0;
printf("\nThe Relocated program looks like the following: \n\n");
printf("%d\t",opcode[n].locctr);
fprintf(fp2,"%d\t",opcode[n].locctr);
for(n=0;n<inc;n++)
{
fprintf(fp2,"%s",opcode[n].add);
printf("%s",opcode[n].add);
i++;
if(i>3)
{
fprintf(fp2,"\t");
printf("\t");
i=0;
count++;
}
if(count>3)
{
fprintf(fp2,"\n%d\t",opcode[n+1].locctr);
printf("\n%d\t",opcode[n+1].locctr);
count=0;
}
}
}
Input File
(File name - relodin.txt):
H
T
T
T
T
T
COPY
000000
300015
211033
000011
454196
000031
241064
212064
000058
000065
152079
25
000000
000000
10
015
001073
140033
200033
19
045
412036
15
135
140030
381045
05
056
19
080
100036
340030
481039
481061
100036
311003
280030
200030
481061
100003
430030
210030
380030
200000
141013
301057
412000
520000
141079
220036
151
301064
381064
301000
503039
430000
301044
543039
OUTPUT
RELOCATING LOADER
-------------------------------Enter the location where the program has to be loaded: 3000
The Relocated program looks like the following:
3000
3016
3032
3048
3064
3080
3096
3112
3128
14303348
10613110
50364810
03200000
44241064
381045xx
30365230
14107930
10644300
40391030
03200030
61383030
xx143030
21303030
xxxxxxxx
00154000
40645030
0025
36283030
21103320
41500045
43003014
40575460
xxxxxxxx
301000xx
39152079
30001548
0033xx41
41961030
10133010
39212064
xxxxxx10
xx343030
22003638
Result:
Thus the Relocating Loader is implemented by re-loading the object program into main
memory at operating system specified location .