Академический Документы
Профессиональный Документы
Культура Документы
EX.NO-1
AIM:
To write a C program to implement Symbol Table for performing
insert, display, delete, search and modify.
ALGORITHM:
Start the program for performing insert, display, delete, search and
modify option in symbol table. Define the structure of the Symbol
Table.
Enter the choice for performing the operations in the symbol Table.
If the entered choice is 1, search the symbol table for the symbol
to be inserted.
If the symbol is already present, it displays Duplicate Symbol.
Else, insert the symbol and the corresponding address in the
symbol table.
If the entered choice is 2, the symbols present in the symbol table are
displayed.
If the entered choice is 3, the symbol to be deleted is searched in the
symbol table. If it is not found in the symbol table it displays Label Not
found. Else, the symbol is deleted.
If the entered choice is 5, the symbol to be modified is searched in the
symbol table. The label or address or both can be modified.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{
char var[20];
int value;
};
struct table tb[20];
Lab Manual [2010]
int I,j,n;
void create();
void modify();
int search(char variable[],int n);
void insert();
void display();
void main()
{
int ch,result=0;
char v[20];
clrscr();
while(1)
{
printf(\n OPTIONS:);
printf(\n 1.CREATION);
printf(\n 2.INSERTION);
printf(\n 3.MODIFY);
printf(\n 4.SEARCH);
printf(\n 5.DISPLAY);
printf(\n 6.EXIT);
printf(\n ENTER UR CHOICE:);
scanf(%d,&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
modify();
break;
case 4:
printf(\n ENTER THE VARIABLE TO BE SEARCHED:);
scanf(%s,v);
result=search(v,n);
if(result==0)
printf(\n VARIABLE IS NOT PRESENT);
else
printf(\n VARIABLE IS PRESENT IN LOCATION %d AND ITS VALUE
IS %d,result,tb[result].value);
break;
Lab Manual [2010]
case 5:
display();
break;
default:
exit(0);
}}}
void create()
{
printf(\n ENTER THE NUMBER OF ENTRIES:);
scanf(%d,&n);
printf(\n ENTER THE ENTRIES:);
for(i=1;i<=n;i++)
{
scanf(%s%d,tb[i].var,&tb[i].value);
for(j=1;j<I;j++)
{
if(strcmp(tb[j].var,tb[i].var)==0)
{
printf(\n VARIABLE ALREADY EXITS \n ENTER ANOTHER
VARIABLE:);
scanf(%s%d,tb[i].var,&tb[i].value);
}}}}
void insert()
{
if(n>=20)
printf(\n TABLE IS FULL SO CANT INSERT:);
else
{
n++;
printf(\n ENTER THE VARIABLE AND VALUE:);
scanf(%s%d,tb[n].var,&tb[n].value);
for(i=1;i<n;i++)
{
if(strcmp(tb[i].var,tb[n].var)==0)
{
printf(\n VARIABLE ALREADY EXITS \n ENTER ANOTHER
VARIABLE:);
scanf(%s%d,tb[n].var,&tb[n].value);
}}}}
void modify()
{
char v[20];
int r;
Lab Manual [2010]
OPTIONS:
1.CREATION
2.INSERTION
3.MODIFY
4.SEARCH
5.DISPLAY
6.EXIT
ENTER UR CHOICE:1
ENTER THE NUMBER OF ENTRIES:2
ENTER THE ENTRIES:x 2
y3
OPTIONS:
1.CREATION
2.INSERTION
3.MODIFY
4.SEARCH
5.DISPLAY
6.EXIT
ENTER UR CHOICE:2
ENTER THE VARIABLE AND VALUE: z 4
OPTIONS:
1.CREATION
2.INSERTION
3.MODIFY
4.SEARCH
5.DISPLAY
6.EXIT
ENTER UR CHOICE:5
THE ELEMENTS IN THE TABLE ARE:
x
2
y
3
z
4
OPTIONS:
1.CREATION
2.INSERTION
3.MODIFY
4.SEARCH
5.DISPLAY
6.EXIT
Lab Manual [2010]
ENTER UR CHOICE:3
ENTER THE VARIABLE TO MODIFY:x
ENTER THE NEW VARIABLE AND VALUE:w 5
OPTIONS:
1.CREATION
2.INSERTION
3.MODIFY
4.SEARCH
5.DISPLAY
6.EXIT
ENTER UR CHOICE:5
THE ELEMENTS IN THE TABLE ARE:
w 5
y
3
z
4
OPTIONS:
1.CREATION
2.INSERTION
3.MODIFY
4.SEARCH
5.DISPLAY
6.EXIT
ENTER UR CHOICE:4
ENTER THE VARIABLE TO BE SEARCHED:y
VARIABLE IS PRESENT IN LOCATION 2 AND ITS VALUE IS 3
OPTIONS:
1.CREATION
2.INSERTION
3.MODIFY
4.SEARCH
5.DISPLAY
6.EXIT
ENTER UR CHOICE:6
RESULT:
Lab Manual [2010]
Thus the program for symbol table creation was executed and the output
was verified.
EX.NO-2
AIM:
To write a C program to implement PASS ONE of a two pass
assembler.
ALGORITHM:
Open the files fp1 and fp4 in read mode and fp2 and fp3 in write
mode.
Read the source program.
If the op code read in the source program is START, the variable
location counter is initialized with the operand value.
Else the location counter is initialized to 0.
The source program is read line by line until the reach of opcode
END.
Check whether the op code read is present in the operation code
table.
If the op code is present, then the location counter is incremented
by 3.
If the op code read is WORD, the location counter is incremented
by3.
If the op code read is RESW, the operand value is multiplied by 3 and
then the location
counter is incremented.
If the op code read is RESB, the location counter value is incremented by
operand value.
If the op code read is BYTE, the location counter is auto incremented.
The length of the source program is found using the location counter
value.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
Lab Manual [2010]
#include<stdlib.h>
void main()
{
char opcode[10],mnemonic[10],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen(INPUT.DAT,r);
fp2=fopen(SYMTAB.DAT,w);
fp3=fopen(OUTPUT.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);
rewind(fp4);
fscanf(fp4,%s,mnemonic);
while(strcmp(mnemonic,END)!=0)
{
if(strcmp(opcode,mnemonic)==0)
{
locctr+=3;
break;
}
fscanf(fp4,%s,mnemonic);
}
if(strcmp(opcode,WORD)==0)
locctr+=3;
else if(strcmp(opcode,RESW)==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,RESB)==0)
Lab Manual [2010]
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(\n Length of the program is %d,length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
INPUT FILES:
INPUT.DAT
MAIN
BEGIN
**
**
**
NUM1
NUM2
CHAR1
CHAR2
**
START
5000
LDA
NUM1
STA
NUM2
STCH
CHAR1
STCH
CHAR2
WORD
5
RESW
1
BYTE
5
RESB
1
END
BEGIN
SYMBOL.DAT
BEGIN
NUM1
NUM2
CHAR1
CHAR2
5000
5012
5015
5018
5019
OUT.DAT
MAIN
BEGIN
START
5000
LDA
NUM1
**
**
**
NUM1
NUM2
CHAR1
CHAR2
STA
STCH
STCH
WORD
RESW
BYTE
RESB
NUM2
CHAR1
CHAR2
5
1
5
1
OUTPUT
Length of the program is 20
10
RESULT:
Thus the program to implement one pass of a two pass assembler was
executed successfully.
EX.NO-3
AIM:
To write a C program to implement PASS TWO of a two pass
assembler.
ALGORITHM:
Start the program.
Initialize all the variables.
Open a file
Read the content of the file
If op code is BYTE then write address, label, op code, operand into a file
Else if op code is WORD then write address, label, op code, operand into
a file
Else if op code is RESB or RESW write address, label, op code,
operand into a file.
If it is not match anything then write address, label, op code, operand into
a file
finally terminate pass two of two pass assembler.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char
symbol[20],opcode[20],mnemonic[20],code[20],operand[20],character,add[20],
objectcode[20],label[20];
Lab Manual [2010]
11
int locctr,flag,flag1,loc;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen(OUT1.DAT,r);
fp2=fopen(TWOOUT1.DAT,w);
fp3=fopen(OPTAB1.DAT,r);
fp4=fopen(SYMTAB1.DAT,r);
fscanf(fp1,%s%s%s,label,opcode,operand);
if(strcmp(opcode,START)==0)
{
fprintf(fp2,\t%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)
{
flag1=1;
break;
}
}
if(flag1==1)
{
itoa(loc,add,10);
Lab Manual [2010]
12
strcpy(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);
strcpy(objectcode,add);
}
else
{
itoa(atoi(operand),add,10);
strcpy(objectcode,add);
}
}
else
strcpy(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,&locctr,label,opcode,operand);
}
fprintf(fp2,\t%s\t%s\t%s\n,label,opcode,operand);
printf(\n Output);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
INPUT FILES:
INPUT.DAT
**
START
**
LDA
**
ADD
**
SUB
**
STA
ALPHA RESW
FIVE RESW
CHARZ WORD
Lab Manual [2010]
2000
FIVE
ALPHA
CHARZ
C1
1
1
1
13
C1
**
RESW
END
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 *
OUT.DAT
** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE CZ
2019 C1 RESB 1
2020 ** END **
Lab Manual [2010]
14
SYMTAB.DAT
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2021
OUTPUT:
The program is executed
OUTPUT FILE:
TWOOUT.DAT
2000
2003
2006
2009
2012
2015
2018
2019
**
START
2000
**
LDA FIVE 002015
**
STA ALPHA
0C2012
**
LDCH
CHARZ
502018
**
STCHC1 542021
ALPHA
RESW
1
FIVE WORD
5
5
CHARZ
BYTE
CZ 5a
C1 RESB1
**
END **
Hardware
PCs
15
RESULT:
Thus the program to implement two pass of a two pass assembler was
executed successfully.
EX.NO-4
AIM:
To write a C program to implement a one pass assembler.
ALGORITHM:
Start the program.
Initialize all the variables.
Open a file for read and write.
Read the content from the input file.
Write the object code with address to the output file.
Finally terminate the one pass assembler.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20
struct input
{
char opcode[10],mnemonic[10],operand[10],label[10];
int loc;
};
struct input table[MAX];
struct symtab
{
char sym[10];
Lab Manual [2010]
16
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[10];
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[1].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;
Lab Manual [2010]
17
j++;
}
}
rewind(fp2);
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);
}
if(strcmp(table[i].opcode,WORD)==0)
{
locctr+=3;
strcpy(table[i].mnemonic,\0);
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,\0);
table[i].loc=atoi(\0);
Lab Manual [2010]
18
}
else if(strcmp(table[i].opcode,RESB)==0)
{
locctr+=(atoi(table[i].operand));
strcpy(table[i].mnemonic,\0);
table[i].loc=atoi(\0);
}
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,i
toa(table[x].loc,add,10)));
for(x=1;x<=j;x++)
printf(%s\t%d\n,symtbl[x].sym,symtbl[x].val);
getch();
}
INPUT FILES
input.dat
**
**
**
**
**
ALPHA
FIVE
CHARZ
C1
**
START
LDA
ADD
SUB
STA
RESW
RESW
WORD
RESW
END
optab.dat
Lab Manual [2010]
19
2000
FIVE
ALPHA
CHARZ
C1
1
1
1
1
ADD
ADDR
SUB
SUBR
MUL
MULR
DIV
DIVR
LDA
LDB
LDX
LDCH
STA
STB
STX
STCH
TIX
J
JSUB
RSUB
JEQ
JLT
JGT
START
END
18
90
1C
94
20
98
24
9C
00
68
04
50
0C
78
10
54
2C
3C
48
4C
30
38
34
*
*
OUTPUT FILES
spout.dat
**
START
2000 0
LDA FIVE 002015
**
ADD ALPHA
182012
**
SUB CHARZ
1C2018
**
STA C1 0C2021
ALPHA
RESW
1
0
FIVE RESW
1
0
CHARZ
WORD
1
1
C1 RESW
1
0
**
END
0
20
RESULT:
Thus the program to implement a one pass assembler was executed
successfully.
EX.NO-5
AIM:
To write a C program to implement a one pass macro processor.
ALGORITHM:
Start the macro processor program.
Include the necessary header files and variables.
Open the three files.
Read the variable until the op code is not equal to zero.
Then check if the op code is equal to Macro if Macro
Then
Copy macro name=label
Get the variable label, op code and operand.
If op code is not equal to MEND perform the operations.
Copy the variable.
Else if op code is equal to macro name.
Perform for loop from 0 to length.
Else if it does not match.
Write label, op code, operand in to a file.
Finally terminate the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
Lab Manual [2010]
21
int n=0,I,flag=0;
char ilab[20],iopd[20],oper[20],NAMTAB[20][20];
FILE *fp1,*fp2,*DEFTAB;
clrscr();
fp1=fopen(MACROIN.DAT,r);
fp2=fopen(MACROOUT.DAT,w);
rewind(fp1);
fscanf(fp1,%s%s%s,ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,MACRO)==0)
{
strcpy(NAMTAB[n],ilab);
DEFTAB=fopen(NAMTAB[n],w);
fscanf(fp1,%s%s%s,ilab,iopd,oper);
while(strcmp(iopd,MEND)!=0)
{
fprintf(DEFTAB,%s\t%s\t%s\n,ilab,iopd,oper);
fscanf(fp1,%s%s%s,ilab,iopd,oper);
}
fclose(DEFTAB);
n++;
}
else
{
flag=0;
for(i=0;i<n;i++)
{
if(strcmp(iopd,NAMTAB[i])==0)
{
flag=1;
DEFTAB=fopen(NAMTAB[i],r);
fscanf(DEFTAB,%s%s%s,ilab,iopd,oper);
while(!feof(DEFTAB))
{
fprintf(fp2,%s\t%s\t%s\n,ilab,iopd,oper);
fscanf(DEFTAB,%s%s%s,ilab,iopd,oper);
}
break;
}
}
if(flag==0)
Lab Manual [2010]
22
fprintf(fp2,%s\t%s\t%s\n,ilab,iopd,oper);
}
fscanf(fp1,%s%s%s,ilab,iopd,oper);
}
printf(\n OUT IN MACROPROSCESSOR);
getch();
}
INPUT FILE:
MACROIN.DAT
M1
**
**
**
**
M2
**
**
**
**
M3
**
**
**
**
**
**
**
**
**
MACRO
LDA
ADD
STA
MEND
MACRO
LDA
SUB
STA
MEND
MACRO
LDA
MUL
STA
MEND
START
M3
M2
M1
END
**
N1
N2
N3
**
**
N1
N2
N4
**
**
N1
N2
N5
**
1000
**
**
**
**
OUTPUT FILE:
MACROOUT.DAT
**
**
**
**
**
START
LDA
MUL
STA
LDA
1000
N1
N2
N5
N1
23
**
**
**
**
**
SUB
STA
LDA
ADD
STA
N2
N4
N1
N2
N3
RESULT:
Thus the program to implement one pass macro processor was executed
successfully.
EX.NO-6
AIM:
To write a C program to implement a two pass macro processor.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<string.h>
FILE *f;
char var[25][10];
char s[25],str[50],v[25],tmp[25];
int j,c,i=0,l;
int t=0;
struct deftab
{
char sym[25];
char xas[25];
Lab Manual [2010]
24
}
dt[50];
void variable(void)
{
fscanf(f,%s,str);
strcpy(dt[t++].xas,str);
j=1;
i=0;
do
{
c=0;
do
{
if(str[j] !=&)
v[c++]=str[j];
j++;
}
while(str[j]!=, && str[j]!=\0);
v[c]=\0;
strcpy(var[i],v);
i++;
}
while(str[j++]!=\0);
}
int main()
{
int cnt=0,m,n,mn;
char res[25];
FILE *f1,*f2;
f=fopen(msource.dat,r);
f1=fopen(deftab.dat,w+);
f2=fopen(namtab.dat,w+);
do
{
fscanf(f,%s,str);
while(strcmp(str,MACRO))
{
if(!strcmp(str,END))
goto b;
strcpy(s,str);
fscanf(f,%s,str);
}
fprintf(f2,%s\t%d,s,t);
Lab Manual [2010]
25
strcpy(dt[t].sym,s);
variable();
fscanf(f,%s,str);
do
{
strcpy(dt[t].sym,str);
fscanf(f,%s,str);
cnt=0;
c=0;
for(l=0;l<strlen(str);l++)
{
if(str[l]==&)
{
cnt++;
m=1;
}
}
if(cnt)
{
for(l=m+1;l<strlen(str);l++)
{
tmp[c++]=str[l];
tmp[c]=\0;
for(n=0;n<i;n++)
if(!strcmp(tmp,var[n]))
{
mn=n;
goto a;
}
}
a:
c=0;
for(n=0;n<m;n++)
res[c++]=str[n];
res[c++]=?;
res[c++]=mn+49;
for(n=l+1;n<strlen(str);n++)
res[c++]=str[n];
res[c]=\0;
strcpy(dt[t++].xas,res);
}
else
{
Lab Manual [2010]
26
strcpy(dt[t++].xas,str);
}
fscanf(f,%s,str);
}
while(strcmp(str,MEND));
fprintf(f2,\t%d\n,t);
strcpy(dt[t].sym,MEND);
strcpy(dt[t++].xas, );
}
while(!feof(f));
b:
for(n=0;n<t;n++)
fprintf(f1,%s\t\t%s\n,dt[n].sym,dt[n].xas);
fclose(f);
fclose(f1);
fclose(f2);
return 0;
}
INPUT FILE
msource.dat
COPY
START 0
RDBUFF MACRO &INDEV,&BUFADR,&RECLTH
CLEAR
X
CLEAR
A
CLEAR
S
LDT #4096
TD =X&INDEV
JEQ *-3
RD =X&INDEV
COMPR
A,S
JEQ *+11
STCH&BUFADR,X
TIXR T
JLT *-19
STX &RECLTH
MEND
WRBUFF MACRO &OUTDEV,&BUFADR,&RECLTH
CLEAR
X
LDT &RECLTH
LDCH
&BUFADR,X
Lab Manual [2010]
27
TD =X&OUTDEV
TIXR T
JLT *-14
MEND
FIRST
STL RETADR
CLOOP
RDBUFF F1,BUFFER,LENGTH
LDA LENGTH
COMP
#0
JEQ ENDFIL
WRBUFF 05,BUFFER,LENGTH
J
CLOOP
ENDFIL
WRBUFF 05,EOF,THREE
J
@RETADR
EOF BYTE
CEOF
THREE
WORD
3
RETADR RESW
1
LENGTH RESW
1
BUFFER RESB4096
END FIRST
OUTPUT FILES
namtab.dat
RDBUFF
WRBUFF
0
15
14
22
deftab.dat
RDBUFF
CLEAR
CLEAR
CLEAR
LDT
TD
JEQ
RD
COMPR
JEQ
STCH
TIXR
JLT
STX
&INDEV,&BUFADR,&RECLTH
X
A
S
#4096
=?_
*-3
=?_
A,S
*+11
&?_
T
*-19
&?_
28
MEND
WRBUFF
CLEAR
LDT
LDCH
TD
TIXR
JLT
MEND
&OUTDEV,&BUFADR,&RECLTH
X
&?_
&?_
=?_
T
*-14
29
RESULT:
Thus the program to implement two pass macro processor was executed
successfully.
EX.NO-7
RELOCATION LOADER
AIM:
To write a C program to implement relocation loader.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
Lab Manual [2010]
30
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(rin.dat,r);
fp2=fopen(rout.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);
Lab Manual [2010]
31
textloc=atoi(output[i-2]);
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]=\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++)
{
obcode[inc].add[num++]=output[i][n];
if(num>1)
{
obcode[inc++].locctr=location++;
num=0;
}
}
}
Lab Manual [2010]
32
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;
}
}
getch();
}
INPUT FILE
rin.dat
H COPY 000000 001077
T 000000 ID 17202D 69202D 4B101036 032026 290000 332007 4B10105D
3F2FEC 032010
T 0000ID 13 0F20160 010003 0F200D 4B10105D 3E2003 454F46
E 000000
OUTPUT
Lab Manual [2010]
33
34
RESULT:
Thus the program to implement relocation loader was executed
successfully.
EX.NO-8
ABSOLUTE LOADER
AIM:
To write a C program to implement absolute loader.
ALGORITHM:
Start the program.
Assign the required variable
Open the files for read and write
Read the content
Using while loop perform the loop until character is not equal to E
Then compare the character is equal to H
If H then
PROGRAM:
#include<stdio.h>
Lab Manual [2010]
35
#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=0;
clrscr();
fp1=fopen("lout.dat","r");
fp2=fopen("loadout1.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;
Lab Manual [2010]
36
}
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();
}
INPUT FILE
Lab Manual [2010]
37
lout.dat
H
T
T
T
T
T
E
COPY
002000
303015
00201E
454F46
002039
08305D
002057
002071
DC3079
002000
002000
00107A
1E 142033
4830309
102036
282030
483061
3C2003
00202A
0C2039
00202D
15
2C2036
483061
182033
4C0000
200003
100000
1E 242030
302030
E0305D
30303F
303057
53A039
2C305E
38303F
0A 102036
4C0000
F1
201000
19
342030
E03079
303064
4FA0039
2C2036
383064
4C0000
15
OUTPUT FILE
loadout1.dat
2000
2010
2020
2030
2040
2050
2060
2070
14203348
830613C2
03648306
31000002
8305D303
0364C000
0644FA00
0015
30309102
00300202
11820334
42030302
05753A03
0F120100
39DC3079
38
03628203
A0C20390
C0000454
030E0305
92C305E3
0342030E
2C203638
03030154
0202D2C2
F4620000
D30303F0
8303F102
03079303
30644C00
RESULT:
Thus the program to implement absolute loader was executed
successfully.
EX.NO-9
AIM:
To write a C program to implement pass one of a direct linking
loader.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct estab
{
char csect[10];char sym_name[10];
int add,len;
Lab Manual [2010]
39
}
table[10];
void main()
{
char in[10];
int i,cnt=0,start,len,loc;
FILE *fp1,*fp2;
clrscr();
fp1=fopen(link1in.dat,r);
fp2=fopen(link1out.dat,w);
printf(\n Enter the location where the program has to be loaded);
scanf(%x,&start);
fprintf(fp2,CSECT\Tsymname\Taddress\Tlength\n);
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,%s,in);
if(strcmp(in,H)==0)
{
fscanf(fp1,%s,in);
strcpy(table[cnt].csect,in);
strcpy(table[cnt].sym_name,**);
fscanf(fp1,%s,in);
table[cnt].add=atoi(in)+start;
fscanf(fp1,%x,&len);
table[cnt++].len=len;
fscanf(fp1,%s,in);
}
if(strcmp(in,D)==0)
{
fscanf(fp1,%s%x,in,&loc);
while(strcmp(in,R)!=0)
{
strcpy(table[cnt].csect,**);
strcpy(table[cnt].sym_name,in);
table[cnt].add=loc+start;
table[cnt++].len=0;
fscanf(fp1,%s%x,in,&loc);
}
while(strcmp(in,T)!=0)
fscanf(fp1,%s,in);
}
if(strcmp(in,T)==0)
Lab Manual [2010]
40
while(strcmp(in,E)!=0)
fscanf(fp1,%s,in);
fscanf(fp1,%s,in);
start=start+len;
}
for(i=0;i<cnt;i++)
fprintf(fp2,%s\t%s\t%x\t
%x\n,table[i].csect,table[i].sym_name,table[i].add,table[i].len);
fclose(fp1);
fclose(fp2);
getch();
}
INPUT FILE:
link1in.dat
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 0201D 77100004 150014
T 000054 16 1000014 15100006 00002F 100014
FFFFCO
M 000024 05 +LISTB
M 000054 06 +LISTC
M 000058 06 +ENDC
M 000064 06 +LISTB
E 000000
H POGB 000000 000088
D LISTB 000060 ENDB 000070
R LISTA ENDA LISTC ENDC
T 000036 11 0100000 772027 05100000
T 000070 18 100000 05100006 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 OOOOOO
H PROGC 000000 000057
Lab Manual [2010]
41
RESULT:
Thus the program to implement pass one of a direct linking loader was
executed successfully.
Lab Manual [2010]
42
EX.NO-10
AIM:
To write a C program to implement pass two of a direct linking loader.
ALGORITHM:
Assign the control section address in a variable, CSADR.
Read the Header record.
From the information available in the header record read, store the control
section length in a variable.
Do the following process until an end record is reached
If the subsequent records read is a text record T, and if the object code
is in character form convert it into machine representation, and move the
object code from the record to the memory location control sections
address plus the specified address in the text record.
If the sequent records read is modification record M then search for the
modifying symbol name in the external symbol table created by pass 1 if
it is found the add, or subtract the corresponding symbol address found
with the value starting at the location (CSADD plus the specified address
in the modification record).
Add the control section length to the current control section address to the
address of the next control section, and repeat the entire process until
there is no more input.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct objectcode
{
char byte[15];
int locctr;
};
struct objectcode code[500];
void main()
{
int loc,length,tlen=0,textloc,inc=0,i,j,reloc,maddr,tloc;
int len,num=0,k,newval,value,addr,start,x;
Lab Manual [2010]
43
char input[20],newloc[20],oper[5],symbol[20],modval[20],in[20];
FILE *fp1,*fp2,*fp3;
clrscr();
fp1=fopen(INP3EX9.DAT,r);
fp2=fopen(INP4EX9.DAT,r);
fp3=fopen(OUTPUT.DAT,w);
printf(\n ENTER THE LOCATION TO BE LOADED:);
scanf(%d,&start);
fscanf(fp1,%s,input);
while(!feof(fp1))
{
if(strcmp(input,H)==0)
{
fscanf(fp1,%s,input);
fscanf(fp1,%s,input);
loc=atoi(input)+start;
tloc=loc;
fscanf(fp1,%d,&length);
fscanf(fp1,%s,input);
}
if(strcmp(input,D)==0)
{
do
{
fscanf(fp1,%s,input);
}
while(strcmp(input,T)!=0);
}
if(strcmp(input,T)==0)
{
fscanf(fp1,%d,&textloc);
textloc=textloc+start;
for(i=0;i<textloc-(tloc+tlen);i++)
{
strcpy(code[inc].byte,XX);
code[inc++].locctr=loc++;
}
fscanf(fp1,%d,&tlen);
tloc=textloc;
fscanf(fp1,%s,input);
}
if(strcmp(input,M)==0)
{
Lab Manual [2010]
44
fscanf(fp1,%s,input);
maddr=atoi(input)+start;
fscanf(fp1,%d,&value);
fscanf(fp1,%s,oper);
fscanf(fp1,%s,input);
rewind(fp2);
fscanf(fp2,%s%d,symbol,&addr);
while(!feof(fp2))
{
if(strcmp(input,symbol)==0)
{
if(strcmp(oper,+)==0)
newval=addr+value;
else
newval=addr-value;
}
fscanf(fp2,%s%d,symbol,&addr);
}
for(x=0;x<inc;x++)
{
if(code[x].locctr==maddr)
break;
}
itoa(newval,modval,10);
j=strlen(modval);
for(k=0;k<j;k++)
{
code[x].byte[num++]=modval[k];
if(num>1)
{
x++;
num=0;
}
}
fscanf(fp1,%s,input);
}
if(strcmp(input,E)==0)
{
fscanf(fp1,%s,input);
start=start+length;
}
else
{
Lab Manual [2010]
45
len=strlen(input);
j=0;
for(i=0;i<2;i++)
code[inc].byte[num++]=input[i];
if(i==2)
{
while(i!=len)
{
in[j]=input[i];
i++;
j++;
}
}
in[j]=\0;
reloc=atoi(in)+start;
itoa(reloc,newloc,10);
if(num>1)
{
code[inc].locctr=loc;
loc++;
inc++;
num=0;
}
for(k=0;k<j;k++)
{
code[inc].byte[num++]=newloc[k];
if(num>1)
{
code[inc].locctr=loc;
loc++;
inc++;
num=0;
}
}
}
fscanf(fp1,%s,input);
}
for(i=0;i<inc;i++)
fprintf(fp3,\n%d\t%s,code[i].locctr,code[i].byte);
fclose(fp1);
fclose(fp2);
fclose(fp3);
getch();
Lab Manual [2010]
46
}
INPUT FILES:
INP3EX9.DAT
H
D
R
T
M
E
PROGA
LISTA
LISTB
000000
000004
000000
000000
000006
ENDB
12
05
+
H
D
R
T
M
E
PROGB
LISTB
LISTA
000000
000001
000000
000000
000010
ENDA
12
05
-
000012
ENDA
000016
LISTB
100004
000012
ENDB
030005
LISTA
000007
770007
2006
2007
2008
2010
OUTPUT:
ENTER THE LOCATION TO BE LOADED: 2000
OUTPUT FILE:
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
00
20
16
10
20
13
15
20
14
18
47
180012
000011
INP4EX9.DAT
LISTA
ENDA
LISTB
ENDB
150014
050016
000012
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
20
12
03
20
01
77
20
19
05
20
28
00
20
24
RESULT:
Thus the program to implement pass two of a direct linking loader was
executed successfully.
48
EX.NO-11
TEXT EDITOR
AIM:
To write a C program to create a text editor.
ALGORITHM:
49
{
char c,ch;
clrscr();
printf("\n *************************");
printf("\n NEW FILE CREATION");
printf("\n *************************");
printf("\n TYPE TEXT \n TO TERMINATE PRESS TAB+ENTER KEYS");
cnt=0;
while((c=getchar())!='\t')
buff[cnt++]=c;
buff[cnt]='\0';
printf("\n *****WARNING*****");
printf("\n\t FILE IS NOT SAVED,DO YOU WANT TO SAVE?(Y/N)");
ch=getch();
if(ch=='Y'||ch=='y')
{
printf("\n ENTER THE NAME FOR FILE:");
loop:
scanf("%s",filename);
fp1=fopen(filename,"r");
if(fp1==NULL)
{
fp1=fopen(filename,"w");
fprintf(fp1,"%s",buff);
}
else
{
printf("\n FILE ALREADY EXISTS,DO YOU WANT TO OVERWRITE?
(y/n)");
ch=getch();
if(ch=='Y'||ch=='y')
{
fp1=fopen(filename,"w");
fprintf(fp1,"%s",buff);
}
else
{
printf("\n ENTER THE NEW FILE NAME:");
goto loop;
}
}
fclose(fp1);
}
Lab Manual [2010]
50
}
void openfile()
{
char c;
clrscr();
printf("\n ***********************");
printf("\n OPEN AND DISPLAY FILE");
printf("\n ***********************");
printf("\n ENTER THE FILENAME:");
loop:
scanf("%s",filename);
fp1=fopen(filename,"r");
if(fp1==NULL)
{
printf("\n FILE DOES NOT EXISTS");
printf("\n ENTER THE CORRECT FILENAME:");
goto loop;
}
else
{
printf("\n********FILE CONTENT******** \n");
while(!feof(fp1))
{
c=getc(fp1);
putchar(c);
}
getch();
}
fclose(fp1);
}
void main()
{
char ch;
while(1)
{
clrscr();
printf("\n *******************");
printf("\n TEXT EDITOR ");
printf("\n ********************");
printf("\n\tNEW(N)\t\tOPEN(O)\t\tEXIT(E)");
printf("\n\n ENTER THE OPTION YOU NEED TO PERFORM:\n");
ch=getch();
switch(ch)
Lab Manual [2010]
51
{
case'N':
case'n':newfile();
break;
case'O':
case'o':openfile();
break;
case'E':
case'e':exit(1);
break;
}
}
}
OUTPUT
*******************
TEXT EDITOR
********************
NEW(N)
OPEN(O)
EXIT(E)
EXIT(E)
52
EXIT(E)
RESULT:
Thus the program to create a text editor was executed successfully.
INDEX
53
EXP
NO
NAME OF EXPERIMENT
PAGE NO.
12
17
22
25
RELOCATION LOADER
31
ABSOLUTE LOADER
36
9
10
11
54
40
44
50