0% found this document useful (0 votes)
39 views36 pages

Program:: Er - Perumal Manimekalai College of Engineering

The document describes a C program that implements a single pass assembler. It includes function definitions to open input and output files, read the assembly code line by line, check for errors, generate object code by looking up mnemonics and symbols, and write output to files. Key steps include initializing variables, opening input and output files, reading the assembly code line by line using fscanf, checking for errors, looking up mnemonics and symbols to generate object code, and writing output to files. The program uses arrays to store mnemonics and their codes, and reads a symbol table to resolve symbols to addresses.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views36 pages

Program:: Er - Perumal Manimekalai College of Engineering

The document describes a C program that implements a single pass assembler. It includes function definitions to open input and output files, read the assembly code line by line, check for errors, generate object code by looking up mnemonics and symbols, and write output to files. Key steps include initializing variables, opening input and output files, reading the assembly code line by line using fscanf, checking for errors, looking up mnemonics and symbols to generate object code, and writing output to files. The program uses arrays to store mnemonics and their codes, and reads a symbol table to resolve symbols to addresses.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 36

ER.

PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*

C Program to implement SYMBOL TABLE

*/

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#define null 0
int size=0;
void insert();
void del();
int search(char lab[]);
void modify();
void display();
struct symbtab
{
char label[10];
int addr;
struct symbtab *next;
};
struct symbtab *first,*last;
void main()
{
int op;
int y;
char la[10];
clrscr();
do
{
printf("\nSYMBOL TABLE IMPLEMENTATION\n");
printf("1.INSERT\n");
printf("2.DISPLAY\n");
printf("3.DELETE\n");
printf("4.SEARCH\n");
printf("5.MODIFY\n");
printf("6.END\n");
printf("\nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1:
insert();
display();
break;
case 2:
display();
break;
case 3:
del();
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

display();
break;
case 4:
printf("Enter the label to be searched: ");
scanf("%s",la);
y=search(la);
if(y==1)
printf("The label is already in the symbol table\n");
else
printf("The label is not found in the symbol tablel\n");
break;
case 5:
modify();
display();
break;
case 6:
break;
}
}while(op<6);
getch();
}
void insert()
{
int n;
char l[10];
printf("Enter the label: ");
scanf("%s",l);
n=search(l);
if(n==1)
printf("The label is already in the symbol table. Duplicate cant be inserted\n");
else
{
struct symbtab *p;
p=malloc(sizeof(struct symbtab));
strcpy(p->label,l);
printf("Enter the address: ");
scanf("%d",&p->addr);
p->next=null;
if(size==0)
{
first=p;
last=p;
}
else
{
last->next=p;
last=p;
}
size++;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

}
void display()
{
int i;
struct symbtab *p;
p=first;
printf("LABEL\tADDRESS\n");
for(i=0;i<size;i++)
{
printf("%s\t%d\n",p->label,p->addr);
p=p->next;
}
}
int search(char lab[])
{
int i,flag=0;
struct symbtab *p;
p=first;
for(i=0;i<size;i++)
{
if(strcmp(p->label,lab)==0)
{
flag=1;
}
p=p->next;
}
return flag;
}
void modify()
{
char l[10],nl[10];
int add,choice,i,s;
struct symbtab *p;
p=first;
printf("What do you want to modify?\n");
printf("1.Only the label\n");
printf("2.Only the address of a particular label\n");
printf("3.Both the label and address\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the old label\n");
scanf("%s",l);
printf("Enter the new label: ");
scanf("%s",nl);
s=search(l);
if(s==0)
printf("\nNo such label");
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

else
{
for(i=0;i<size;i++)
{
if(strcmp(p->label,l)==0)
{
strcpy(p->label,nl);
}
p=p->next;
}
}
break;
case 2:
printf("Enter the label whose address is to be modified: ");
scanf("%s",l);
printf("Enter the new address: ");
scanf("%d",&add);
s=search(l);
if(s==0)
printf("\nNo such label");
else
{
for(i=0;i<size;i++)
{
if(strcmp(p->label,l)==0)
{
p->addr=add;
}
p=p->next;
}
}
break;
case 3:
printf("Enter the old label: ");
scanf("%s",l);
printf("Enter the new label: ");
scanf("%s",nl);
printf("Enter the new address: ");
scanf("%d",&add);
s=search(l);
if(s==0)
printf("\nNo such label");
else
{
for(i=0;i<size;i++)
{
if(strcmp(p->label,l)==0)
{
strcpy(p->label,nl);
p->addr=add;
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

}
p=p->next;
}
}
break;
}
}
void del()
{
int a;
char l[10];
struct symbtab *p,*q;
p=first;
printf("Enter the label to be deleted: ");
scanf("%s",l);
a=search(l);
if(a==0)
printf("Label not found\n");
else
{
if(strcmp(first->label,l)==0)
first=first->next;
else if(strcmp(last->label,l)==0)
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=null;
last=p;
}
else
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=q->next;
}
size--;
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

Output:
Symbol Table Implementation
1.Insert
2.Display
3.Delete
4.Search
5.Modify
6.End
Enter your Option: 1
Enter the Label: vijay
Enter Address: 98
Label Address
Vijay 98
Symbol Table Implementation
1.Insert
2.Display
3.Delete
4.Search
5.Modify
6.End
Enter your Option: 3
Enter the label to delete: vijay
Label Address
..
Symbol Table Implementation
1.Insert
2.Display
3.Delete
4.Search
5.Modify
6.End
Enter your Option: 6

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
C Program to implement PASS ONE OF TWO PASS ASSEMBLER*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#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("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,"%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;
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

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("\nThe length of the program is %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

INPUT.DAT:
MAIN
BEGINLDA
**
**
**
NUM1
NUM2
CHAR1
CHAR2
**

START
2000
NUM1
ADD
NUM2
LDCH
CHAR1
STCH
CHAR2
WORD 5
RESW 1
BYTE
C'A'
RESB
1
END
BEGIN

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

OPTAB.DAT:
ADD
SUB
MUL
DIV
LDA
LDB
LDX
LDCH
STA
STB
STX
STCH
J
JSUB
RSUB
JEQ
JLT
JGT
START
END

18
1C
20
24
00
68
04
50
0C
78
10
54
3C
48
4C
30
38
34
*
*

SYMTAB.DAT:
BEGIN2000
NUM1
NUM2
CHAR1
CHAR2

2012
2015
2018
2019

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
C Program to implement PASS TWO OF TWO PASS ASSEMBLER
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char a[10],ad[10],label[10],opcode[10],operand[10],mnemonic[10],symbol[10];
int i,locctr,code,add,len,actual_len;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("twoout.dat","w");
fp2=fopen("symtab.dat","r");
fp3=fopen("out.dat","r");
fp4=fopen("optab.dat","r");
fscanf(fp3,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp3,"%d%s%s%s",&locctr,label,opcode,operand);
}
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"BYTE")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t",locctr,label,opcode,operand);
len=strlen(operand);
actual_len=len-3;
for(i=2;i<(actual_len+2);i++)
{
itoa(operand[i],ad,16);
fprintf(fp1,"%s",ad);
}
fprintf(fp1,"\n");
}
else if(strcmp(opcode,"WORD")==0)
{
len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",locctr,label,opcode,operand,a);
}
else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))
{
fprintf(fp1,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
}
else
{
rewind(fp4);
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

*/

10

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

fscanf(fp4,"%s%d",mnemonic,&code);
while(strcmp(opcode,mnemonic)!=0)
fscanf(fp4,"%s%d",mnemonic,&code);
if(strcmp(operand,"**")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",locctr,label,opcode,operand,code);
}
else
{
rewind(fp2);
fscanf(fp2,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
{
fscanf(fp2,"%s%d",symbol,&add);
}
fprintf(fp1,"%d\t%s\t%s\t%s\t%d%d\n",locctr,label,opcode,operand,code,add);
}
}
fscanf(fp3,"%d%s%s%s\n",&locctr,label,opcode,operand);
}
fprintf(fp1,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
printf("FINISHED");
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

INPUT.DAT:
MAIN
BEGINLDA
**
**
**
NUM1
NUM2
CHAR1
CHAR2
**

START
2000
NUM1
ADD
NUM2
LDCH
CHAR1
STCH
CHAR2
WORD 5
RESW 1
BYTE
C'A'
RESB
1
END
BEGIN

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

11

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

OPTAB:
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
*
*

SYMTAB.DAT:
BEGIN2000
NUM1
NUM2
CHAR1
CHAR2

2012
2015
2018
2019

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

12

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
SINGLE PASS ASSEMBLER
*/
#include<stdio.h>
#include<string.h>
#define q 11//no. of mnemonics in the array A
void main()
{
int lc,ad,address,err=0;
int s,num,l,i=0,j,n=0,line=1,f=0,f1=0,t=0,ni=0,m=0,t1;
FILE *fp1,*fp2,*fp3,*fp4;
char lab[10],op[10],val[10],code[10];
char a[20]
[15]={"STA","STL","LDA","LDB","J","JEQ","J","SUB","COMP","STCH","ADD","SUB"};
char b[20][15]={"14","32","03","69","34","30","48","28","24","16","0C"};
char sym[15][10];
int symadd[15];
clrscr();
fp1=fopen("INPUT.DAT","r");
fp2=fopen("OBJFILE.DAT","w");
fp3=fopen("ERROR.DAT","w");
fp4=fopen("SYMTAB.DAT","w");
while((!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
t++;
m++;
if(strcmp(op,".")==0)
m=0;
else if(strcmp(op,"END")==0)
break;
}
t=t-1;
m--;
fclose(fp1);
fp1=fopen("INPUT.DAT","r");
fscanf(fp1,"%s\t%s\t%x",lab,op,&lc);
fprintf(fp3,"-------------------------------------\n");
fprintf(fp3,"LINE NO.\t|ERROR FOUND\n");
fprintf(fp3,"-------------------------------------");
fprintf(fp4,"SYMBOL\tADDRESS");
s=lc;
fprintf(fp2,"H^%s^00%x^%x\n",lab,lc,t*3);
fprintf(fp2,"T^00%x^",lc);
if(m>10)
fprintf(fp2,"1E");
else
fprintf(fp2,"%x",m*3);
while((op,".")!=0&&(!feof(fp1)))
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

13

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
line++;
if(strcmp(lab,"$")!=0)
{
for(i=0;i<n;i++)
{
if(strcmp(lab,sym[i])==0)
{
f=1;
break;
}
f=0;
}
if(f==0)
{
strcpy(sym[n],lab);
symadd[n]=lc;
fprintf(fp4,"\n%s\t%x",lab,lc);
n++;
}
if(f==1){
fprintf(fp3,"%d\t\t|SYMBOL ALREADY DEFINED\n",line);err++;}
}
num=atoi(val);
if(strcmp(op,"RESW")==0)
lc=lc+(num*3);
else if(strcmp(op,"RESB")==0)
lc=lc+num;
else if(strcmp(op,"BYTE")==0)
{
num=strlen(val)-3;
lc=lc+num;
for(i=2,j=0;i<strlen(val)-1;i++)
{
code[j]=val[i];
j++;
}
code[j]='\0';
fprintf(fp2,"^%s",code);
ni++;
}
else
lc=lc+3;
if(strcmp(op,".")==0)
break;
}
while(strcmp(op,"END")!=0&&(!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

14

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

line++;
if(strcmp(op,"END")==0)
break;
if((strcmp(lab,"$")!=0)&&((strcmp(op,"RESW")!=0||strcmp(op,"RESB")!=0||
strcmp(op,"WORD")!=0||strcmp(op,"BYTE")==0)))
{
for(i=0;i<n;i++)
{
if(strcmp(lab,sym[i])==0)
{
f=1;
break;
}
f=0;
}
if(f==0)
{
strcpy(sym[n],lab);
symadd[n]=lc;
fprintf(fp4,"\n%s\t%x",lab,lc);
n++;
}
else{
fprintf(fp3,"\n%d\t\t|SYMBOL ALREADY DEFINED");err++;}
}
else if(strcmp(op,"RESW")==0||strcmp(op,"RESB")==0||
strcmp(op,"WORD")==0||strcmp(op,"BYTE")==0)
fprintf(fp3,"\n%d\t\t|Declaration not allowed here",line);
if(strcmp(op,"RESW")!=0&&strcmp(op,"RESB")!=0&&strcmp(op,"WORD")!
=0&&strcmp(op,"BYTE")!=0)
{
for(i=0;i<q;i++)
{
if(strcmp(op,a[i])==0)
{
strcpy(code,b[i]);
f1=0;
break;
}
f1=1;
}
if(f1==1){
fprintf(fp3,"\n%d\t\t|WRONG OPCODE",line);err++;}
for(i=0;i<n;i++)
{
if(strcmp(val,sym[i])==0)
{
address=symadd[i];
f=0;
break;
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

15

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

}
f=1;
}
if(f){
fprintf(fp3,"\n%d\t\t|UNDEFINED SYMBOL",line);err++;}
}
if(ni<10)
{
fprintf(fp2,"^%s%x",code,address);
ni++;
}
else
{
fprintf(fp2,"T^00%x^",lc);
if(m>10)
{
fprintf(fp2,"1E");
m=m-10;
}
else
{
fprintf(fp2,"%x",m*3);
fprintf(fp2,"^%s%x",code,address);
ni=0;
}
}
lc=lc+3;
}
fprintf(fp2,"\nE^00%x",s);
fprintf(fp3,"No of errors=%d\n--------------------------------------",err);
printf("Output file:OBJCODE.DAT\nErrors are described in ERROR.DAT\nSymbol
table is in the file:SYMTAB.DAT");
getch();
fcloseall();
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

16

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

INPUT.DAT
COPY START
1000
RETADR
RESW 1
BUFFER
RESB 4
EOF BYTE C`EOF`
$
.
$
FIRST STA RETADR
$
STL BUFFER
$
J
FIRST
$
END START
ERROR.DAT
------------------------------------LINE NO.
|ERROR FOUND
-------------------------------------No of errors=0
OBJECT.DAT
H^H^00200^1e
T^00200^1e
E^00200

SYMTAB.DAT
SYMBOL
T
200
14
203
1039 206
92
209
1765 20c
11110 20f
43
212
1060 215
99
218
1000 21b

ADDRESS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

17

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
C Program to implement SINGLE PASS MACRO PROCESSOR
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int n,flag,i;
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");
n=0;
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\n",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;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

*/

18

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

}
if(flag==0)
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
}
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
getch();
}
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.DAT
**
**
**
**
**
**
**
**
**
**
**
**

START
LDA N1
MUL N2
STA N5
LDA N1
SUB N2
STA N4
LDA N1
ADD N2
STA N3
END **
END **

1000

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

19

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
C Program to implement ABSOLUTE LOADER
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char input[10];
int start,length,address;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("input.dat","r");
fp2=fopen("output.dat","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%d",&start);
fscanf(fp1,"%d",&length);
fscanf(fp1,"%s",input);
}
else if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",input);
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
else
{
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
printf("FINISHED");
getch();
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

*/

20

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

INPUT.DAT:
H 1000 232
T 1000 142033 483039 102036
T 2000 298300 230000 282030 302015
E
OUTPUT.DAT
1000
1001
1002
1003
1004
1005
1006
1007
1008
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011

14
20
33
48
30
39
10
20
36
29
83
00
23
00
00
28
20
30
30
20
15

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

21

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
C Program to implement RELOCATING LOADER
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char add[6],length[10],input[10],bitmask[12],binary[12],relocbit;
int start,len,inp,i,address,opcode,addr,actualadd;
FILE *fp1,*fp2;
clrscr();
printf("Enter the actual starting address: ");
scanf("%d",&start);
fp1=fopen("input.dat","r");
fp2=fopen("output.dat","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",add);
fscanf(fp1,"%s",length);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",bitmask);
address+=start;
len=strlen(bitmask);
for(i=0;i<len;i++)
{
fscanf(fp1,"%d",&opcode);
fscanf(fp1,"%d",&addr);
relocbit=bitmask[i];
if(relocbit=='0')
actualadd=addr;
else
actualadd=addr+start;
fprintf(fp2,"%d\t%d%d\n",address,opcode,actualadd);
address+=3;
}
fscanf(fp1,"%s",input);
}
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

*/

22

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

fclose(fp1);
fclose(fp2);
printf("FINISHED");
getch();
}
INPUT.DAT:
H 1000 200
T 1000 11001 14 1033 48 1039 90 1776 92 1765 57 1765
T 2011 11110 23 1838 43 1979 89 1060 66 1849 99 1477
E 1000
Enter the actual starting address: 7000
OUTPUT.DAT
8000
8003
8006
8009
8012
9011
9014
9017
9020
9023

148033
488039
901776
921765
578765
238838
438979
898060
668849
991477

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

23

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
C Program to implement PASS ONE OF DIRECT LINKING LOADER
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct estab
{
char csect[10];
char sym_name[10];
int add,length;
}table[10];
void main()
{
char input[10];
int i,count=0,start,length,loc;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("linkin.dat","r");
fp2=fopen("linkout.dat","w");
printf("\nEnter the location where the program has to be located: ");
scanf("%x",&start);
fprintf(fp2,"CSect\tSym_Name\tAddress\t\tLength\n\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,"%x",&length);
table[count++].length=length;
fscanf(fp1,"%s",input);
}
if(strcmp(input,"D")==0)
{
fscanf(fp1,"%s%x",input,&loc);
while(strcmp(input,"R")!=0)
{
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

*/

24

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

strcpy(table[count].csect,"**");
strcpy(table[count].sym_name,input);
table[count].add=loc+start;
table[count++].length=0;
fscanf(fp1,"%s%x",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\t%x\t\t%x\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length);
fcloseall();
getch();
}
OUTPUT:
Enter the location where the program has to be located: 5075
LINKIN.DAT
H
D
R
T
T
M
M
M
M
E

PROGA
LISTA
LISTB
00002010
00005416
00002405
000054
00005806
00006406
000000

000000000070
000040ENDA
000054
ENDB
LISTC
ENDC
03201077100004
15001
10001415100006
00002F100014
+LISTB
06
+LISTC
+ENDC
+ENDC

H
D
R
T
T
M
M
M
M
M

PROGB
LISTB
LISTA
00003611
00007018
00003705
00004405
00007006
00007406
00007806

000000000088
000060ENDB
000070
ENDA
LISTC
ENDC
03100000
7720270510030
10000005100006
0510020
+LISTA
+ENDA
+ENDA
+ENDC
+ENDC

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

0510030

25

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

M
E

00008206
000000

H
D
R
T
T
M
M
M
M
M
M
E

PROGC
LISTC
LISTA
00001812
00004215
00001905
00002305
00002705
00004806
00005106
00005406
000000

+ENDA

000000000057
000030ENDC
000042
ENDA
LISTB
ENDB
03100000
77100004
05100000
100030100008100011100000
+LISTA
+LISTB
+ENDA
+LISTA
+ENDA
+LISTB

LINKOUT.DAT
CSect

Sym_Name

Address

Length

PROGA
**
**
PROGB
**
**
PROGC
**
**

**
LISTA
ENDA
**
LISTB
ENDB
**
LISTC
ENDC

5075
50b5
50c9
50e5
5145
5155
516d
519d
51af

70
0
0
88
0
0
57
0
0

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

26

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
C Program to implement PASS TWO OF DIRECT LINKING LOADER
*/
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7,*fp8;
char rec[20],name[20],len[20],t[20],string[1000],mod[40]
[40],est1[20],est2[20],est4[20],sign[40][1],temp;
int ptn1[20][20],ptn2[20][20];
int
l,h=0,lent[20],i,st,m1,m2,start[20],add[20],k=0,est3,z1=0,val[40],ptn[40],offset[40],j,num,progs
tart,count=0;
fp1=fopen("DLL_IN.txt","r");
fp2=fopen("ESTAB.txt","r");
fp3=fopen("ntemp.txt","w");
fp4=fopen("memory.txt","w");
fp6=fopen("six.txt","w");
fscanf(fp1,"%s%s%x%s",rec,name,&st,len);
for(l=0;l<3;l++)
{
if(l==1)
fp3=fopen("ntempb.txt","w");
if(l==2)
fp3=fopen("ntempc.txt","w");
fscanf(fp1,"%s",t);
do{
if(strcmp(t,"T")==0)
{
fscanf(fp1,"%x%x",&start[h],&lent[h]);
if(h!=0) {
for(i=0;i<(start[h]-(start[h-1]+lent[h-1]));i++)
fprintf(fp3,"x");
}
h++;
fscanf(fp1,"%s",t);
do{
fprintf(fp3,"%s",t);
fscanf(fp1,"%s",t);
}while((strcmp(t,"T")!=0)&&(strcmp(t,"M")!=0));
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

27

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

else if(strcmp(t,"M")==0)
{
fscanf(fp1,"%x%x%s%s",&ptn[k],&offset[k],sign[k],mod[k]);
fscanf(fp2,"%s%s%x%s",est1,est2,&est3,est4);
progstart=est3;
do{
if(strcmp(mod[k],est2)==0){
val[z1]=est3;
z1++;
break;
}
else if(strcmp(mod[k],est1)==0) {
val[z1]=est3;
z1++;
break;
}
fscanf(fp2,"%s%s%x%s",est1,est2,&est3,est4);
}while(!feof(fp2));
rewind(fp2);
fscanf(fp1,"%s",t);
k++;
}
else if(strcmp(t,"E")==0)
{
fscanf(fp1,"%s",t);
if(l!=2)
fscanf(fp1,"%s%s%x%s",rec,name,&st,len);
break;
}
else if(strcmp(t,"R")==0)
{
while(strcmp(t,"T")!=0)
fscanf(fp1,"%s",t);
}
else if(strcmp(t,"D")==0)
{
while(strcmp(t,"R")!=0)
fscanf(fp1,"%s",t);
}
}while(1);
fclose(fp3);
for(i=0;i<k;i++){
if(l==0)
fp3=fopen("ntemp.txt","r+");
if(l==1)
fp3=fopen("ntempb.txt","r+");
if(l==2)
fp3=fopen("ntempc.txt","r+");
fp5=fopen("temp1.txt","w");
fseek(fp3,(ptn[i]*2)+1,0);
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

28

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

for(j=0;j<offset[i];j++)
{
fscanf(fp3,"%c",&temp);
fprintf(fp5,"%c",temp);
}
fprintf(fp5,"\n");
fclose(fp5);
fp5=fopen("temp1.txt","r");
fscanf(fp5,"%x",&num);
if(sign[i][0]=='+')
{
num=num+val[i];
fseek(fp3,(ptn[i]*2)+1,0);
if(offset[i]==5)
fprintf(fp3,"0%x",num);
else
fprintf(fp3,"00%x",num);
}
else
{
num=num-val[i];
fseek(fp3,(ptn[i]*2)+1,0);
fprintf(fp3,"00%x",num);
}
fclose(fp3);
fclose(fp5);
}
k=0;h=0;z1=0;
}
fp3=fopen("ntemp.txt","r");
fscanf(fp3,"%s",string);
fclose(fp3);
fprintf(fp6,"%s",string);
fp3=fopen("ntempb.txt","r");
fscanf(fp3,"%s",string);
fclose(fp3);
fprintf(fp6,"%s",string);
fp3=fopen("ntempc.txt","r");
fscanf(fp3,"%s",string);
fclose(fp3);
fprintf(fp6,"%s",string);
fclose(fp6);
fp6=fopen("six.txt","r");
fscanf(fp6,"%s",string);
for(i=0;i<strlen(string);i++)
{
if(i==0) {
fprintf(fp4,"%x\t",progstart);
progstart+=16;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

29

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

if((i%8==0)&&(i!=0))
fprintf(fp4,"\t");
if((i%32==0)&&(i!=0)) {
fprintf(fp4,"\n");
fprintf(fp4,"%x\t",progstart);
progstart+=16;
}
fprintf(fp4,"%c",string[i]);
}
return 0;
}
DLL_IN.TXT
H PROGA 000000 00003A
D LISTA 000030 ENDA 000050 .
R LISTB LISTC ENDC
T 000000 1D 172027 4B100000 032023 290000 332007 4B100000 3F2FEC 032016 0F2016
T 00001D 0D 010003 0F200A 4B100000 3E2000
M 000004 05 + LISTB
M 000011 05 + LISTC
E 000000
H PROGB 000000 00002E
D LISTB 000060 ENDB 000070 .
R LISTA ENDA
T 000000 1D B410 B400 B440 77201F E3201B 332FFA DB2015 A004 332009 57900000
B850
T 000020 0E 3B2FE9 13100000 4F0000 F1000000
M 000007 05 + LISTA
M 000022 05 + ENDA
E 000000
H PROGC 000000 00001C
D LISTC 000030 ENDC 000042 .
R LISTA ENDA
T 000000 1C B410 77100000 E32012 332FFA 53900000 DF2008 B850 3B2FEE 4F000005
M 000006 05 + LISTA
M 000013 06 + ENDA
E 000000

ESTAB.TXT
PROGA - 003000 000063
- LISTA 003030 - ENDA 003050 PROGB - 003063 00007f
- LISTB 0030c3 - ENDB 0030d3 PROGC - 0030e2 000051
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

30

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

- LISTC 003112 - ENDC 003124

MEMORY.TXT
3000
3010
3020
3030
3040
3050
3060
3070

1720274B
4B103112
0F200A4B
77205013
09579000
F0000F10
32FFA539
00005

1030c303
3F2FEC03
1000003E
201B332F
00B850xx
00000B41
00000DF2

20232900
20160F20
2000B410
FADB2015
x3B2FE91
07710000
008B0034

00332007
16010003
B400B440
A0043320
30305004
0E050423
02FEE4F0

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

31

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

32

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

PROGRAM:
/*
SIMPLE TEXT EDITOR
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
void newf();
void view();
void saveas();
void modify();
void main()
{
int op;
clrscr();
do
{
printf("\nMENU\n");
printf("1.New\n");
printf("2.Open\n");
printf("3.Save As\n");
printf("4.Modify\n");
printf("5.Exit\n");
printf("Enter u'r choice: ");
scanf("%d",&op);
switch(op)
{
case 1:
newf();
break;
case 2:
view();
break;
case 3:
saveas();
break;
case 4:
modify();
break;
case 5:
exit(0);
default:
printf("\nWrong choice!!!");
break;
}
}while(op!=5);
}
void newf()
{
FILE *f;
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

*/

33

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

char fname[20],c;
printf("\nEnter the Filename: ");
scanf("%s",fname);
printf("\nType the content and CTRL+Z to terminate:\n");
f=fopen(fname,"w");
rewind(f);
while((c=getchar())!=EOF)
{
putc(c,f);
}
fclose(f);
}
void view()
{
FILE *f;
char fname[20],c;
printf("\nEnter the name of the file:");
scanf("%s",&fname);
if(searchpath(fname))
{
f=fopen(fname,"r");
while((c=getc(f))!=EOF)
{
printf("%c",c);
}
}
else
printf("\nThe file %s does not exist",fname);
fclose(f);
}
void saveas()
{
FILE *f1,*f2;
char c,sou[20],des[20];
printf("\nEnter the Source file name: ");
scanf("%s",sou);
if(searchpath(sou))
{
printf("Enter the Destination file name: ");
scanf("%s",des);
f1=fopen(sou,"r");
f2=fopen(des,"w");
while((c=getc(f1))!=EOF)
{
putc(c,f2);
}
fclose(f1);
fclose(f2);
}
else
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

34

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

printf("\nFile does not exist");


getch();
}
void modify()
{
int ch1;
FILE *f1;
char c,*word,*sent,fname[20];
printf("Enter the filename to be modified: ");
scanf("%s",fname);
if(searchpath(fname))
{
printf("\n1.Character");
printf("\n2.Word");
printf("\n3.Sentence");
printf("\nEnter U'r choice: ");
scanf("%d",&ch1);
if(ch1==1)
{
f1=fopen(fname,"a+");
fseek(f1, 0L, SEEK_END);
printf("Enter the character and CTRL+Z to exit:\n ");
while((c=getchar())!=EOF)
{
putc(c,f1);
}
}
else if(ch1==2)
{
printf("Enter the word: ");
scanf("%s",word);
f1=fopen(fname,"a+");
fseek(f1, 0L, SEEK_END);
fputs(word,f1);
}
else
{
printf("Enter the sentence and CTRL+Z to exit: ");
f1=fopen(fname,"a+");
fseek(f1, 0L, SEEK_END);
while((c=getchar())!=EOF)
{
putc(c,f1);
}
}
}
else
printf("\nFilename does not exist");
fcloseall();
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

35

ER.PERUMAL MANIMEKALAI COLLEGE OF ENGINEERING

OUTPUT:
MENU
1.New
2.Open
3.Save As
4.Modify
5.Exit
Enter u'r choice: 1

Enter your file name: vijay.txt


Type the content and Ctrl+Z to save and exit
Vijay is good boy
He is studying BE in PMC TECH
MENU
1.New
2.Open
3.Save As
4.Modify
5.Exit
Enter u'r choice: 2
Enter file name to open: vijay.txt
Vijay is good boy
He is studying BE in PMC TECH
MENU
1.New
2.Open
3.Save As
4.Modify
5.Exit
Enter u'r choice: 5

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

36

You might also like