Implementation of Pass ONE of Linking Loader


/*PASS ONE OF LINKING LOADER*/

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

void main()

{

FILE *in,*es,*address;

int progaddr,csaddr,cslth,value;

char type[10],addr[10],obj[10],mflag[10];

clrscr();

printf(“Enter the starting address(OS)\n”);

scanf(“%d”,&progaddr);

csaddr=progaddr;

in=fopen(“OBJECT.TXT”,”r”);

es=fopen(“ESTAB.TXT”,”w”);

address=fopen(“ADDRESS.TXT”,”w”);

fprintf(address,”%d”,progaddr);

fclose(address);

fscanf(in,”%s\t%s\t%s\t%s”,type,addr,obj,mflag);

while(!feof(in))

{

cslth=atoi(obj);

fprintf(es,”%s\t-\t%d\t%s\n”,addr,csaddr,obj);

do

{

fscanf(in,”%s\t%s\t%s\t%s”,type,addr,obj,mflag);

if(strcmp(type,”D”)==0)

{

value=csaddr+atoi(obj);

fprintf(es,”-\t%s\t%d\t-\n”,addr,value);

}

}while(strcmp(type,”E”)!=0);

csaddr=csaddr+cslth;

fscanf(in,”%s\t%s\t%s\t%s”,type,addr,obj,mflag);

}

fclose(in);

fclose(es);

getch();

}

OUTPUT:

Enter the starting address(OS)

4000

ESTAB.TXT

PROGA          -           4000    9

-                       A1       4006    -

-                       A2       4009    -

PROGB          -           4009    9

-                       B1       4015    -

-                       B2       4018    -

Program for Relocation Loader


Relocation Loader

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

struct object_code

{          int locctr;

char add[10];

}obcode[300];

void main()

{          char input[100][16], output[100][16], binary[20], address[20], stloc[10];

int len, bitmask, loc, tlen=0, tloc, textloc, i=0, location, j, k, count=0, start, n, num=0, inc=0;

FILE *fp1, *fp2;

clrscr();

fp1=fopen(“linput.dat”, “r”);

fp2=fopen(“loadout.dat”, “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);

strcpy(output[i], NULL);

textloc=textloc+start;

itoa(textloc, output[i-2], 10);

for(n=0; n<(textloc-(tloc+tlen)); n++)

{

strcpy(obcode[inc].add, “xx”);

obcode[inc++].locctr=location++;

}

tlen=atoi(output[i-1]);

tloc=textloc;

k=0;

}

else

{

if(binary[k]==’1′)

{

num=0;

len=strlen(output[i]);

strcpy(address, NULL);

for(j=2; j<len; j++)

{

address[num]=output[i][j];

output[i][j]=”;

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++)

{

obcode[inc].add[num++]=output[i][n];

if(num>1)

{

obcode[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;

fprintf(fp2, “%d\t”,obcode[n].locctr);

for(n=0; n<inc; n++)

{          fprintf(fp2, “%s”, obcode[n].add);

i++;

if(i>3)

{          fprintf(fp2, “\t”);

i=0;

count++;

}

if(count>3)

{          fprintf(fp2, “\n%d\t”, obcode[n+1].locctr);

count=0;

}

}

printf(“\nProgram is relocated…”);

getch();

}

Program for Pass I Direct Linking Loader


Pass ONE Direct Linking Loader

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

#define MAX 10

struct estab

{          char csect[10];

char sym_name[10];

long int add;

int length;

}table[MAX];

void main()

{          FILE *fp1, *fp2;

char input[10];

long int i, count =0, start, length, loc;

clrscr();

fp1=fopen(“linkin.dat”, “r”);

fp2=fopen(“linkout.dat”, “w”);

printf(“Enter the location where the program has to be loaded:”);

scanf(“%lx”, &start);

fprintf(fp2, “CSECT \t symname \t address \t length\n”);

rewind(fp1);

while(!feof(fp1))

{

fscanf(fp1, “%s”, input);

if(strcmp(input, “H”)==0)

{

fscanf(fp1, “%s”, input);

strcpy(table[count].csect, input);

strcpy(table[count].sym_name, “”);

fscanf(fp1, “%s”, input);

table[count].add=atoi(input)+start;

fscanf(fp1, “%s”, input);

length=atoi(input);

table[count++].length=atoi(input);

fscanf(fp1, “%s”, input);

}

if(strcmp(input, “D”)==0)

{

fscanf(fp1, “%s%lx”, input, &loc);

while((strcmp(input, “R”)!=0))

{

strcpy(table[count].csect, “”);

strcpy(table[count].sym_name, input);

table[count].add=loc+start;

table[count++].length=0;

fscanf(fp1, “%s%lx”, input, &loc);

}

while(strcmp(input, “T”)!=0)

fscanf(fp1, “%s”, input);

}

if(strcmp(input, “T”)==0)

while(strcmp(input, “E”)!=0)

fscanf(fp1, “%s”, input);

fscanf(fp1, “%s”, input);

start=start+length;

}

for(i=0; i<count; i++)

fprintf(fp2, “%s\t%s\t%lx\t%d\n”, table[i].csect, table[i].sym_name, table[i].add, table[i].length);

printf(“\nPass 1 of Direct Linking Loaded is completed”);

getch();

}

Follow

Get every new post delivered to your Inbox.

Join 71 other followers