61050810 Symbol Table

39
1.SOURCE CODE: #include<stdio.h> #include<conio.h> #include<alloc.h> #include<string.h> #include<stdlib.h> #define NULL 0 int size=0; void Insert(); void Display(); void Delete(); int Search(char lab[]);void Modify(); struct SymbTab { char label[10],symbol[10]; int addr; struct SymbTab *next;}; struct SymbTab *first,*last; void main() { int op,y; char la[10]; clrscr(); do { printf(“\n\tSYMBOL TABLE IMPLEMENTATION\n”); printf(“\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.MODIFY\n\t6.END\n”); printf(“\n\tEnter your option : “); scanf(“%d”,&op); switch(op) { case 1: Insert(); break; case 2: Display(); break; case 3: Delete(); break; case 4: printf(“\n\tEnter the label to be searched : “); scanf(“%s”,la); y=Search(la); printf(“\n\tSearch Result:”); if(y==1) printf(“\n\tThe label is present in the symbol table\n”); else printf(“\n\tThe label is not present in the symbol table\n”); break;

Transcript of 61050810 Symbol Table

Page 1: 61050810 Symbol Table

1.SOURCE CODE:#include<stdio.h>#include<conio.h>#include<alloc.h>#include<string.h>#include<stdlib.h>#define NULL 0int size=0;void Insert();void Display();void Delete();int Search(char lab[]);void Modify();struct SymbTab{char label[10],symbol[10];int addr;struct SymbTab *next;};struct SymbTab *first,*last;void main(){int op,y;char la[10];clrscr();do{printf(“\n\tSYMBOL TABLE IMPLEMENTATION\n”);printf(“\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.MODIFY\n\t6.END\n”);printf(“\n\tEnter your option : “);scanf(“%d”,&op);switch(op){case 1:Insert();break;case 2:Display();break;case 3:Delete();break;case 4:printf(“\n\tEnter the label to be searched : “);scanf(“%s”,la);y=Search(la);printf(“\n\tSearch Result:”);if(y==1)printf(“\n\tThe label is present in the symbol table\n”);elseprintf(“\n\tThe label is not present in the symbol table\n”);break;

Page 2: 61050810 Symbol Table

case 5:Modify();break;case 6:exit(0);}}while(op<6);getch();}void Insert(){int n;char l[10];printf(“\n\tEnter the label : “);scanf(“%s”,l);n=Search(l);if(n==1)printf(“\n\tThe label exists already in the symbol table\n\tDuplicate can’t be inserted”);else{struct SymbTab *p;p=malloc(sizeof(struct SymbTab));strcpy(p->label,l);printf(“\n\tEnter the symbol : “);scanf(“%s”,p->symbol);printf(“\n\tEnter the address : “);scanf(“%d”,&p->addr);p->next=NULL;if(size==0){first=p;last=p;}else{last->next=p;last=p;}size++;}printf(“\n\tLabel inserted\n”);}void Display(){int i;struct SymbTab *p;p=first;printf(“\n\tLABEL\t\tSYMBOL\t\tADDRESS\n”);for(i=0;i<size;i++){

Page 3: 61050810 Symbol Table

printf(“\t%s\t\t%s\t\t%d\n”,p->label,p->symbol,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(“\n\tWhat do you want to modify?\n”);printf(“\n\t1.Only the label\n\t2.Only the address\n\t3.Both the label and address\n”);printf(“\tEnter your choice : “);scanf(“%d”,&choice);switch(choice){case 1:printf(“\n\tEnter the old label : “);scanf(“%s”,l);s=Search(l);if(s==0)printf(“\n\tLabel not found\n”);else{printf(“\n\tEnter the new label : “);scanf(“%s”,nl);for(i=0;i<size;i++){if(strcmp(p->label,l)==0)strcpy(p->label,nl);p=p->next;}printf(“\n\tAfter Modification:\n”);Display();}break;case 2:

Page 4: 61050810 Symbol Table

printf(“\n\tEnter the label where the address is to be modified : “);scanf(“%s”,l);s=Search(l);if(s==0)printf(“\n\tLabel not found\n”);else{printf(“\n\tEnter the new address : “);scanf(“%d”,&add);for(i=0;i<size;i++){if(strcmp(p->label,l)==0)p->addr=add;p=p->next;}printf(“\n\tAfter Modification:\n”);Display();}break;case 3:printf(“\n\tEnter the old label : “);scanf(“%s”,l);s=Search(l);if(s==0)printf(“\n\tLabel not found\n”);else{printf(“\n\tEnter the new label : “);scanf(“%s”,nl);printf(“\n\tEnter the new address : “);scanf(“%d”,&add);for(i=0;i<size;i++){if(strcmp(p->label,l)==0){strcpy(p->label,nl);p->addr=add;}p=p->next;}printf(“\n\tAfter Modification:\n”);Display();}break;}}void Delete(){int a;char l[10];

Page 5: 61050810 Symbol Table

struct SymbTab *p,*q;p=first;printf(“\n\tEnter the label to be deleted : “);scanf(“%s”,l);a=Search(l);if(a==0)printf(“\n\tLabel 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–;printf(“\n\tAfter Deletion:\n”);Display();}}

Page 6: 61050810 Symbol Table

2.Pass One of two pass assembler#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char opcode[10],operand[10],label[10],code[10][10],ch; char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"}; int locctr,start,len,i=0,j=0; FILE *fp1,*fp2,*fp3; clrscr(); fp1=fopen("INPUT.DAT","r"); fp2=fopen("SYMTAB.DAT","w"); fp3=fopen("OUT.DAT","w"); 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",locctr); if(strcmp(label,"**")!=0) fprintf(fp2,"%s\t%d\n",label,locctr); strcpy(code[i],mnemonic[j]); while(strcmp(mnemonic[j],"END")!=0) { if(strcmp(opcode,mnemonic[j])==0) { locctr+=3; break; } strcpy(code[i],mnemonic[j]); j++; } if(strcmp(opcode,"WORD")==0) locctr+=3; else if(strcmp(opcode,"RESW")==0) locctr+=(3*(atoi(operand))); else if(strcmp(opcode,"RESB")==0) locctr+=(atoi(operand)); else if(strcmp(opcode,"BYTE")==0) ++locctr; fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand);

Page 7: 61050810 Symbol Table

} fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand); fcloseall(); printf("\n\nThe contents of Input Table :\n\n"); fp1=fopen("INPUT.DAT","r"); ch=fgetc(fp1); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp1); } printf("\n\nThe contents of Output Table :\n\n\t"); fp3=fopen("OUT.DAT","r"); ch=fgetc(fp3); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp3); } len=locctr-start; printf("\nThe length of the program is %d.\n\n",len); printf("\n\nThe contents of Symbol Table :\n\n"); fp2=fopen("SYMTAB.DAT","r"); ch=fgetc(fp2); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp2); } fcloseall(); getch();}

INPUT FILE : INPUT.DAT** START 2000** LDA FIVE** STA ALPHA** LDCH CHARZ** STCH C1ALPHA RESW 1FIVE WORD 5CHARZ BYTE C'Z'C1 RESB 1** END **

Page 8: 61050810 Symbol Table

3.Pass two of two pass assembler:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char a[10],ad[10],label[10],opcode[10],operand[10],symbol[10],ch; int

st,diff,i,address,add,len,actual_len,finaddr,prevaddr,j=0;

char mnemonic[15][15]={"LDA","STA","LDCH","STCH"};

char code[15][15]={"33","44","53","57"};

FILE *fp1,*fp2,*fp3,*fp4;

clrscr();

fp1=fopen("ASSMLIST.DAT","w");

fp2=fopen("SYMTAB.DAT","r");

fp3=fopen("INTERMED.DAT","r");

fp4=fopen("OBJCODE.DAT","w");

fscanf(fp3,"%s%s%s",label,opcode,operand);

while(strcmp(opcode,"END")!=0)

{

prevaddr=address;

fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);

}

finaddr=address;

fclose(fp3);

fp3=fopen("INTERMED.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);

fprintf(fp4,"H^%s^00%s^00%d\n",label,operand,finaddr);

fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);

st=address;

diff=prevaddr-st;

fprintf(fp4,"T^00%d^%d",address,diff);

}

while(strcmp(opcode,"END")!=0)

Page 9: 61050810 Symbol Table

{

if(strcmp(opcode,"BYTE")==0)

{

fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);

len=strlen(operand);

actual_len=len-3;

fprintf(fp4,"^");

for(i=2;i<(actual_len+2);i++)

{

itoa(operand[i],ad,16);

fprintf(fp1,"%s",ad);

fprintf(fp4,"%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",address,label,opcode,operand,a);

fprintf(fp4,"^00000%s",a);

}

else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))

fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);

else

{

while(strcmp(opcode,mnemonic[j])!=0)

j++;

if(strcmp(operand,"COPY")==0)

fprintf(fp1,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code[j]);

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%s%d\n",address,label,opcode,operand,code[j],add);

fprintf(fp4,"^%s%d",code[j],add);

}

}

fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);

Page 10: 61050810 Symbol Table

}

fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);

fprintf(fp4,"\nE^00%d",st);

printf("\n Intermediate file is converted into object code");

fcloseall();

printf("\n\nThe contents of Intermediate file:\n\n\t");

fp3=fopen("INTERMED.DAT","r");

ch=fgetc(fp3);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp3);

}

printf("\n\nThe contents of Symbol Table :\n\n");

fp2=fopen("SYMTAB.DAT","r");

ch=fgetc(fp2);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp2);

}

printf("\n\nThe contents of Output file :\n\n");

fp1=fopen("ASSMLIST.DAT","r");

ch=fgetc(fp1);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp1);

}

printf("\n\nThe contents of Object code file :\n\n");

fp4=fopen("OBJCODE.DAT","r");

ch=fgetc(fp4);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp4);

}

fcloseall();

getch();

}

Page 11: 61050810 Symbol Table

INPUT FILES:

INTERMED.DATCOPY START 20002000 ** LDA FIVE2003 ** STA ALPHA2006 ** LDCH CHARZ2009 ** STCH C12012 ALPHA RESW 12015 FIVE WORD 52018 CHARZ BYTE C'EOF'2019 C1 RESB 12020 ** END **

SYMTAB.DATALPHA 2012FIVE 2015CHARZ 2018C1 2019

4.Single pass assembler:#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char opcode[10],operand[10],label[10],a[10],ad[10],symbol[10],ch; char code[10]

[10],code1[10][10]={"33","44","53","57"};

char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};

char mnemonic1[10][10]={"LDA","STA","LDCH","STCH"};

int locctr,start,length,i=0,j=0,k,l=0;

int st,diff,address,add,len,actual_len,finaddr,prevaddr;

FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7;

clrscr();

fp1=fopen("INPUT.DAT","r");

fp2=fopen("SYMTAB.DAT","w");

fp3=fopen("INETERMED.DAT","w");

fscanf(fp1,"%s%s%s",label,opcode,operand);

if(strcmp(opcode,"START")==0)

{

start=atoi(operand);

locctr=start;

Page 12: 61050810 Symbol Table

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",locctr);

if(strcmp(label,"**")!=0)

fprintf(fp2,"%s\t%d\n",label,locctr);

strcpy(code[i],mnemonic[j]);

while(strcmp(mnemonic[j],"END")!=0)

{

if(strcmp(opcode,mnemonic[j])==0)

{

locctr+=3;

break;

}

strcpy(code[i],mnemonic[j]);

j++;

}

if(strcmp(opcode,"WORD")==0)

locctr+=3;

else if(strcmp(opcode,"RESW")==0)

locctr+=(3*(atoi(operand)));

else if(strcmp(opcode,"RESB")==0)

locctr+=(atoi(operand));

else if(strcmp(opcode,"BYTE")==0)

++locctr;

fprintf(fp3,"\t%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;

fcloseall();

printf("\n\nThe contents of Input file:\n\n");

fp1=fopen("INPUT.DAT","r");

ch=fgetc(fp1);

while(ch!=EOF)

{

printf("%c",ch);

Page 13: 61050810 Symbol Table

ch=fgetc(fp1);

}

printf("\n\nLength of the input program is %d.",length);

printf("\n\nThe contents of Symbol Table:\n\n");

fp2=fopen("SYMTAB.DAT","r");

ch=fgetc(fp2);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp2);

}

fcloseall();

fp4=fopen("ASSMLIST.DAT","w");

fp5=fopen("SYMTAB.DAT","r");

fp6=fopen("INTERMED.DAT","r");

fp7=fopen("OBJCODE.DAT","w");

fscanf(fp6,"%s%s%s",label,opcode,operand);

while(strcmp(opcode,"END")!=0)

{

prevaddr=address;

fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);

}

finaddr=address;

fclose(fp6);

fp6=fopen("INTERMED.DAT","r");

fscanf(fp6,"%s%s%s",label,opcode,operand);

if(strcmp(opcode,"START")==0)

{

fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand);

fprintf(fp7,"H^%s^00%s^00%d\n",label,operand,finaddr);

fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);

st=address;

diff=prevaddr-st;

fprintf(fp7,"T^00%d^%d",address,diff);

}

while(strcmp(opcode,"END")!=0)

{

if(strcmp(opcode,"BYTE")==0)

{

fprintf(fp4,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);

len=strlen(operand);

Page 14: 61050810 Symbol Table

actual_len=len-3;

fprintf(fp7,"^");

for(k=2;k<(actual_len+2);k++)

{

itoa(operand[k],ad,16);

fprintf(fp4,"%s",ad);

fprintf(fp7,"%s",ad);

}

fprintf(fp4,"\n");

}

else if(strcmp(opcode,"WORD")==0)

{

len=strlen(operand);

itoa(atoi(operand),a,10);

fprintf(fp4,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);

fprintf(fp7,"^00000%s",a);

}

else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))

fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);

else

{

while(strcmp(opcode,mnemonic1[l])!=0)

l++;

if(strcmp(operand,"COPY")==0)

fprintf(fp4,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code1[l]);

else

{

rewind(fp5);

fscanf(fp5,"%s%d",symbol,&add);

while(strcmp(operand,symbol)!=0)

fscanf(fp5,"%s%d",symbol,&add);

fprintf(fp4,"%d\t%s\t%s\t%s\t%s%d\n",address,label,opcode,operand,code1[l],add);

fprintf(fp7,"^%s%d",code1[l],add);

}

}

fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);

}

fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);

fprintf(fp7,"\nE^00%d",st);

printf("\nObject Program has been generated.");

fcloseall();

Page 15: 61050810 Symbol Table

printf("\n\nObject Program:\n\n");

fp7=fopen("OBJCODE.DAT","r");

ch=fgetc(fp7);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp7);

}

fcloseall();

getch();

}

INPUT FILE:INPUT.DATCOPY START 2000** LDA FIVE** STA ALPHA** LDCH CHARZ** STCH C1ALPHA RESW 1FIVE WORD 5CHARZ BYTE C'EOF'C1 RESB 1** END **

5.Macro processor:

#include<studio.h>

#include<stdlib.h>

#include<conio.h>

#include<string.h>

FILE *f1,*f2,*f3,*f4,*f5;

void main()

{

char lbl[20],opc[20],opr[20],mname[20],arg[20],check[20];char ch,dlbl[20],dopc[20],dopr[

20];

int c;

clrscr();

f1=fopen("MACIN.DAT","r");

rewind(f1);

f2=fopen("NAMETAB.DAT","r");

rewind(f2);

Page 16: 61050810 Symbol Table

f3=fopen("DEFTAB.DAT","r");

f4=fopen("EXPAND.DAT","w");

f5=fopen("ARGTAB.DAT","w");

while(!feof(f1))

{

l1:

fscanf(f1,"%s %s %s",lbl,opc,opr);

if(strcmp(opc,mname)==0)

c=1;

if(strcmp(opc,"MACRO")==0)

{

while(strcmp(opc,"MEND")!=0)

{

fscanf(f1,"%s%s%s",lbl,opc,opr);

continue;

}

goto l1;

}

rewind(f2);

rewind(f3);

fscanf(f2,"%s",mname);

if(strcmp(opc,mname)==0)

{

fprintf(f5," %s",opr);

rewind(f5);

while(!feof(f3))

{

fscanf(f3,"%s%s%s",dlbl,dopc,dopr);

if(strcmp(dopc,"MEND")!=0)

{

if(strcmp(dopc,"MACRO")==0)

{

continue;

}

if(strcmp(dopr,"=X'?1'")==0)

strcpy(dopr,"=X'F1'");

if(strcmp(dopr,"?2,X")==0)

strcpy(dopr,"BUFFER,X");

if(strcmp(dopr,"?3")==0)

strcpy(dopr,"LENGTH");

if(c==1)

Page 17: 61050810 Symbol Table

{

fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr);

c=0;

}

fprintf(f4," %s\t%s\t%s\n",dlbl,dopc,dopr);

}

}

goto l1;

}

fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr);

}

fcloseall();

printf("\n INPUT\n\n Macro Program before expanded \n");

printf(" ---------------------------------\n");

f1=fopen("MACIN.DAT","r");

ch=fgetc(f1);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(f1);

}

printf("\n Definition Table \n");

printf(" ---------------------------------\n");

f2=fopen("DEFTAB.DAT","r");

ch=fgetc(f2);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(f2);

}

printf("\n Name Table \n");

printf(" ---------------------------------\n");

f3=fopen("NAMETAB.DAT","r");

ch=fgetc(f3);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(f3);

}

getch();

clrscr();

Page 18: 61050810 Symbol Table

printf("\n\n OUTPUT\n\n Macro Program after expanded \n");

printf(" ---------------------------------\n\n");

f4=fopen("EXPAND.DAT","r");

ch=fgetc(f4);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(f4);

}

printf("\n Argument Table \n");

printf(" ---------------------------------\n\n");

f5=fopen("ARGTAB.DAT","r");

ch=fgetc(f5);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(f5);

}

fcloseall();

getch();

}

INPUT FILE :

MACIN.DATCOPY START NULLRDBUFF MACRO INDEV,BUFADR,RECLTHNULL CLEAR XNULL CLEAR ANULL CLEAR SNULL +LDT #4096NULL TD =X'&INDEV'NULL JEQ *-3NULL RD =X'&INDEV'NULL COMPR A,SNULL JEQ *+11NULL STCH BUFADR,XNULL TIXR TNULL JLT *-19NULL STX RECLTHNULL MEND NULLFIRST STL RETADRCLOOP RDBUFF F1,BUFFER,LENGTHNULL LDA LENGTHNULL COMP #0NULL JEQ ENDFIL

Page 19: 61050810 Symbol Table

EOF BYTE C'EOF'THREE WORD 3RETADR RESW 1LENGTH RESW 1BUFFER RESB 4096NULL END FIRST

DEFTAB.DATCOPY START NULLRDBUFF MACRO &INDEV,&BUFADR,&RECLTHNULL CLEAR XNULL CLEAR ANULL CLEAR SNULL +LDT #4096NULL TD =X'?1'NULL JEQ *-3NULL RD =X'?1'NULL COMPR A,SNULL JEQ *+11NULL STCH ?2,XNULL TIXR TNULL JLT *-19NULL STX ?3NULL MEND NULL

NAMETAB.DATRDBUFF

6.Absolute loader:

#include<stdio.h>

#include<conio.h>

#include<string.h>

char input[10],label[10],ch1,ch2;

int addr, w=0, start, ptaddr, l, length=0, end, count=0, k, taddr, address, i=0;

FILE *fp1,*fp2;

void check();

void main()

{ clrscr();

fp1=fopen("INPUT.dat","r");

fp2=fopen("OUTPUT.dat","w");

fscanf(fp1,"%s",input);

printf("\n\n\t\t\t\tABSOLUTE LOADER\n");

fprintf(fp2,"\n-------------------------------------------------------\n");

fprintf(fp2,"MEMORY ADDRESS\t\t\tCONTENTS");

fprintf(fp2,"\n-------------------------------------------------------\n");

Page 20: 61050810 Symbol Table

while(strcmp(input,"E")!=0)

{

if(strcmp(input,"H")==0)

{

fscanf(fp1,"%s %x %x %s",label,&start,&end,input);

address=start;

}

else if(strcmp(input,"T")==0)

{

l=length;

ptaddr=addr;

fscanf(fp1,"%x %x %s",&taddr,&length,input);

addr=taddr;

if(w==0)

{

ptaddr=address;

w=1;

}

for(k=0;k<(taddr-(ptaddr+l));k++)

{

address=address+1;

fprintf(fp2,"xx");

count++;

if(count==4)

{

fprintf(fp2," ");

i++;

if(i==4)

{

fprintf(fp2,"\n\n%x\t\t",address);

i=0;

}

count=0;

}

}

if(taddr==start)

fprintf(fp2,"\n\n%x\t\t",taddr);

fprintf(fp2,"%c%c",input[0],input[1]);

check();

fprintf(fp2,"%c%c",input[2],input[3]);

check();

Page 21: 61050810 Symbol Table

fprintf(fp2,"%c%c",input[4],input[5]);

check();

fscanf(fp1,"%s",input);

}

else

{

fprintf(fp2,"%c%c",input[0],input[1]);

check();

fprintf(fp2,"%c%c",input[2],input[3]);

check();

fprintf(fp2,"%c%c",input[4],input[5]);

check();

fscanf(fp1,"%s",input);

}

}

fprintf(fp2,"\n-------------------------------------------------------\n");

fcloseall();

printf("\n\n The contents of output file:\n\n");

fp2=fopen("OUTPUT.DAT","r");

ch2=fgetc(fp2);

while(ch2!=EOF)

{

printf("%c",ch2);

ch2=fgetc(fp2);

}

fcloseall();

getch();

}

void check()

{

count++;

address++;

taddr=taddr+1;

if(count==4)

{

fprintf(fp2," ");

i++;

if(i==4)

{

fprintf(fp2,"\n\n%x\t\t",taddr);

i=0;

Page 22: 61050810 Symbol Table

}

count=0;

}

}

INPUT FILE :

INPUT.DATH COPY 001000 00107AT 001000 1E 141033 482039 001036 281030 301015 482061 3C1003 00102A 0C1039 00102DT 00101E 15 0C1036 482061 081033 4C0000 454F46 000003 000000T 001047 1E 041030 001030 E0205D 30203F D8205D 281030 302057 549039 2C205E 38203FT 001077 1C 101036 4C0000 000000 001000 041030 E02079 302064 509039 DC2079 2C1036E 001000

7.Relocating Loader:#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

void convert(char h[12]);

char bitmask[12];

char bit[12]={0};

void main()

{char add[6],length[10],input[10],binary[12],relocbit,ch,pn[5];

int start,inp,len,i,address,opcode,addr,actualadd,tlen;

FILE *fp1,*fp2;

clrscr();

printf("\n\n Enter the actual starting address : ");

scanf("%x",&start);

fp1=fopen("RLIN.DAT","r");

fp2=fopen("RLOUT.DAT","w");

fscanf(fp1,"%s",input);

fprintf(fp2," ----------------------------\n");

fprintf(fp2," ADDRESS\tCONTENT\n");

fprintf(fp2," ----------------------------\n");

while(strcmp(input,"E")!=0)

{

if(strcmp(input,"H")==0)

{

fscanf(fp1,"%s",pn);

fscanf(fp1,"%x",add);

Page 23: 61050810 Symbol Table

fscanf(fp1,"%x",length);

fscanf(fp1,"%s",input);

}

if(strcmp(input,"T")==0)

{

fscanf(fp1,"%x",&address);

fscanf(fp1,"%x",&tlen);

fscanf(fp1,"%s",bitmask);

address+=start;

convert(bitmask);

len=strlen(bit);

if(len>=11)

len=10;

for(i=0;i<len;i++)

{

fscanf(fp1,"%x",&opcode);

fscanf(fp1,"%x",&addr);

relocbit=bit[i];

if(relocbit=='0')

actualadd=addr;

else

actualadd=addr+start;

fprintf(fp2,"\n %x\t\t%x%x\n",address,opcode,actualadd);

address+=3;

}

fscanf(fp1,"%s",input);

}

}

fprintf(fp2," ----------------------------\n");

fcloseall();

printf("\n\n The contents of output file (RLOUT.DAT):\n\n");

fp2=fopen("RLOUT.DAT","r");

ch=fgetc(fp2);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp2);

}

fclose(fp2);

getch();

}

Page 24: 61050810 Symbol Table

void convert(char h[12])

{

int i,l;

strcpy(bit,"");

l=strlen(h);

for(i=0;i<l;i++)

{

switch(h[i])

{

case '0':

strcat(bit,"0");

break;

case '1':

strcat(bit,"1");

break;

case '2':

strcat(bit,"10");

break;

case '3':

strcat(bit,"11");

break;

case '4':

strcat(bit,"100");

break;

case '5':

strcat(bit,"101");

break;

case '6':

strcat(bit,"110");

break;

case '7':

strcat(bit,"111");

break;

case '8':

strcat(bit,"1000");

break;

case '9':

strcat(bit,"1001");

break;

case 'A':

strcat(bit,"1010");

Page 25: 61050810 Symbol Table

break;

case 'B':

strcat(bit,"1011");

break;

case 'C':

strcat(bit,"1100");

break;

case 'D':

strcat(bit,"1101");

break;

case 'E':

strcat(bit,"1110");

break;

case 'F':

strcat(bit,"1111");

break;

}

}

}

INPUT FILE :

RLIN.DATH COPY 001000 00107AT 001000 1E FFC 14 0033 48 1039 10 0036 28 0030 30 0015 48 1061 3C 0003 20 002A 1C 0039 30 002DT 002500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003E 000000

8.Pass one of a direct link loader:

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct estab

{

char csname[10];

char extsym[10];

int address;

int length;

}es[20];

Page 26: 61050810 Symbol Table

void main()

{

char input[10],name[10],symbol[10],ch; int count=0,progaddr,csaddr,add,len;

FILE *fp1,*fp2;

clrscr();

fp1=fopen("LINP.DAT","r");

fp2=fopen("LOADMAP.DAT","w");

printf("\n\nEnter the address where the program has to be loaded : ");

scanf("%x",&progaddr);

csaddr=progaddr;

fprintf(fp2,"CS_NAME\tEXT_SYM_NAME\tADDRESS\tLENGTH\n");

fprintf(fp2,"--------------------------------------\n");

fscanf(fp1,"%s",input);

while(strcmp(input,"END")!=0)

{

if(strcmp(input,"H")==0)

{

fscanf(fp1,"%s",name);

strcpy(es[count].csname,name);

strcpy(es[count].extsym," ");

fscanf(fp1,"%x",&add);

es[count].address=add+csaddr;

fscanf(fp1,"%x",&len);

es[count].length=len;

fprintf(fp2,"%s\t%s\t\t%x\t

%x\n\n",es[count].csname,es[count].extsym,es[count].address,es[count].length);

count++;

}

else if(strcmp(input,"D")==0)

{

fscanf(fp1,"%s",input);

while(strcmp(input,"R")!=0)

{

strcpy(es[count].csname," ");

strcpy(es[count].extsym,input);

fscanf(fp1,"%x",&add);

es[count].address=add+csaddr;

es[count].length=0;

fprintf(fp2,"%s\t%s\t\t%x\t

%x\n\n",es[count].csname,es[count].extsym,es[count].address,es[count].length);

count++;

Page 27: 61050810 Symbol Table

fscanf(fp1,"%s",input);

}

csaddr=csaddr+len;

}

else if(strcmp(input,"T")==0)

{

while(strcmp(input,"E")!=0)

fscanf(fp1,"%s",input);

}

fscanf(fp1,"%s",input);

}

fprintf(fp2,"--------------------------------------");

fcloseall();

printf("\n The contents of output file:\n\n");

fp2=fopen("LOADMAP.DAT","r");

ch=fgetc(fp2);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp2);

}

fclose(fp2);

getch();

}

INPUT FILE:

LINP.DATH PROGA 000000 000063D LISTA 000040 ENDA 000054R LISTB ENDB LISTC ENDCT 000020 141033 465555 678909 568787 345678T 000054 000014 789087 776555 876666 456666M 000054 06 +LISTCE 000020

H PROGB 000000 00007FD LISTB 000060 ENDB 000070R LISTA ENDA LISTC ENDCT 000036 141033 465555 678909 568787 345678T 000070 000000 789087 776555 876666 456666M 000054 06 +ENDAM 000054 06 -LISTAM 000054 06 +LISTC

Page 28: 61050810 Symbol Table

E

H PROGC 000000 000051D LISTC 000030 ENDC 000042R LISTA ENDA LISTC ENDBT 000018 141033 465555 678909 568787 345678T 000042 000020 789087 776555 876666 456666M 000054 06 +ENDAM 000054 06 -LISTAM 000054 06 +PROGCE END

9.Pass two of a direct link loader:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

struct exttable

{

char csect[20], sname[20];

int padd,plen;

}estab[20];

struct objectcode

{

unsigned char code[15];

int add;

}obcode[500];

void main()

{

FILE *fp1,*fp2,*fp3;

int i,j,x,y,pstart,exeloc,start,textloc,loc,textlen,length,location,st,s;

int n=0,num=0,inc=0,count=0,record=0,mloc[30],mlen[30];

signed long int newadd;

char operation,lbl[10],input[10],label[50][10],opr[30],ch,*add1,address[10];

clrscr();

fp1=fopen("L2IN.dat","r");

fp2=fopen("L1OUT.dat","r");

fp3=fopen("L2OUT.dat","w");

while(!feof(fp2))

{

fscanf(fp2,"%s %s %x %x", estab[num].csect, estab[num].sname, &estab[num].padd,

Page 29: 61050810 Symbol Table

&estab[num].plen);

num++;

}

exeloc=estab[0].padd;

loc=exeloc;

start=loc;

st=start;

while(!feof(fp1))

{

fscanf(fp1,"%s",input);

if(strcmp(input,"H")==0)

{

fscanf(fp1,"%s",input);

for(i=0;i<num;i++)

if(strcmp(input,estab[i].csect)==0)

{

pstart=estab[i].padd;

break;

}

while(strcmp(input,"T")!=0)

fscanf(fp1,"%s",input);

}

do

{

if(strcmp(input,"T")==0)

{

fscanf(fp1,"%x",&textloc);

textloc=textloc+pstart;

for(i=0;i<(textloc-loc);i++)

{

strcpy(obcode[inc].code,"..");

obcode[inc++].add=start++;

}

fscanf(fp1,"%x",&textlen);

loc=textloc+textlen;

}

else if(strcmp(input,"M")==0)

{

fscanf(fp1,"%x",&mloc[record]);

mloc[record]=mloc[record]+pstart;

fscanf(fp1,"%x",&mlen[record]);

Page 30: 61050810 Symbol Table

fscanf(fp1,"%s",label[record++]);

}

else

{

length=strlen(input);

x=0;

for(i=0;i<length;i++)

{

obcode[inc].code[x++]=input[i];

if(x>1)

{

obcode[inc++].add=start++;

x=0;

}

}

}

fscanf(fp1,"%s",input);

}while(strcmp(input,"E")!=0);

if(strcmp(input,"E")==0)

fscanf(fp1,"%s",input);

}

for(n=0;n<record;n++)

{

operation=label[n][0];

length=strlen(label[n]);

for(i=1;i<length;i++)

{

lbl[i-1]=label[n][i];

}

lbl[length-1]='\0';

length=0;

strcpy(address,"\0");

location=mloc[n]-exeloc;

loc=location;

count=0;

while(length<mlen[n])

{

strcat(address,obcode[location++].code);

count++;

length+=2;

}

Page 31: 61050810 Symbol Table

for(i=0;i<num;i++)

{

if(strcmp(lbl,estab[i].csect)==0)

break;

if(strcmp(lbl,estab[i].sname)==0)

break;

}

switch(operation)

{

case '+':

newadd=strtol(address,&add1,16)+(long int)estab[i].padd;

break;

case '-':

newadd=strtol(address,&add1,16)-(long int)estab[i].padd;

break;

}

ltoa(newadd,address,16);

x=0; y=0;

while(count>0)

{

obcode[loc].code[x++]=address[y++];

if(x>1)

{

x=0; loc++;

count--;

}

}}

count=0;

n=0;

s=st-16;

fprintf(fp3,"%x\t",s);

for(i=1;i<=16;i++)

{

fprintf(fp3,"xx");

if(i==4||i==8||i==12)

{

fprintf(fp3,"\t");

}

}

fprintf(fp3,"\n\n%x\t",obcode[0].add);

for(i=0;i<inc;i++)

Page 32: 61050810 Symbol Table

{

fprintf(fp3,"%s",obcode[i].code);

n++;

if(n>3)

{

fprintf(fp3,"\t");

n=0;

count++;

}

if(count>3)

{

fprintf(fp3,"\n\n%x\t",obcode[i+1].add);

count=0;

}}

fcloseall();

printf("\n\t***** PASS TWO OF A DIRECT-LINKING LOADER *****\n");

printf("\nThe contents of the output file (L2OUT.DAT):");

printf("\n---------------------------------------------------------------");

printf("\nAddress\t\t\t\tContents");

printf("\n---------------------------------------------------------------\n");

fp3=fopen("L2OUT.dat","r");

ch=fgetc(fp3);

while(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp3);

}

fclose(fp3);

getch();

}

INPUT FILES:

LINK1IN.DATH PROGA 000000 000063

D LISTA 000040 ENDA 000054

R LISTB ENDB LISTC ENDC

T 000020 0A 03201D 77100004 050014

T 000054 0F 100014 000008 004051 000004 100000

M 000024 05 +LISTB

M 000054 06 +LISTC

Page 33: 61050810 Symbol Table

M 000060 06 +LISTB

M 000060 06 -LISTA

E 000020

H PROGB 000000 00007F

D LISTB 000060 ENDB 000070

R LISTA ENDA LISTC ENDC

T 000036 0B 03100000 772027 05100000

T 000070 0F 100000 000008 004051 000004 100060

M 000037 05 +LISTA

M 00003E 05 +ENDA

M 00003E 05 -LISTA

M 000070 06 +ENDA

M 000070 06 -LISTA

M 000070 06 +LISTC

M 00007C 06 +PROGB

M 00007C 06 -LISTA

E 000000

H PROGC 000000 0000051

D LISTC 000030 ENDC 000042

R LISTA ENDA LISTB ENDB

T 000018 0C 03100000 77100004 05100000

T 000042 0F 100030 000008 004051 000004 100000

M 000019 05 +LISTA

M 00001D 05 +LISTB

M 000021 05 +ENDA

M 000021 05 -LISTA

M 000042 06 +ENDA

M 000042 06 -LISTA

M 000042 06 +PROGC

M 00004E 06 +LISTB

M 00004E 06 -LISTA

E 000000

LINK1OUT.DAT

PROGA ** 4000 63** LISTA 4040** ENDA 4054PROGB ** 4063 7F** LISTB 40C3

Page 34: 61050810 Symbol Table

** ENDB 40D3PROGC ** 40E2 51** LISTC 4112** ENDC 4124

10. Simple text editor:

#include<stdio.h>

#include<conio.h>

#include<process.h>

int i,j,ec,fg,ec2;

char fn[20],e,c;

FILE *fp1,*fp2,*fp;

void Create();

void Append();

void Delete();

void Display();

void main()

{

do {

clrscr();

printf("\n\t\t***** TEXT EDITOR *****");

printf("\n\n\tMENU:\n\t-----\n ");

printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.DELETE\n\t5.EXIT\n");

printf("\n\tEnter your choice: ");

scanf("%d",&ec);

switch(ec)

{

case 1:

Create();

break;

case 2:

Display();

break;

case 3:

Append();

break;

case 4:

Delete();

break;

case 5:

Page 35: 61050810 Symbol Table

exit(0);

}

}while(1);

}

void Create()

{

fp1=fopen("temp.txt","w");

printf("\n\tEnter the text and press '.' to save\n\n\t");

while(1)

{

c=getchar();

fputc(c,fp1);

if(c == '.')

{

fclose(fp1);

printf("\n\tEnter then new filename: ");

scanf("%s",fn);

fp1=fopen("temp.txt","r");

fp2=fopen(fn,"w");

while(!feof(fp1))

{

c=getc(fp1);

putc(c,fp2);

}

fclose(fp2);

break;

}}

}

void Display()

{

printf("\n\tEnter the file name: ");

scanf("%s",fn);

fp1=fopen(fn,"r");

if(fp1==NULL)

{

printf("\n\tFile not found!");

goto end1;

}

while(!feof(fp1))

{

c=getc(fp1);

Page 36: 61050810 Symbol Table

printf("%c",c);

}

end1:

fclose(fp1);

printf("\n\n\tPress any key to continue...");

getch();

}

void Delete()

{

printf("\n\tEnter the file name: ");

scanf("%s",fn);

fp1=fopen(fn,"r");

if(fp1==NULL)

{

printf("\n\tFile not found!");

goto end2;

}

fclose(fp1);

if(remove(fn)==0)

{

printf("\n\n\tFile has been deleted successfully!");

goto end2;

}

else

printf("\n\tError!\n");

end2: printf("\n\n\tPress any key to continue...");

getch();

}

void Append()

{

printf("\n\tEnter the file name: ");

scanf("%s",fn);

fp1=fopen(fn,"r");

if(fp1==NULL)

{

printf("\n\tFile not found!");

goto end3;

}

while(!feof(fp1))

{

c=getc(fp1);

Page 37: 61050810 Symbol Table

printf("%c",c);

}

fclose(fp1);

printf("\n\tType the text and press 'Ctrl+S' to append.\n");

fp1=fopen(fn,"a");

while(1)

{

c=getch();

if(c==19)

goto end3;

if(c==13)

{

c='\n';

printf("\n\t");

fputc(c,fp1);

}

else

{

printf("%c",c);

fputc(c,fp1);

}

}

end3: fclose(fp1);

getch();

}

OUTPUT:

FILE CREATION: