Sie sind auf Seite 1von 49

1.

IMPLENTATION OF A SYMBOL TABLE

Aim:
To write a C program to implement Symbol Table
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.

Source Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int line_no,j=0;
void create();
void insert();
void modify();
void search();
void display();
void input();
struct al
{
char label[10];
char opcode[10];
char operand[10];
}a[20];
struct sym
{
char symb[10];
int addr;
}s[20];
void main()
{
int op;
char ch[5];
clrscr();
input();
display();
do
{
printf("1.Create\n2.Modify\n3.Insert\n4.Search\n5.Exit\n");
printf("Enter Your Choice");
scanf("%d",&op);
switch(op)
{
case 1:
create();
break;
case 2:
modify();
printf("Symbol table Modified..");
break;
case 3:
insert();
printf("Insertion operation Performed..");
break;
case 4:
search();
break;
case 5:
exit(0);
}
printf("\n Do you want to continue(Y orN):");
scanf("%s",ch);
}
while(strcmp(ch,"Y")==0);
getch();
}
void input()
{
int i=0;
printf("Enter the program:\n\n");
printf("LABEL\tOPCODE\tOPERAND\n");
do
{
scanf("%s\t%s\t%s",a[i].label,a[i].opcode,a[i].operand);
i++;
}
while(strcmp(a[i-1].opcode,"END"));
line_no =i;
}
void display()
{
int i;
printf("The assembly Language program is \n");
for(i=0;i<line_no;i++)
{
printf("\n%d\t%s\t%s\t%s\n",i,a[i].label,a[i].opcode,a[i].operand);
}
}
void create()
{
int locctr,i;
for(i=0;i<line_no;i++)
{
if(strcmp(a[i].opcode,"START")==0)
locctr=atoi(a[i].operand);
else if(strcmp(a[i-1].opcode,"START")==0);
else
{ if(strcmp(a[i-1].opcode,"WORD")==0)
locctr=locctr+3;
else if(strcmp(a[i-1].opcode,"RESW")==0)
locctr = locctr+3*atoi(a[i-1].operand);
else if(strcmp(a[i-1].opcode,"BYTE")==0)
locctr = locctr+strlen(a[i-1].operand);
else
locctr = locctr+3;
}
if(strcmp(a[i].label,"$")!=0)
{
strcpy(s[j].symb,a[i].label);
s[j].addr = locctr;
printf("\n%s\t%d\n",s[j].symb,s[j].addr);
}
j++;
}
}
void modify()
{
int ln;
printf("Enter the line number to be modified:");
scanf("%d",&ln);
scanf("%s\t%s\t%s",a[ln].label,a[ln].opcode,a[ln].operand);
display();
create();
}
void insert()
{
int i,ln;
printf("Enter the line number to be inserted:");
scanf("%d",&ln);
for(i=line_no;i>=ln;i--)
{
strcpy(a[i+1].label,a[i].label);
strcpy(a[i+1].opcode,a[i].opcode);
strcpy(a[i+1].operand,a[i].operand);
}
line_no++;
printf("Enter the statement:\n");
scanf("%s\t%s\t%s",a[ln].label,a[ln].opcode,a[ln].operand);
display();
create();
}
void search()
{
char sh[10];
int i,t=0;

printf("Enter the label to be searched");


scanf("%s",sh);
for(i=0;i<line_no;i++)
{
if(strcmp(a[i].label,sh)==0)
printf("The label is found at libne no:%d\n",i);
t=1;
}
if(t==0)
printf("Labelnot found..");
}
LOGIN: Sumathi

OUTPUT:

Enter the program:

LABEL OPCODE OPERAND


COPY start 1000
first stl retaddr
cloop jsub rdrec
$ LDA LENGTH
RETADDR RESW 1
LENGTH RESW 1
$ END FIRST
The assembly Language program is

0 COPY start 1000


1 first stl retaddr
2 cloop jsub rdrec
3 $ LDA LENGTH
4 RETADDR RESW 1
5 LENGTH RESW 1
6 $ END FIRST
1.Create
2.Modify
3.Insert
4.Search
5.Exit
Enter Your Choice1

COPY 1000
FIRST 1000
CLOOP 1003
RETADR 1009
LENGTH 1012

Do you want to continue(Y orN):Y


1.Create
2.Modify
3.Insert
4.Search
5.Exit
Enter Your Choice2
Enter the line number to be modified:4
RETARD RESW 2
The assembly Language program is
0 COPY start 1000
1 first stl retaddr
2 cloop jsub rdrec
3 $ LDA LENGTH
4 RETADDR RESW 2
5 LENGTH RESW 1
6 $ END FIRST

COPY 1000
FIRST 1000
CLOOP 1003
RETADR 1009
LENGTH 1015
Symbol table modified..
Do you want to continue(Y orN):Y
1.Create
2.Modify
3.Insert
4.Search
5.Exit
Enter Your Choice3
Enter the line number to be inserted:3
Enter the statement:
$ COMP ZERO

The assembly Language program is

0 COPY start 1000


1 first stl retaddr
2 cloop jsub rdrec
3 $ COMP ZERO
4 $ LDA LENGTH
5 RETADDR RESW 2
6 LENGTH RESW 1
7 $ END FIRST

COPY 1000
FIRST 1000
CLOOP 1003
RETADR 1012
LENGTH 1018
Insertion Operation performed..
Do you want to continue(Y orN):Y
1.Create
2.Modify
3.Insert
4.Search
5.Exit
Enter Your Choice4
Enter the label to be searched: Length
The label is found at line no: 6

Do you want to continue(Y orN):N


2.IMPLENTATION OF PASS ONE OF A PASS TWO ASSEMBLER

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 opcode 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 opcode read is present in the operation code table.
If the opcode is present, then the location counter is incremented by 3.
If the opcode read is WORD, the location counter is incremented by3.
If the opcode read is RESW, the operand value is multiplied by 3 and then the location
counter is incremented.
If the opcode read is RESB, the location counter value is incremented by operand value.
If the opcode read is BYTE, the location counter is auto incremented.
The length of the source program is found using the location counter value.

source Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct symb
{
char lbl[100];
int addr,addr1;
}st[100];
int k=0;
FILE *f1,*f2,*f3;
void main()
{
char lbl[100],opc[100],opr[100];
int addr,i,addr1;
clrscr();
j:
f1=fopen("pgm.txt","W");
printf("Enter the Assembly Code:");
while(1)
{
scanf("%s",lbl);
scanf("%s",opc);
scanf("%s",opr);
if(stricmp(lbl,"null")==0)
strcpy(lbl,"--");
if(stricmp(opr,"null")==0);
strcpy(opr,"--");
fprintf(f1,"%s\t%s\t%s\n",lbl,opc,opr);
if(stricmp(opc,"end")==0)
{ break;
}
}
fclose(f1);
fopen("pgm.txt","r");
fscanf(f1,"%s\t%s\t%s\n",lbl,opc,opr);
f2 = fopen("symb.txt","W");
f3=fopen("passone.txt","W");
if(stricmp(opc,"start")==0)
addr=atoi(opr);
else
addr =0;
fprintf(f3,"%d\t%s\t%s\t%s\n",addr,lbl,opc,opr);
addr1=addr;
while(!feof(f1))
{
fscanf(f1,"%s\t%s\t%s\n",lbl,opc,opr);
fprintf(f3,"%d\t%s\t%s\t%s\n",addr,lbl,opc,opr);
if(stricmp(lbl,"--")!=0)
{
for(i=0;i<k;i++)
{
if(stricmp(st[i].lbl,lbl)==0)
{
printf("lable Duplication Error\n");
goto j;
}
}
strcpy(st[k].lbl,lbl);
st[k].addr=addr;
k++;
}
if(stricmp(opc,"resb")==0)
addr+=atoi(opr);
else if(stricmp(opc,"resw")==0)
addr+=(3*(atoi(opr)));
else if(stricmp(opc,"byte")==0)
addr+=((strlen(opc))-3);
else
addr+=3;
}

for(i=0;i<k;i++)
{
fprintf(f2,"%s\t%d\n",st[i].lbl,st[i].addr);
}
fclose(f2);
fclose(f3);
f2=fopen("symb.txt","r");
f3=fopen("passone.txt","r");
printf("Symbol table:\n\n");
while(!feof(f2))
{
fscanf(f2,"%s\t%d\n",lbl,&addr);
printf("%s\t%d\n",lbl,addr);
}
printf("intermediate file:\n\n");
while(!feof(f3))
{
fscanf(f3,"%d\t%s\t%s\t%s\n",&addr,lbl,opc,opr);
printf("%d\t5s\t$s\t%s\n",addr,lbl,opc,opr);
}
printf("%x(HEX)",(addr-addr1));
getch();
}
LOGIN : Sumathi

OUTPUT:

Enter the Assembly code:

Null start 1000


Null lda alpha
Null sub one
Null sta beta
one word 1
alpha resw 1
beta resw 1
null end null

Symbol Table:

one 1009
alpha 1012
beta 1015

Intermediate file:

1000 --- start 1000


1000 --- lda alpha
1003 --- sub one
1006 --- sta beta
1009 one word 1
1012 alpha resw 1
1015 beta resw 1
1018 --- end ---

Program length :12(Hex)


3.IMPLEMENTATION OF PASS TWO OF A TWO PASS ASSEMBLER

Aim :
To write a c program to implement pass two of a two pass assembler
Algorithm:
Get the intermediate file from pass1
Use the symbol table file and choose it for two pass assembler
Store all opcode’s assembler directives in a file
In the intermediate file,compare the opcode with file and get the corresponding opcode
value
Append this value with its corresponding location counter and store
label,opcode,operand,object code in a new f ile
Create a file to store and generate the object program format by using the object code
generated in pass1
Display the new file with its contents as header,text and end records.

Source Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
FILE *fptr1,*fptr2,*fptr3,*fptr4,*fp;
char address[10],label[10],opcode[10],operand[10];
char op[10],val[10],objcode[10];
char sym[10],adr[10];
char hexeq[10],dash[10],temp[10],c;
int start,end,recstart,recend;
int no,i,j,k,len,l,receln[30];
int intohex(char integer[])
{
int n;
n=0;
while(1)
{
n++;
itoa(n,hexeq,16);
if(strcmpi(integer,hexeq)==0)
{
return n;
}
}
}
void recordlen()
{
fptr4=fopen("c:/object.txt","r");
i=0;
j=0;
while(!feof(fptr4))
{
fscanf(fptr4,"%s%s%s%s%s",address,label,opcode,operand,objcode);
if(strcmpi(opcode,"START")==0)
{
recstart=intohex(operand);
}
else if(strcmpi(opcode,"START")!=0 && strcmpi(opcode,"END")!=0)
{
if(strcmp(objcode,dash)!=0)
{
if(i==10)
{
recend=intohex(address);
receln[j]=recend-recstart;
recstart=recend;
i=0;
j++;
}
}
else
i--;
i++;
}
else if(strcmpi(opcode,"END")==0)
{
recend=intohex(address);
receln[j]=recend-recstart;
}
}
fclose(fptr4);
}
void main()
{
fptr1=fopen("c:/intermed.txt","r");
fptr2=fopen("c:/symtab.txt","r");
fptr3=fopen("c:/optab.txt","r");
fptr4=fopen("c:/object.txt","w");
strcpy(dash,"-");
strcpy(objcode,NULL);
clrscr();
while(!feof(fptr1))
{
fscanf(fptr1,"%s%s%s%s",address,label,opcode,operand);
if(strcmpi(opcode,"START")==0)
{
start=intohex(address);
fprintf(fptr4,"\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,dash);
continue;
}
else if((strcmpi(opcode,"RESW")==0)||(strcmpi(opcode,"RESB"))==0)
{
fprintf(fptr4,"\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,dash);
continue;
}
else if(strcmpi(opcode,"WORD")==0)
{
strcpy(objcode,NULL);
strcpy(temp,NULL);
no=atoi(operand);
itoa(no,hexeq,16);
strcat(objcode,hexeq);
len=6-strlen(objcode);
for(i=0;i<len;i++)
{
strcat(temp,"0");
}
strcat(temp,objcode);
strcpy(objcode,temp);
strupr(objcode);
}
else if(strcmpi(opcode,"BYTE")==0)
{
fp=fopen("c:/hex.txt","w");
if(operand[0]=='C')
{
for(i=2;i<strlen(operand)-1;i++)
{
fprintf(fp,"%X",operand[i]);
}
fclose(fp);
fp=fopen("c:/hex.txt","r");
fscanf(fp,"%s",hexeq);
strcat(objcode,hexeq);
fclose(fp);
}
else
{
for(i=0;i<strlen(operand)-1;i++)
{
objcode[i-2]=operand[i];
}
objcode[i-2]='\0';
}
}
else if(strcmpi(opcode,"END")==0)
{
end=intohex(address);
fprintf(fptr4,"\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,dash);
break;
}
else
{
while(!feof(fptr3))
{
fscanf(fptr3,"%s%s",op,val);
if(strcmp(op,opcode)==0)
{
strcat(objcode,val);
break;
}
}
}
while(!feof(fptr2))
{
fscanf(fptr2,"%s%s",sym,adr);
if(strcmp(sym,operand)==0)
{
strcat(objcode,adr);
break;
}
}
fprintf(fptr4,"\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,objcode);
strcpy(objcode,NULL);
rewind(fptr2);
rewind(fptr3);
}
fclose(fptr1);
fclose(fptr2);
fclose(fptr3);
fclose(fptr4);
fptr4=fopen("c:/object.txt","r");
printf("\nFILE WITH OBJECT CODE\n");
printf("------------------------\n\n");
printf("LOC\tLABEL\tOPCODE\tOPERAND\tOBJCODE\n");
printf("---\t----\t-------\t----------\t----------");
while(!feof(fptr4))
{
fscanf(fptr4,"%s%s%s%s%s",address,label,opcode,operand,objcode);
printf("\n%s\t%s\t%s\t%s\t%s",address,label,opcode,operand,objcode);
}
fclose(fptr4);
getch();
recordlen();
fptr4=fopen("c:/object.txt","r");
fptr1=fopen("c:/objprog.txt","w");
i=0;
j=0;
while(!feof(fptr4))
{
fscanf(fptr4,"%s%s%s%s%s",address,label,opcode,operand,objcode);
if(strcmpi(opcode,"START")==0)
{
len=end-start;
itoa(len,hexeq,16);
l=6-strlen(hexeq);
strcpy(temp,NULL);
for(k=0;k<l;k++)
{
strcat(temp,"0");
}
strcat(temp,hexeq);
strcat(hexeq,temp);
strupr(hexeq);
printf("\n\nprogram length=%s\n",hexeq);
getch();
recstart=start;
fprintf(fptr1,"H%s%s%s\n",label,address,hexeq);
fprintf(fptr1,"T 00%X%X",recstart,receln[j]);
}
else if(strcmpi(opcode,"END")!=0)
{
if(strcmpi(objcode,dash)!=0)
{
if(i==10)
{
j++;
fprintf(fptr1,"\nT");
recstart=intohex(address);
fprintf(fptr1,"00%X%X%s",recstart,receln[j],objcode);
i=0;
}
else
{
fprintf(fptr1,"%s",objcode);
}
}
else if(strcmpi(objcode,dash)==0)
{
i--;
}
i++;
}
else if(strcmpi(opcode,"END")==0)
{
fprintf(fptr1,"\nE 00%X",start);
}
}
fclose(fptr1);
fclose(fptr4);
fptr1=fopen("c:/objprog.txt","r");
printf("\n OBJECT PROGRAM\n");
printf("--------------------\n\n");
c=getc(fptr1);
while(c!=EOF)
{
printf("%c",c);
c=getc(fptr1);
}
fclose(fptr1);
getch();
}
INPUT FILE:
INTERMED.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 C'EOF'
2019 C1 RESB 1
2020 ** END **

OPTAB.DAT
LDA 33
STA 44
LDCH 53
STCH 57
END *

SYMTAB.DAT
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019

LOGIN: Sumathi

OUTPUT FILE:

ASSMLIST.DAT

** START 2000
2000 ** LDA FIVE 332015
2003 ** STA ALPHA 442012
2006 ** LDCH CHARZ 532018
2009 ** STCH C1 572019
2012 ALPHA RESW 1
2015 FIVE WORD 5 000005
2018 CHARZ BYTE C'EOF' 454f46
2019 C1 RESB 1
2020 ** END **
4.IMPLEMENTATION OF SINGLE PASS ASSEMBLER

Aim:
To implement a single pass assembler using ‘c’ language
Algorithm:
Object code is generated in the program
If symbol is not yet defined,the operand address is omitted
The symbol is entered in the symbol table with ‘$’ pin
When the definition is encountered, program address is inserted in a list associated with
the symbol
continue the process fill the end of program fill all the forward references

Source program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
struct input
{
char label[10],opcode[10],operand[10],mnem[10];
int loc;
}table[MAX];
struct symtab
{
char sym[10];
int f,val,ref;
}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[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].mnem,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].mnem,'\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].mnem,'\0');
table[i].loc=atoi('\0');
}
else if(strcmp(table[i].opcode,"RESB")==0)
{
locctr+=(atoi(table[i].operand));
strcpy(table[i].mnem,'\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].mnem,itoa(table[x].l
oc,add,10)));
for(x=1;x<j;x++)
printf("%s\t%d\n",symtbl[x],symtbl[x].val);
getch();
}
INPUT FILE:
INPUT.DAT
$ START 1000
$ JSUB CLOOP
$ JSUB DLOOP
ALPHA WORD 23
BETA RESW 3
GAMMA BYTE C'Z'
DELTA RESB 7
CLOOP LDA ALPHA
DLOOP STA DELTA
$ LDCH GAMMA
$ STCH DELTA
$ END $

OPTAB.DAT

START $
JSUB 48
LDA 14
STA 03
LDCH 53
STCH 57
END $

LOGIN: sumathi

OUTPUT FILE:

SPOUT.DAT
$ START 1000 0
$ JSUB CLOOP 481026
$ JSUB DLOOP 481029
ALPHA WORD 23 23
BETA RESW 3 0
GAMMA BYTE C’Z’ 90
DELTA RESB 7 0
CLOOP LDA ALPHA 141006
DLOOP STA DELTA 0
$ LDCH GAMMA 531018
$ STCH DELTA 571019
$ END $ 0
OUTPUT:
CLOOP 1026
DLOOP 1029
ALPHA 1006
BETA 1009
GAMMA 1018
DELTA 1019
5. IMPLEMENTATION OF MACRO PROCESSOR

Aim:
To write a c program to implement macro processor
Algorithm:
Start the macro processor program
Include the necessary header files and variable
Open the three files
f1=macin.dat with read privilege
f2=macout.dat with write privilege
f3= deftab.dat with write privilege
Get the variable form f1 file macin.dat for label,opcode,operand
Read the variable until the opcode is not is equal to zero
Then check if the opcode is equal to Macro if Macro
Then
Copy macroname=label
Get the variable label ,opcode ,operand
In these if condition perform the while loop until opcode is not equal to MEND
Copy the variable
d[lines].lab=label
d[lines].opc=opcode
d[lines].oper=operand
and increase lines++;
close while loop and if condtion
else if opcode is equal to macro name
Perform the for loop from 0 to length
fprint for d[i].lab,d[i].opc,d[i].oper
else if it is not match
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
Finally terminate the program

Source program :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char n,n1,c1,i;
char fn[10][10],ilab[20],iopd[20],m[20][3],oper[20],opd[20];
FILE *fp1,*fp2,*p[5];
clrscr();
n=0;
fp1=fopen("z:macin.dat","r");
while(!feof(fp1))
{
fscanf(fp1,"%s\t%s\t%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
n++;
}
printf("no of macros=%d\n",n);
n1=n;
printf("\n 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("z:outm.dat","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);
}
INPUT FILE:
MACIN.DAT
macro m1
move a,b
mend --
macro m2
lda b
mend --
start 1000
lda a
call m1
call m2
add a,b

LOGIN: sumathi
OUTPUT FILE:

OUTM.DAT
** macro m1
** move a , b
** mend –
** macro m2
** lda b
** mend –
** start 1000
** lda a
** move a , b
** lda b
** add a , b
** add a , b

M1.DAT:
** move a , b

M2.DAT:
** lda b
6. IMPLEMENTATION OF ABSOLUTE LOADER

Aim:
To implement the Absolute Loader using ‘c’ language
Algorithm:
Read the Loader record
Verify program name and length
Read first text record from the input file
Process the following steps until an end record is reached
If Object code is in character form convert in to internal hexadecimal representation
Move object codes to specified locations in memory
Write the starting location counter value of a blocks of object code and the
corresponding internal representation to the output file
Read next record from the input file
Go to the address specified in end record
Close all the files and edit

Source program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct obj_code
{
int locctr;
char byte[5];
}code[200];
void main()
{
FILE *fp1,*fp2;
char input[15];
int i,len,textloc,tlen,loc;
int n=0,count=0,inc=0,tloc=0,num=0;
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(fp2,"%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();
}

INPUT FILE :

LOUT.DAT:

H COPy 002000 00107A


T 002000 1E 142033 483039 102036 282030 302015
483061 3C2003 00202A 0C2039 00202D
T 00201E 15 2C2036 483061 182033 4C0000 454F46
200003 100000
T 002039 1E 242030 302030 E0305D 30303F D8305D
282030 303057 53A039 2C305E 38303F
T 002057 A 102036 4C0000 F1 201000
T 002071 19 342030 E03079 303064 4FA039 DC3079
E 002000

LOGIN: sumathi

OUTPUT FILE:

LOADOUT.DAT

2000 14203348 30391020 36282030 30201548


2010 30613C20 0300202A 0C203900 202D2C20
2020 36483061 1820334C 0000454F 46200003
2030 100000XX XXXXXXXX XX242030 302030E0
2040 305D3030 3FD8305D 28203030 305753A0
2050 392C305E 38303F10 20364C00 00F12010
2060 00XXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
2070 XX342030 E0307930 30644FA0 39DC3079
2080 2C203638 30644C00 0015
7. IMPLEMENTATION OF RELOCATION LOADER

Aim
To write a c program to Perform the Relocation Loader
Algorithm
Start the program
Include the necessary header file and variable
Open the two file for
fp1= relinput.dat and give read
fp2= reloutput.dat and give write
Read the content
Using while loop perform the loop until character is not equal to E
while(strcmp(input,"E")!=0)
If the character is H
Get the variable add, length, and input
Else if the character is T
Get the variable address and bitmask
And perform the for loop for starting zero to up to len
Get the opcode ,addr and assign relocbit to bitmask
If relocabit is zero
Then
actualadd=addr;
else
Add the addr and star value
Finally terminate the program

Source program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct obj_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,tloc,tlen,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("\n ENTER THE LOCATION WHERE THE PROGRAM HAS TO BE
LOADED:\n");
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=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,"yy");
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;
}
}
}
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:

LINPUT.DAT:
H COPY 000000 001073
T 000000 10 15 140033 481039 100036 280030 300015 481061
311003
200030 211033 200033
T 000011 19 045 412036 481061 380033 412000 454196 100003
200000
T 000031 15 135 140030 430030 141013 301044 241064 210030
301057
543039 212064 381045
T 000058 05 056 100036 520000 151
T 000065 19 080 340030 141079 301064 503039 152079 220036
381064
430000 25
E 000000

LOGIN: sumathi

OUTPUT FILE:

LOADOUT.DAT:

1000 14103348 20391010 36281030 30001548


1016 10613110 03200030 21103320 0033yy41
1032 30364810 61381033 41300045 41961010
1048 03200000 yy141030 43003014 10133010
1004 44241004 21105050 20575440 39212004
1080 381045yy yyyyyyyy yyyyyyyy yyyyyy10
1096 10365210 00152000 301000yy yy341030
1112 14107930 20645030 39152079 22003638
1128 10644300 0025
8. IMPLEMENTATION OF PASS1 OF THE DIRECT LINKING LOADER

Aim
To write a c program to Implement pass two of direct linking loader
Algorithm :
Star the program for linking loader
Assign the necessary variable and the header variable
Open the two file
In fp1 for link input file for read privilege
And fp2 for load map file for write privilege
Read the character until the input is not equal to END
And the check the character in if condition input is H then
Get the name from fp1
Copy the name between csname=name
And extsym=**
Else if the character is D
Then get the input variable
In these if condition perform the while loop the read character until the input is not equal
to R
The copy csnmae=**
And extsym=input
Then add address =add+csaddr
And length is equal to zero
And perform the operation
Finally terminate the program

Source Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
char sub[10];
struct estab
{
char ctrlname[10];
char sym[10];
int addr,length;
}
estab[10];
void substr(char*inp,int i,int j)
{
int k,x=0;
char str[10];
for(k=i;k<=j;k++)
{
if(inp[k]!=' ')
sub[x++]=inp[k];
sub[x]='\0';
}
}
void main()
{
int n,i,j,paddr,csaddr,ctrlen,esptr=0;
char str1[16],str[20][80];
clrscr();
printf("enter the program address:\n");
scanf("%d",&paddr);
csaddr=paddr;
strcpy(str[0],"HPROGA 00000000063");
strcpy(str[1],"DLISTA 000040ENDA 00054");
strcpy(str[2],"T00101218031036482061081033");
strcpy(str[3],"M00100606+COPY");
strcpy(str[4],"E001000");
strcpy(str[5],"HPROGB 00000000073");
strcpy(str[6],"DLISTB 000040ENDB 00054");
strcpy(str[7],"T00101218031036482061081033");
strcpy(str[8],"E001000");
strcpy(str[9],"HPROGC 00000000053");
strcpy(str[6],"DLISTC 000040ENDC 00054");
strcpy(str[11],"E001000");
strcpy(str[12],"END");
n=13;
for(i=0;i<=n;i++)
{
if(str[i][0]=='H')
{
substr(str[i],1,6);
strcpy(estab[esptr].ctrlname,sub);
strcpy(estab[esptr].sym,"_");
estab[esptr].addr=csaddr;
substr(str[i],13,18);
strcpy(str1,sub);
ctrlen=estab[esptr++].length=atoi(str1);
while(str[i][0]!='E')
{
if(str[i][0]=='D')
{
for(j=1;j<strlen(str[i]);j=j+12)
{
strcpy(estab[esptr].ctrlname,"_");
substr(str[i],j,j+5);
strcpy(estab[esptr].sym,sub);
substr(str[i],j+6,j+11);
estab[esptr].addr=atoi(sub)+csaddr;
estab[esptr++].length=0;
}
}
i++;
}
csaddr=csaddr+ctrlen;
}
}
printf("\n PASS1 output for direct linking loader:\n");
printf("\n CSECTION\tSYMBOL\tADDRESS\TLENGTH\n");
for(i=0;i<esptr;i++)
printf("\n%s\t%s\t%d\t%d\n",estab[i].ctrlname,estab[i].sym,estab[i].addr,estab[i].length);
getch();
}
LOGIN: sumathi

OUTPUT:

enter the program address:


1000

PASS1 output for direct linking loader:

CSECTION SYMBOL ADDRESS LENGTH

PROGA _ 1000 63

_ LISTA 1040 0

_ ENDA0 1054 0

PROGB _ 1063 73

_ LISTC 1103 0

_ ENDC0 1117 0

PROGC _ 1136 53
9. IMPLEMENTATION OF PASS2 OF THE DIRECT LINKING LOADER

Aim
To write a c program to Implement pass two of direct linking loader
Algorithm :
Star the program for linking loader
Assign the necessary variable and the header variable
Open the two file
In fp1 for link input file for read privilege
And fp2 for load map file for write privilege
Read the character until the input is not equal to END
And the check the character in if condition input is H then
Get the name from fp1
Copy the name between csname=name
And extsym=**
Else if the character is D
Then get the input variable
In these if condition perform the while loop the read character until the input is not equal
to R
The copy csnmae=**
And extsym=input
Then add address =add+csaddr
And length is equal to zero
And perform the operation
Finally terminate the program

SourceProgram:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
char sub[10];
//int i=0;
struct loader
{
int loc;
char value[10];
}
ldr[50];
struct estab
{
char ctrlname[10];
char sym[10];
int addr,length;
}
estab[10];
void substr(char *inp,int i,int j)
{
int k,x=0;
for(k=i;k<=j;k++)
{
if(inp[k]!=' ')
sub[x++]=inp[k];
sub[x]='\0';
}
}
void main()
{
int x1,i,i1,j,x,n,csddr,ctrlen,start,end,mlength,mstart,a,ct,paddr,length;
int esptr=0,cnt=0,pstart,plength;
char str1[10],str[20][80],name[10];
clrscr();
strcpy(estab[0].ctrlname,"PROGA");
strcpy(estab[0].sym,"-");
estab[0].addr=4000;
estab[0].length=63;
strcpy(estab[1].ctrlname,"-");
strcpy(estab[1].sym,"LISTA");
estab[1].addr=4040;
estab[1].length=0;
strcpy(estab[2].ctrlname,"-");
strcpy(estab[2].sym,"ENDA");
estab[2].addr=4054;
estab[2].length=0;
esptr=3;
strcpy(str[0],"HPROGA 0010000000453");
strcpy(str[1],"DLISTA 000040ENDA 000054");
strcpy(str[2],"T00100012156789094203");
strcpy(str[3],"T0010001218031036482061081033");
strcpy(str[4],"T00103606101036");
strcpy(str[5],"T00104806300005");
strcpy(str[6],"M00100604+PROGA");
strcpy(str[7],"M00101404+LISTA");
strcpy(str[8],"M00103604+ENDA");
strcpy(str[9],"E001000");
strcpy(str[10],"END");
substr(str[0],1,6);
strcpy(name,sub);
printf("\n program name:%s \n",name);
substr(str[0],7,12);
strcpy(str1,sub);
pstart=atoi(str1);
printf("\n start location:%d\n",pstart);
substr(str[0],13,18);
strcpy(str1,sub);
plength=atoi(str1);
printf("\n proglen %d\n",plength);
n=11;
substr(str[2],1,6);
strcpy(str1,sub);
for(i=1;i<n;i++)
{
if(str[i][0]!='E')
{
if(str[i][0]=='T')
{
substr(str[i],1,6);
strcpy(str1,sub);
start=atoi(str1);
if(end<start)
for(j=end;j<start;j=j+2)
{
ldr[cnt].loc=j;
strcpy(ldr[cnt++].value,"xx");
}
substr(str[i],7,8);
strcpy(str1,sub);
length=atoi(str1);
end=start+length;
for(j=9;j<strlen(str[i]);j=j+2)
{
substr(str[i],j,j+1);
strcpy(str1,sub);
ldr[cnt].loc=start;
strcpy(ldr[cnt++].value,str1);
start=start+2;
}
}
else if(str[i][0]=='M')
{
substr(str[i],1,6);
mstart=atoi(sub);
substr(str[i],7,8);
mlength=atoi(sub);
substr(str[i],10,strlen(str[i]));
strcpy(name,sub);
for(i1=0;i1<esptr;i1++)
{
if((strcmp(estab[i1].sym,name))==0||(strcmp(estab[i1].ctrlname,name)==0))
{
for(a=0;a<cnt;a++)
{
if(ldr[a].loc==mstart)
{
ct=0;
while(ct<mlength)
{
itoa(estab[i].addr,str1,10);
substr(str1,ct,ct+1);
itoa(atoi(ldr[a].value)+atoi(sub),str1,10);
strcpy(ldr[a].value,str1);
ct+=2;
a+=1;
}
}
}
}
}
}

}
}
for(i=0;i<cnt;i++)
printf("\n%d\t%s",ldr[i].loc,ldr[i].value);
getch();
}
LOGIN: sumathi

OUTPUT:

program name:PROGA

start location:1000

proglen 45

1000 15
1002 67
1004 89
1006 9
1008 42
1010 03
1000 18
1002 03
1004 10
1006 36
1008 48
1010 20
1012 61
1014 8
1016 10
1018 33
1012 xx
1014 0
10.IMPLEMENTATION OF A SIMPLE TEXT EDITOR

Aim:
To write a c program for a simple text editor
Algorithm:
To display a menu using switch case
Enter the choice
If the choice is the appropriate one and perform the next process
Enter the file name and edit the text
Select the next choice and open the file to view the contents
Then select the next choice to copy and paste the text

Source program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
void newfile();
void openfile();
void copytext();
void pastetext();
void main()
{
int i;
clrscr();
do
{
printf("\n1.newfile\n2.openfile\n3.copytext\n4.pastetext\n5.exit");
scanf("%d",&i);
printf("enter ur choice");
switch(i)
{
case 1:
newfile();
break;
case 2:
openfile();
break;
case 3:
copytext();
break;
case 4:
pastetext();
break;
}
}while(i<=4);
getch();
}
void newfile()
{
FILE *f1;
char name[15],c;
clrscr();
printf("give name of file which has to be created");
scanf("%s",name);
f1=fopen(name,"w");
printf("enter text to be written into file & press 'ctrl-z' to terminate");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
}
void openfile()
{
char name[15],c;
FILE *f2;
printf("enter the name of file to open");
scanf("%s",name);
f2=fopen(name,"r");
if(f2==NULL)
{
printf("file does not exit");
}
else
{
printf("content of file are:");
while((c=getc(f2))!=EOF)
printf("%c",c);
}
fclose(f2);
getch();
}
void copytext()
{
FILE *f1,*f2;
char f[5],oldword[50],fromfile[50];
printf("enter file name");
scanf("%s",f);
f1=fopen(f,"r");
f2=fopen("clipboard.txt","w");
printf("enter string to be searched");
scanf("%s",oldword);
if(f1==NULL)
{
printf("\n file does not exist");
}
else
{
fscanf(f1,"%s",fromfile);
while(!feof(f1))
{
if(strcmp(oldword,fromfile)==0)
printf(f2,"%s",oldword);
fscanf(f1,"%s",fromfile);
}
fclose(f1);
fclose(f2);
}
getch();
}
void pastetext()
{
char c[15],b;
FILE *f1,*f2;
printf("enter file name");
scanf("%s",c);
f1=fopen("clipboard.txt","r");
f2=fopen(c,"a+");
if(f1==NULL)
{
printf("\n files does not exist");
}
while(!feof(f1))
{
b=getc(f1);
fprintf(f2,"%c",b);
}
fclose(f2);
fclose(f1);
}
LOGIN : sumathi

OUTPUT:
1.newfile
2.openfile
3.copytext
4.pastetext
5.exit

enter ur choice:1
give name of file which has to be cerated:file1
enter text to be written into file & press 'ctrl-z' to terminate:system soft
^Z

1.newfile
2.openfile
3.copytext
4.pastetext
5.exit

enter ur choice:2
enter the name of file to open:file1
content of file are:
system soft

1.newfile
2.openfile
3.copytext
4.pastetext
5.exit

enter ur choice:3
enter file name:file1
enter string to be searched:system

1.newfile
2.openfile
3.copytext
4.pastetext
5.exit

enter ur choice:2
enter the name of file to open:clipboard.txt
content of file are:system
1.newfile
2.openfile
3.copytext
4.pastetext
5.exit

enter ur choice:4
enter file name:file2
content of file are:system

1.newfile
2.openfile
3.copytext
4.pastetext
5.exit

enter ur choice:5

Das könnte Ihnen auch gefallen