Sunteți pe pagina 1din 45

SYMBOL TABLE

EX.NO:
DATE :

AIM:
To write a C program to implement Symbol Table

ALGORITHM:

1. Start the program for performing insert, display, delete, search and modify
option in symbol table
2. Define the structure of the Symbol Table
3. Enter the choice for performing the operations in the symbol Table
4. If the entered choice is 1, search the symbol table for the symbol to be
inserted.
5. If the symbol is already present, it displays "Duplicate Symbol".
6. Else, insert the symbol and the corresponding address in the symbol table.
7. If the entered choice is 2, the symbols present in the symbol table are
displayed.
8. If the entered choice is 3, the symbol to be deleted is searched in the symbol
table.
9. If it is not found in the symbol table it displays "Label Not found". Else, the
symbol is deleted.
10.If the entered choice is 5, the symbol to be modified is searched in the
symbol table.
11. The label or address or both can be modified.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0,k=0,opt,b[200],tab[200];
char ch,oz;
clrscr();
printf("\n Symbol Table Creation:");
option:
printf("\n1.Create table \n2.Insert character \n3.Modify character \n4.Search character
\n5.Display table \n6.Exit");
printf("Enter ur choice:\t");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the total no of character to be inserted in a table\t");
fflush(stdin);
scanf("%d",&k);
printf("\nTable created for %d character\t",k);
goto option;
case 2:
printf("\nEnter the character \t");
fflush(stdin);
scanf("%c",&ch);
tab[i]=((int)ch);
printf("\nEnter the reference number\t");
scanf("%d",&b[i]);
i++;
goto option;
case 3:
printf("\nEnter the character to be modify\t");
fflush(stdin);
scanf("%c",&ch);
if(k==0)
printf("\nCreate the table first");
for(i=0;i<k;i++)
{if(ch==tab[i])
j=1;
else
j=0;
if(j==1)
{
printf("\nEnter the new character to be modify:\t");
fflush(stdin);
scanf("%c",&oz);
tab[i]=((int)oz);
printf("\nCharacter modified");
goto option;
}}
case 4:
printf("\nEnter the character to be searched:\n");
fflush(stdin);
scanf("%c",&ch);
if(k==0)
printf("\nCreate the table first");
for(i=0;i<k;i++)
{
if(ch==tab[i])
j=1;
else
j=0;
if(j==1)
printf("\nCharacter found at %d position",i);
else
printf("\nCharacter not found at %d position",i);}
goto option;
case 5:
printf("\nThe created table is shown below\n");
printf("\nS.no \tchar \tRegno\n");
for(i=0;i<k;i++)
printf("\n%d\t%c\t%d\n",i,tab[i],b[i]);
fflush(stdin);
goto option;
case 6:
exit(0);
}
getch();
}
OUTPUT:

SYMBOL TABLE
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit

Enter your choice:1

Enter the number of entries:2

Enter the variable and value:

Ram 25
Raja 35

The table after creation


Variable Value
Ram 25
Raja 35

Enter your choice:2

Enter the variable and value:Ramesh 62

Table after creation is


Variable Value
Ram 25
Raja 35
Ramesh 62

Enter your choice:3

Enter the variable to be searched for:Ram

The current value of variable is Ram & 25


Enter the new variable and its value:Dino 97

The table after creation:


Variable Value
Dino 97
Raja 35
Ramesh 62

Enter your choice:4

Enter the variable to be searched for:Dino


Variable=Dino Value=97 Location=1

Enter your choice:5

Variable Value
Dino 97
Raja 35
Ramesh 62

Enter your choice:6

RESULT:
Thus a ‘C’ program for implementation of symbol table is written, executed
and output is verified.
PASS ONE OF PASS TWO ASSEMBLER
EX.NO:
DATE :

AIM:
To write a C program to implement pass one of pass two assembler.

ALGORITHM :

1. Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode
2. Read the source program
3. If the opcode read in the source program is START, the variable location
counter is initialized with the operand value.
4. Else the location counter is initialized to 0.
5. The source program is read line by line until the reach of opcode END.
6. Check whether the opcode read is present in the operation code table.
7. If the opcode is present, then the location counter is incremented by 3.
8. If the opcode read is WORD, the location counter is incremented by3.
9. If the opcode read is RESW, the operand value is multiplied by 3 and then
the location counter is incremented.
10.If the opcode read is RESB, the location counter value is incremented by
operand value.
11.If the opcode read is BYTE, the location counter is auto incremented.
12.The length of the source program is found using the location counter value.
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char opcode[10],mnemonic[3],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("input.dat","r");
fp2=fopen("symbol.dat","w");
fp3=fopen("out.dat","w");
fp4=fopen("optab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);}
else
locctr=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,locctr);
fscanf(fp4,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if(strcmp(opcode,code)==0)
{
locctr+=3;
break;
}
fscanf(fp4,"%s%s",code,mnemonic);
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
locctr+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++locctr;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);}
fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
length=locctr-start;
printf("the length of a program is %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
OUTPUT:

The length of the string is:14

RESULT:
Thus a ‘C’ program for implementation of pass one of pass two assembler is
written, executed and output is verified.
PASS TWO OF PASS TWO ASSEMBLER
EX.NO:
DATE :

AIM:
To write a c program to implement pass two of pass two assembler

ALGORITHM:

1. Get the intermediate file from pass1


2. Use the symbol table file and choose it for two pass assembler
3. Store all opcode’s assembler directives in a file
4. In the intermediate file,compare the opcode with file and get the
corresponding opcode value
5. Append this value with its corresponding location counter and store
label,opcode,operand,object code in a new file
6. Create a file to store and generate the object program format by using the
object code generated in pass1
7. Display the new file with its contents as header,text and end records.
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char
symbol[20],opcode[20],mnemonic[20],operand[20],label[20],code[20],character,add[20],
objectcode[20];
int locctr,flag,flag1,loc;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("out.dat","r");
fp2=fopen("imput.dat","w");
fp3=fopen("optab.dat","w");
fp4=fopen("symtab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);}
while(strcmp(opcode,"END")!=0)
{
flag=0;
rewind(fp3);
fscanf(fp3,"%s%s",mnemonic,code);
while(strcmp(mnemonic,"END")!=0)
{
if((strcmp(opcode,mnemonic)==0)&&(strcmp(code,"*")!=0))
{
flag=1;
break;
}
fscanf(fp3,"%s%s",mnemonic,code);}
if(flag==1)
{
flag1=0;
rewind(fp4);
while(!feof(fp4))
{
fscanf(fp4,"%s%d",symbol,&loc);
if(strcmp(symbol,operand)==0)
{
flag=1;
break;
}}
if(flag1==1)
{itoa(loc,add,10);
strcmp(objectcode,strcat(code,add));
}}
else if(strcmp(opcode,"BYTE")==0||strcmp(opcode,"WORD")==0)
{
if(operand[0]=='c'||operand[0]=='x')
{
character=operand[2];
itoa(character,add,16);
strcmp(objectcode,add);
}
else
{itoa(atoi(operand),add,10);
strcmp(objectcode,add);
}}
else
strcmp(objectcode,"\0");
fprintf(fp2,"%d\t%s\t%s\t%s\t%s\n",locctr,label,opcode,operand,objectcode);
fscanf(fp1,"%d%s%s%s\n",&locctr,label,opcode,operand);}
fprintf(fp2,"\t%s\t%s\t%s\n",label,opcode,operand);
close(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
OUTPUT:
input.dat
MAIN START 2000
BEGIN LDA NUM
** STA NUM2
** LDCH CHAR1
** STCH CHAR2
NUM1 WORD 5
NUM2 RESW 1
CHAR1 BYTE CA
CHAR2 RESB 1
** END BEGIN
optab.dat
ADD 18
ADDR 90
SUB 1C
SUBR 94
MUL 20
MULR 98
DIV 24
DIVR 9C
LDA 00
LDB 68
LDX 04
LDCH 50
STA 0C
STB 78
STX 10
STCH 54
TIX 2C
J 3C
JSUB 48
RSUB 4C
JEQ 30
JLT 38
JGT 34
START *
END *
symtab.dat
BEGIN 2000
NUM1 2006
NUM2 2009
CHAR1 2012
CHAR2 2013
out.dat
MAIN START 2000
2000 BEGIN LDA NUM
2003 ** STA NUM2
2006 ** LDCH CHAR1
2006 ** STCH CHAR2
2006 NUM1 WORD 5
2009 NUM2 RESW 1
2012 CHAR1 BYTE CA
2013 CHAR2 RESB 1
2014 ** END BEGIN

RESULT:
Thus a ‘C’ program for implementation of pass 1 of pass 2 assembler is
written, executed and output is verified.
IMPLEMENTATION OF AN ASSEMBLER
EX.NO:
DATE :

AIM:
To write a ‘C’ program for the implementation of an assembler.

ALGORITHM:

1. Open and Read the input file


2. If the input line has the opcode “START” do the following
2.1 Find if there is any operand field after “START”, initialize the LC to the operand
value
2.2 Otherwise if there is no value in the operand field then LC is set to 0
3. Write the input line to the intermediate file
4. Do the following steps until the opcode is END
4.1 Check the Symbol table, if the symbol is not available then enter that symbol into the
SYMTAB, along with the memory address in which it is stored. Otherwise, the error
message should be displayed
4.2 If there is a opcode
4.2.1 If opcode is present in the OPTAB, then increment the LC by 3 and
Start writing the location counter, opcode and operand fields of the
corresponding statement to the output file, along with the object code.
4.2.2 If opcode is “WORD”, then increment LC by 3;
4.2.3 If opcode is “BYTE”, then increment LC by 1;
4.2.4 If opcode is “RESW” then increment LC by the integer equivalent
of the operand value * 3;
4.2.5 If opcode is “RESB”, then increment LC by the integer equivalent
of the operand value
4.2.6 If there is no symbol/label in the operand field, then the operand
address is assigned as zero and it is assembled with the object code of the
instruction
4.2.7 Write the processed lines in the intermediate file along with their
location counters
5. To find the length of the program, Subtract the starting address of the program from the
final value of the LC
6. Close all the files and exit

CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{char temp[20],temp1[20],ins[20],code[5];
int i,j,adder,byte;
FILE *f1,*f2,*f3;
clrscr();
f1=fopen("pro.dat","r");
f2=fopen("opcode1.dat","r");
f3=fopen("out.dat","w");
adder=1000;
while(!feof(f1))
{i=0;
j=0;
fgets(temp,20,f1);
while(temp[i]!='\n')
{if(temp[i]>='A'&&temp[i]<-'Z')
{temp[j]=temp[i];
j++;}
i++;}
temp1[j]='\0';
f2=fopen("opcode1.dat","r");
while(!feof(f2))
{fscanf(f2,"%s%d%s",ins,&byte,code);
printf("\n%s%s\n Byte:%d\n Code:%s\n\n",ins,byte,code);
getch();
fprintf(f3,"%d\t%s\t%s",adder,temp,code);
switch(byte)
{case 1:
{fprintf(f3,"\n");
break;}
case 2:
{fprintf(f3,"\n",temp[i-1],temp[i-2]);
break;}
case 3:
{fprintf(f3,"\n%c%c%c%c\n",temp[i-1],temp[j-1],temp[i-4],temp[i-3]);
break;}}
adder=adder+byte;
break;}
fclose(f2);}
getch();}
OUTPUT:
pro.dat
LDA
MOV A B
STA

opcode1.dat
LDA 2 46
MOV B A 47
STA 2 77

out.dat
1000 LDA
46
1002 MOV A B
46
1004 STA
46
1006 STA
46

RESULT:
Thus the program for the implementation of an assembler is written,
executed and the output is verified.
IMPLEMENTATION OF MACROPROCESSOR
EX.NO:
DATE:

AIM:
To write a ‘C’program for the implementation of a macroprocessor.

ALGORITHM:

1. Start the macro processor program


2. Include the necessary header files and variable
3. Open the three files f1=source.dat with read privilege,f2=mac.dat with write
privilege and f3= macent.dat with write privilege
4. Get the variable form f1 file macin.dat for label,opcode,operand
5. Read the variable until the opcode is not is equal to zero Then check if the
opcode is equal to Macro
6. if Macro Then Copy macroname=label
7. Get the variable label ,opcode ,operand
8. In these if condition perform the while loop until opcode is not equal to
MEND Copy the variable
9. else if opcode is equal to macro name
10. Perform the for loop from 0 to length
11. Finally terminate the program

CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
FILE*inpt,*inpt1,*oupt;
char mac_name[20],mac_val[20],tok_wrd[20],mac_wrd[20];
int replaced=0;
void main()
{clrscr();
inpt=fopen("source.c","r");
inpt1=fopen("mac.c","r");
oupt=fopen("macent.txt","w");
while(!feof(inpt))
{fscanf(inpt,"%s",&tok_wrd);
rewind(inpt1);
while(!feof(inpt1))
{fscanf(inpt1,"%s",&mac_wrd);
if(strcmp(tok_wrd,mac_val)==0)
{fscanf(inpt1,"%s",&mac_val);
fprintf(oupt," ");
printf("%s",mac_val);
replaced=1;}}
if(replaced==0)
{fprintf(oupt,tok_wrd);
fprintf(oupt," ");
printf("\n %s",tok_wrd);}
replaced=0;}
getch();
}

OUTPUT:
SOURCE.C

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
temp=a+b;
temp=a/b;
getch();
}

MAC.C

#define a 10
#define b 20

MACENT.txt

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
temp=10+20;
temp=10/20;
getch();
}

RESULT:
Thus the program for the implementation of a macroprocessor is written,
executed and the output is verified.
ABSOLUTE LOADER:
EX.NO:
DATE:

AIM:
To implement the Absolute Loader using ‘c’ language

ALGORITHM:

1. Read the Loader record


2. Verify program name and length
3. Read first text record from the input file
4. Process the following steps until an end record is reached
5. If Object code is in character form convert in to internal hexadecimal
representation
6. Move object codes to specified locations in memory
7. Write the starting location counter value of a blocks of object code and the
corresponding internal representation to the output file
8. Read next record from the input file
9. Go to the address specified in end record
10.Close all the files and edit
CODING:
#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;}}
fclose(fp1);
fclose(fp2);
getch();
}
OUTPUT:

lout.dat
H COPYEE 002000 00107A
T 002000 1E 142023 483039 102036 282030 302015 483061 3C2003 00202A
T 00201E 15 2C2036 483061 182033 4C0000 454F46 200003 100000
T 002039 1E 242030 302030 E0305D 30303F D8305D 222030 303057 53A039 2C3
05E 38303F
T 002057 0A 102036 4C0000 F1 201000
T 002071 19 342030 E43079 303064 4FA039 DC3079 2C3036 383064 4C0000
E 002000

loadout.dat
2000 14202348 30391020 36282030 30201548
2010 30613C20 0300202A 2C203648 30611820
2020 334C0000 454F4620 00031000 00xxxxxx
2030 xxxxxx24 20303020 30E0305D 30303FD8
2040 305D2220 30303057 53A0392C 305E3830
2050 3F102036 4C0000F1 201000xx xxxxxxxx
2060 xxxxxxxx xxxxxxxx xxxxxx34 2030E430
2070 79303064 4FA039DC 30792C30 36383064
2080 4C0000

RESULT:
Thus a ‘C’ program for implementation of Absolute loader is written,
executed and output is verified.
IMPLEMENTATION OF A HASHING
EX.NO:
DATE :

AIM:
To write a ‘C’program for the implementation of a hashing

ALGORITHM:

1. Start the program


2. Declare all the required header files and variables
3. Declare the structure SYMB with the variables ADD,LABEL for SY[11]
4. Define the main() and the function search()
5. Perform the symbol table operations using hashing
6. Search the symbol in the symbol table if it is present display the location
7. Else display the symbol is not present
8. Stop the program
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 11
char l[10];
struct symb
{int add;
char label[10];
}sy[11];
void search();
void main()
{int a[MAX],num,key,i,ch;
char ans;
int create(int);
void lprob(int[],int,int);
void display(int[]);
clrscr();
for(i=0;i<MAX;i++)
a[i]=0;
do
{printf("\n enter your choice 1.create a symbol table, 2.search in the symbol
table\n");
scanf("%d",&ch);
switch(ch)
{case 1:
do
{printf("\n enter the address:");
scanf("%d",&num);
key=create(num);
printf("enter the label:");
scanf("%s",l);
lprob(a,key,num);
printf("\n continue(Y/N)?");
ans=getch();}
while(ans=='Y');
display(a);
break;
case 2:
search();
break;}}
while(ch<=2);
getch();}
int create(int num)
{int key;
key=num%11;
return key;}
void lprob(int a[MAX],int key,int num)
{int flag,i,count=0;
void display(int a[]);
flag=0;
if(a[key]==0)
{a[key]=num;
sy[key].add=num;
strcpy(sy[key].label,l);}
else
{i=0;
while(i<MAX)
{if(a[i]!=0)
count++;
i++;}
if(count==MAX)
{printf("\n hash table is full");
display(a);
getch();
exit(1);}
for(i=key+1;i<MAX;i++)
if(a[i]==0)
{a[i]=num;
flag=1;
sy[key].add=num;
strcpy(sy[key].label,l);
break;}
for(i=0;i<key&&flag==0;i++)
if(a[i]==0)
{a[i]=num;
flag=1;
sy[key].add=num;
strcpy(sy[key].label,l);
break;}}}
void display(int a[MAX])
{FILE *fp;
int i;
fp=fopen("symbol.txt","w");
printf("\n the symbol table is ");
printf("\n the hash values address label");
for(i=0;i<MAX;i++)
{printf("\n %d \t%d \t %s",i,sy[i].add,sy[i].label);
//printf(fp1,"\n %d%d%s",i,sy[i].add,sy[i].label);}
fclose(fp);}
void search()
{FILE *fp1;
char la[10];
int set=0,s;
int j,i;
printf("enter the label:");
scanf("%s",la);
fp1=fopen("symbol.txt","r");
for(i=0;i<MAX;i++)
{fscanf(fp1,"%d%d",&j,&sy[i].add);
if(sy[i].add!=0)
fscanf(fp1,"%s",sy[i].label);}
for(i=0;i<MAX;i++)
{if(sy[i].add!=0)
{if(strcmp(sy[i].label,la)==0)
{set=1;
s=sy[i].add;}}}
if(set==1)
printf("\n the label is %s ..is present in the symbol table at address %d \n",la, s);
else
printf("\n the label is not present int the symbol table \n");
}
OUTPUT:

Enter your choice 1. create a symbol table 2. search in the symbol


1

Enter the address:2010


Enter the label:@

continue(y/n)?

Enter the address:2009


Enter the label:!

continue(y/n)?
the symbol table is
hash values address label
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 2009 !
8 2010 @
9 0
10 0

Enter your choice 1. create a symbol table 2. search in the symbol


2

Enter the label:@

the label is @ ..is present in the symbol table at address 2010

Enter your choice 1. create a symbol table 2. search in the symbol

RESULT:
Thus the program for the implementation of hashing is written, executed
and the output is verified.
PASS 1 OF DIRECT LINKING LOADER
EX.NO:
DATE :

AIM:
To write a c program to Implement pass two of direct linking loader

ALGORITHM:

1. Enter the location where the program has to be loaded


2. Assign the address got from the user as the first control section address
3. Read the header record of the control section
a. From the details of the header read and store the control section length in a
variable
b. Enter the control section name with its address into the external symbol table
4. For each symbol in the subsequent ‘D’ records the symbol must be entered into
the symbol table along with its address, added along with the corresponding
control section until the END record is reached
5. Assign the starting address of next control section as the address of the current
control section plus the length of the control section
6. Repeat the process from step 3 to 5 until there is no more records
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *stream;
char ch,prg[500],prg1[500];
int i,pos;
pos=0;
clrscr();
stream=fopen("code.txt","r");
do
{
ch=fgetc(stream);
prg[pos]=ch;
pos++;
}
while(ch!=EOF);
printf("%s\n",prg);
for(i=0;i<=pos;i=i+2)
{
printf("%u\t%c\n%u\t%c",&prg[i],prg[i],&prg[i+1],prg[i+1]);
printf("\n");
}
getch();
}
OUTPUT:

CODE.TXT

05
07
1A
1C

65022 0
65023 5
65024

65025 0
65026 7
65027

65028 1
65029
65030 A
65031

65032 1
65033
65034 C
65035

65036
65037

RESULT:
Thus the program for the implementation of pass one of a dynamic
linking loader is written, executed and the output is verified.
PASS 2 OF DIRECT LINKING LOADER
EX.NO:
DATE :

AIM:
To write a c program to Implement pass two of direct linking loader

ALGORITHM:

1. Enter the location where the program has to be loaded


2. Assign the address got from the user as the first control section address
3. Read the header record of the control section
i. From the details of the header read and store the control section length in a
variable
ii. Enter the control section name with its address into the external symbol table
4. For each symbol in the subsequent ‘D’ records the symbol must be entered into
the symbol table along with its address, added along with the corresponding
control section until the END record is reached
5. Assign the starting address of next control section as the address of the current
control section plus the length of the control section
6. Repeat the process from step 3 to 5 until there is no more records
CODING:
#include<stdio.h>
#include<conio.h>
struct tex
{char type;
int stadd;
int nob;
char arr[50];
}tt[5];
void main()
{FILE *f1,*f2;
int i=0,j,lc,n,d;
clrscr();
f1=fopen("in.dat","r");
f2=fopen("out.dat","w");
while(!feof(f1))
{switch(fgetc(f1))
{case 'H':
break;
case 'T':
if(i<1)
{fscanf(f1,"%x%x%s",&tt[i].stadd,&tt[i].nob,&tt[i].arr);
lc=tt[i].stadd;
tt[i].arr[tt[i].stadd+tt[i].nob]='\0';
j=0;
while(tt[i].arr[j]!='\0')
{fprintf(f2,"%x\t%c%c\n",lc,tt[i].arr[j],tt[i].arr[j+1]);
j+=2;
lc++;}
i++;}
else
{fscanf(f1,"%x%x%s",&tt[i].stadd,&tt[i].nob,&tt[i].arr);
n=tt[i].stadd+tt[i].nob;
d=tt[i].stadd-n;
for(j=0;j<d;j++)
{fprintf(f2,"%x\t%c%c ",lc,tt[i].arr[j],tt[i].arr[j+1]);
i+=2;
lc++;}}
case 'E':
break;}}
fcloseall();
getch();
}
OUTPUT:

in.dat
H COPY 2400 15
T 2300 04 04766790
T 2300 04 05587643
E 3310 02 1123

out.dat
2300 04
2301 76
2302 67
2303 90

RESULT:
Thus a ‘C’ progrom for implementation pass two direct linking loader is
written, executed and output is verified.
IMPLEMENTATION OF RELOCATION LOADER
EX.NO:
DATE:

AIM:
To write a c program to Perform the Relocation Loader

ALGORITHM:
1. Start the program
2. Include the necessary header file and variable
3. Open the two file for fp1= relinput.dat and give read fp2= reloutput.dat and
give write
4. Read the content Using while loop perform the loop until character is not
equal to E
5. If the character is H
6. Get the variable add, length, and input
7. Else if the character is T
8. Get the variable address and bitmask
9. And perform the for loop for starting zero to up to len
10.Get the opcode ,addr and assign relocbit to bitmask
11.If relocabit is zero Then actualadd=addr; else
12.Add the addr and star value
13.Finally terminate the program
CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
int locctr;
char add[10];
};
struct object_code code[500];
void main()
{
char input[100][16],output[100][16],binary[20],address[20],stloc[4];
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("RLOADIN.DAT","r");
fp2=fopen("RLOADOUT.DAT","w");
rewind(fp1);
rewind(fp2);
printf("\nEnter the location where the program has to be loaded:");
scanf("%s",stloc);
start=atoi(stloc);
location=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=atoi(output[i-2]);
textloc=textloc+start;
itoa(textloc,output[i-2],10);
for(n=0;i<(textloc-(tloc+tlen));n++)
{
strcpy(code[inc].add,"xx");
code[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]='\o';
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++)
{
code[inc].add[num++]=output[i][n];
if(num>1)
{
code[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",code[n].locctr);
for(n=0;n<inc;n++)
{
fprintf(fp2,"%s",code[n].add);
i++;
if(i>3)
{fprintf(fp2,"\t");
i=0;
count++;}
if(count>3)
{
fprintf(fp2,"\n%d\t",code[n+1].locctr);
count=0;}}
fclose(fp1);
fclose(fp2);
getch();
}
OUTPUT:

RLOADIN.DAT

H COPY 000000 001073


T 000000 10 00F 140033 481039 100036 280030
300015 481061 311003 200030
211033 200033
T 000011 19 02D 412036 481061 380033 412000
454196 100003 200000
T 000031 15 087 140030 430030 141013 301044
241064 210030 301057 543039
212064 381045
T 000058 05 038 100036 520000 151000 301000
T 000065 19 050 340030 141079 301064 503039
152079 220036 381064 430000
25
E 000000

RLOADOUT.DAT

3000 14003348 10391000 36280030 30001548


3016 10613110 03200030 21103320 00334120
3032 36481061 38003341 20004541 96100003
3048 20000014 00304300 30141013 30104424
3064 10642100 30301057 54303921 20643810
3080 45100036 52000015 10003010 00340030
3096 14107930 10645030 39152079 22003638
3112 10644300 0025
RESULT:
Thus a ‘C’ pogram for implementation of relocation loader is written,
executed and output is verified.
TEXT EDITOR
EX.NO:
DATE :

AIM:
To implements a simple Text Editor with features like insertion/deletin of a
character, word and sentences.

ALGORITHM:

1. Read the integer variable as cur x,cur y


2. Assign as cur x->where x and cur y->where y
3. Read the Text color and Text background color
4. Print the text editor and integer values as cur x, cur y
5. Read the character as c
6. Using while condition the character c is assign as c<-getch()
7. Using switch condition the c will be pass
8. Case loop the curx+1 and cur y
9. Using if loop the condition is check
10. Read the break condition
11. Terminate the program
CODING:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<dos.h>
int curx,cury;
void cur_pos()
{
curx=wherex();
cury=wherey();
textcolor(LIGHTRED);
textcolor(YELLOW);
gotoxy(35,1);
cprintf("TEXT EDITOR\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
gotoxy(95,49);
cprintf("\n %02d* %02d",curx,cury);
gotoxy(curx,cury);
//textbackground(BLUE);}
void main()
{
char c;
clrscr();
cur_pos();
while(c!=27)
{
c=getch();
switch(c)
{case 80:
gotoxy(curx,cury+1);cur_pos();
break;
case 77:
gotoxy(curx+1,cury);cur_pos();
break;
case 72:
gotoxy(curx,cury-1);cur_pos();
break;
case 75:
gotoxy(curx-1,cury);cur_pos();
break;
case 32:
printf(" ");cur_pos();
break;
case 13:
gotoxy(1,cury+1);cur_pos();
break;
case 8:
printf(" ");gotoxy(curx-1,cury);cur_pos();
break;
default:
textcolor(BLUE);
if((c>=97&&c<=122)||(c>48&&c<=57))
printf("%c",c);
cur_pos();
break;
}
cur_pos();
}
}
OUTPUT:

1.INSERTION:

TEXT EDITOR

SYSTEMSOFTWARE

01:101

2.DELETION:

TEXT EDITOR

01:00
RESULT:
Thus a ‘C’ program for implementation of text editor is written, executed
and output is verified.

S-ar putea să vă placă și