1. /*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 -
2. 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();
}
3. 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();
}
4. Symbol Table Generation
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct table
{
char var[10];
int value;
}t[20];
int i,j,n,flag=0;
int insert()
{
printf(“Enter the variable\n”);
scanf(“%s”, t[i].var);
if(t[i].var[0] >= ’0′ && t[i].var[0] <= ’9′)
{
printf(“The variable should start with an alphabet.\n”);
flag=1;
}
else
for(j=1;j<i;j++)
{
if(strcmp(t[i].var, t[j].var) == 0)
{
printf(“The variable already exists.\n”);
flag=1;
}
}
if(flag==1)
{
i–;
return 0;
}
printf(“Enter the value\n”);
scanf(“%d”, &t[i].value);
return 1;
}
int search(char v[])
{
int flag=0;
for(j=1; j<=i; j++)
if(strcmp(t[j].var, v)==0)
{
flag=1;
break;
}
if (flag==1)
return j;
else
return 0;
}
void modify()
{
char v[10];
int result=0;
printf(“Enter the variable to be modified..”);
scanf(“%s”, v);
result = search(v);
if(result==0)
printf(“%s does not exist in the table\n”, v);
else
{
printf(“The current value of the variable %s is %d.\n”, t[result].var, t[result].value);
printf(“Enter the new value…\n”);
scanf(“%d”, &t[result].value);
}
}
void display()
{
printf(“\t Variable \t\t Value\n”);
for(j=1;j<=i;j++)
printf(“\t%s\t\t%d\n”, t[j].var, t[j].value );
}
void main()
{
int ch, result=0;
char v[10];
clrscr();
do
{
printf(“Enter your choice: \n 1.Create\n 2.Insert\n 3.Modify\n 4.Search\n 5.Display\n 6.Exit\n”);
scanf(“%d”, &ch);
switch(ch)
{
case 1:
printf(“Enter the number of entries:”);
scanf(“%d”, &n);
for(i=1; i<=n; i++)
{
if(insert()==1)
printf(“Entry added..\n”);
else
printf(“Invalid entry… \n”);
}
printf(“The Table after creation is …\n”);
display();
break;
case 2:
if(i>=20)
printf(“Cannot Insert. Table is FULL\n”);
else
{
i++;
if(insert()==1)
printf(“Entry added..\n”);
else
printf(“Invalid entry… \n”);
}
printf(“The Table after insertion is …\n”);
display();
break;
case 3:
modify();
printf(“The Table after modification is…\n”);
display();
break;
case 4:
printf(“Enter the variable to search…”);
scanf(“%s”, v);
result=search(v);
if(result==0)
printf(“The variable does not exist in the table…\n”);
else
{
printf(“The location of the variable is %d\n”, result);
printf(“The value of %s is %d”, t[result].var , t[result].value );
}
break;
case 5:
printf(“The Symbol Table is…\n”);
display();
break;
case 6:
exit(1);
}
}while(ch<7);
getch();
}
5. Single Pass Compiler
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
struct input
{
char label[10], opcode[10], operand[10], mnemonic[5];
int loc;
};
struct input table[MAX];
struct symtab
{
char sym[10];
int f, val, ref;
};
struct symtab symtbl[MAX];
void main()
{
int f1, i=1, j=1, flag, locctr, x;
char add[10], code[10], mnemcode[5];
FILE *fp1, *fp2, *fp3;
clrscr();
fp1 = fopen(“input.dat”, “r”);
fp2 = fopen(“optab.dat”, “r”);
fp3 = fopen(“spout.dat”, “w”);
fscanf(fp1, “%s%s%s”, table[i].label, table[i].opcode, table[i].operand);
if(strcmp(table[i].opcode, “START”)==0)
{
locctr = atoi(table[i].operand);
i++;
fscanf(fp1, “%s%s%s”, table[i].label, table[i].opcode, table[i].operand);
}
else
locctr=0;
while(strcmp(table[i].opcode, “END”)!=0)
{
if(strcmp(table[i].label, “**”)!=0)
{
for(x=1; x<j; x++)
{
f1=0;
if((strcmp(symtbl[x].sym, table[i].label)==0) && (symtbl[x].f==1))
{
symtbl[x].val=locctr;
symtbl[x].f=0;
table[symtbl[x].ref].loc= locctr;
f1=1;
break;
}
}
if(f1==0)
{
strcpy(symtbl[j].sym, table[i].label);
symtbl[j].val=locctr;
symtbl[j].f=0;
j++;
}
}
fscanf(fp2, “%s%s”, code, mnemcode);
while(strcmp(code, “END”)!=0)
{
if(strcmp(table[i].opcode, code)==0)
{
strcpy(table[i].mnemonic, mnemcode);
locctr+=3;
for(x=1; x<=j; x++)
{
flag=0;
if(strcmp(table[i].operand, symtbl[x].sym)==0)
{
flag=1;
if(symtbl[x].f==0)
table[i].loc = symtbl[x].val;
break;
}
}
if(flag!=1)
{
strcpy(symtbl[j].sym, table[i].operand);
symtbl[j].f=1;
symtbl[j].ref=i;
j++;
}
}
fscanf(fp2, “%s%s”, code, mnemcode);
}
rewind(fp2);
if(strcmp(table[i].opcode, “WORD”)==0)
{
locctr+=3;
strcpy(table[i].mnemonic, ”);
table[i].loc=atoi(table[i].operand);
}
else if(strcmp(table[i].opcode, “RESW”)==0)
{
locctr+=(3*(atoi(table[i].operand)));
strcpy(table[i].mnemonic, ”);
table[i].loc=atoi(”);
}
else if(strcmp(table[i].opcode, “RESB”)==0)
{
locctr+=(atoi(table[i].operand));
strcpy(table[i].mnemonic, ”);
table[i].loc = atoi(”);
}
else if(strcmp(table[i].opcode, “BYTE”)==0)
{
++locctr;
if((table[i].operand[0]==’C')||(table[i].operand[0]==’X'))
table[i].loc=(int)table[i].operand[2];
else
table[i].loc=locctr;
}
i++;
fscanf(fp1, “%s%s%s”, table[i].label, table[i].opcode, table[i].operand);
}
for(x=1; x<=i; x++)
fprintf(fp3, “%s\t%s\t%s\t%s\n”, table[x].label, table[x].opcode, table[x].operand, strcat(table[x].mnemonic, itoa(table[x].loc, add, 10)));
for(x=1; x<=j; x++)
printf(“%s\t%d\n”, symtbl[x].sym, symtbl[x].val);
getch();
}
6. Macro Processor
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{ char n1, n=0, c1, i;
char fn[10][10], ilab[20], iopd[20], m[20][3], oper[20], opd[20];
FILE *fp1, *fp2, *p[5];
clrscr();
fp1 = fopen(“macin.dat”, “r”);
while(!feof(fp1)) {
fscanf(fp1, “%s%s%s”, ilab, iopd, oper);
if(strcmp(iopd, “macro”)==0)
n++;
}
printf(“No.of macros=%d\n”, n);
n1=n;
printf(“Enter the text filename\n”);
for(i=0;i<n;i++) {
scanf(“%s”, fn[i]);
p[i]=fopen(fn[i], “w”);
}
n=0;
rewind(fp1);
while(!feof(fp1)) {
fscanf(fp1, “%s%s%s”, ilab, iopd, oper);
if(strcmp(iopd, “macro”)==0) {
strcpy(m[n],oper);
fscanf(fp1, “%s%s%s”, ilab, iopd, oper);
while(strcmp(iopd, “mend”)!=0) {
fprintf(p[n], “%s %s %s\n”, ilab, iopd, oper);
fscanf(fp1, “%s%s%s”, ilab, iopd, oper);
}
fclose(p[n]);
n++;
}
}
for(i=0;i<n1;i++)
p[i] = fopen(fn[i],”r”);
fp2=fopen(“outm.txt”, “w”);
rewind(fp1);
fscanf(fp1, “%s%s%s”, ilab, iopd, oper);
while(!feof(fp1)) {
if(strcmp(iopd, “call”)==0) {
for(i=0; i<n1; i++) {
if(strcmp(m[i], oper)==0) {
rewind(p[i]);
fscanf(p[i], “%s%s%s”, ilab, iopd, oper);
while(!feof(p[i])) {
fprintf(fp2, “%s %s %s\n”, ilab, iopd, oper);
c1=1;
fscanf(p[i], “%s%s%s”, ilab, iopd, oper);
}
break;
}
}
}
if(c1!=1)
fprintf(fp2, “%s %s %s\n”, ilab, iopd, oper);
c1=0;
fscanf(fp1, “%s%s%s”, ilab, iopd, oper);
}
fprintf(fp2, “%s %s %s\n”, ilab, iopd, oper);
}
7. Absolute Loader
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
int locctr;
char byte[5];
};
struct object_code code[200];
void main()
{
FILE *fp1, *fp2;
char input[15];
int i, len, n=0, count=0, inc=0, textloc, tlen, tloc=0, num=0, loc;
clrscr();
fp1=fopen(“lout.dat”, “r”);
fp2=fopen(“loadout.dat”, “w”);
rewind(fp1);
rewind(fp2);
fscanf(fp1, “%s”, input);
if(strcmp(input, “H”)==0)
{
for(i=0; i<4; i++)
{
if(i==1)
fscanf(fp1, “%x”, &loc);
else
fscanf(fp1, “%s”, input);
}
}
tloc=loc;
while(strcmp(input, “E”)!=0)
{
if(strcmp(input, “T”)==0)
{
fscanf(fp1, “%x”, &textloc);
for(i=0; i<(textloc-(tloc+tlen)); i++)
{
strcpy(code[inc].byte, “xx”);
code[inc++].locctr=loc++;
}
fscanf(fp1, “%x”, &tlen);
tloc=textloc;
}
else
{
len=strlen(input);
for(i=0; i<len; i++)
{
code[inc].byte[num++] = input[i];
if(num>1)
{
code[inc].locctr=loc;
loc++;
inc++;
num=0;
}
}
}
fscanf(fp1, “%s”, input);
}
n=0;
i=0;
count=0;
fprintf(fp2, “%x\t”, code[i].locctr);
for(i=0; i<inc; i++)
{
fprintf(fp2, “%s”, code[i].byte);
n++;
if(n>3)
{
fprintf(fp2, “\t”);
n=0;
count++;
}
if(count>3)
{
fprintf(fp2, “\n%x\t”, code[i+1].locctr);
count=0;
}
}
getch();
}
1 comments:
bad
Post a Comment