Pass1 of Direct Link Loader

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

IMPLEMENTATION OF PASS 1 OF A DIRECT LINKING LOADER

Algorithm
Begin
get PROGADDR from operating system
set CSADDR to PROGADDR {for first control section}
while not end of input do
begin
i. read next input record {Header record for control section}
ii. set CSLTH to control section length
iii. search ESTAB for control section name
iv. if found then
set error flag {duplicate external symbol}
v. else
enter control section name into ESTAB with value CSADDR
while record type 'E' do
vi. begin
read next input record
if record type = 'D' then
for each symbol in the record do
begin
i. search ESTAB for symbol name
ii. if found then
set error flag (duplicate external symbol)
iii. else
enter symbol into ESTAB with value (CSADDR + indicated address)
end {for}
end {while 'E'}
add CSLTH to CSADDR {starting address for next control section
end {while not EOF}
end {Pass 1}
Program:
#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("dlinkin.txt","r");
fp2=fopen("dlinkout.txt","w");
printf("\nEnter the location where the program has to be loaded:");
scanf("%lx",&start);
fprintf(fp2,"CSECT\tSymname\tAddress\tLength\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,"\0");
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,"\0");
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);
getch();
}





Output:
INPUT FILE:

dlinkin.txt
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 150014
T 000054 16 100014 15100006 00002F 100014 FFFFC0
M 000024 05 +LISTB
M 000054 06 +LISTC
M 000058 06 +ENDC
M 000064 06 +LISTB
E 000000

H PROGB 000000 000088
D LISTB 000060 ENDB 000070
R LISTA ENDA LISTC ENDC
T 000036 11 03100000 772027 05100000
T 000070 18 100000 05100006 05100020 05100030 100000
M 000037 05 +LISTA
M 000044 05 +ENDA
M 000070 06 +ENDA
M 000074 06 +ENDC
M 000078 06 +ENDC
M 000082 06 +ENDA
E 000000

H PROGC 000000 000057
D LISTC 000030 ENDC 000042
R LISTA ENDA LISTB ENDB
T 000018 12 03100000 77100004 05100000
T 000042 15 100030 100008 100011 100000 100000
M 000019 05 +LISTA
M 000023 05 +LISTB
M 000027 05 +ENDA
M 000048 06 +LISTA
M 000051 06 +ENDA
M 000054 06 +LISTB
E 000000

OUTPUT FILE:
dlinkout.txt

You might also like