Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
62 views

5014 Symbol Table Program:: (Program Name - Symtab.c)

Thus the single pass assembler is implemented by reading the source file, object code file and symbol table simultaneously to generate the object program with header, text and end records in one pass without creating intermediate files. The source file, object code file and symbol table are the inputs to the single pass assembler.

Uploaded by

Abhishek Bose
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

5014 Symbol Table Program:: (Program Name - Symtab.c)

Thus the single pass assembler is implemented by reading the source file, object code file and symbol table simultaneously to generate the object program with header, text and end records in one pass without creating intermediate files. The source file, object code file and symbol table are the inputs to the single pass assembler.

Uploaded by

Abhishek Bose
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 39

3060820 5014 SYMBOL TABLE Program: (Program name - symtab.

c)
#include<stdio.h> #include<conio.h> #include<string.h> FILE *fp, *fp1; char fname[25],oname[25],label[25],opcode[25],operand[25],ch,x[25],i[25]; char label1[25], lab[25]; int lineno,locctr,choice,j,m,flag; void create(); void search(); void display(); void insert(); void main() { clrscr(); printf("\n\t\t\t Design of Symbol Table"); printf("\n\t\t\t.......................................\n"); printf("Enter the File Name :\t"); gets(fname); fp = fopen(fname,"r"); while(fscanf(fp,"%d%s%s%s",&lineno,label,opcode,operand)!= EOF) { printf("%d\t%s\t%s\t%s\n",lineno,label,opcode,operand); } fclose(fp); do { printf("\n 1 - Create\n 2 - Insert\n 3 - Search\n 4 - Modify\n 5 - Display"); printf("\nEnter the choice : \t"); scanf("%d",&choice); switch(choice) { case 1 : create(); break; case 2 : insert(); break; case 3 : search(); break; case 5 : display();

break; default: printf("\n Invalid Choice \n"); } fflush(stdin); printf(" Do u want to continue : \t"); scanf("%c", &ch); }while(ch=='y' || ch=='Y'); } void create() { fp = fopen(fname,"r"); printf("\nEnter the starting memory address : \t"); scanf ("%X", &locctr); printf("Enter the name of the output file : \t"); scanf("%s", oname); fp1 = fopen (oname, "w"); while (fscanf(fp,"%d%s%s%s", &lineno,label,opcode,operand) != EOF) { if(strcmp(label,"0")==0) locctr = locctr + 3; else { fprintf(fp1,"%s\t%X\n", label, locctr); locctr = locctr + 3; } } fclose(fp1); fclose(fp); } void display() { fp1 = fopen (oname, "r"); printf("\nCONTENTS IN SYMBOL TABLE are\n"); while (fscanf(fp1, "%s%x\n", label, &locctr) != EOF) { printf("%s\t %x\n", label, locctr); } fclose(fp1); } void insert() { fp1 = fopen(oname, "a"); printf("\nEnter the label & its address which u want to insert into symtab \n");

scanf("%s", i); scanf("%x", &j); fprintf(fp1, "%s\t%x\n", i,j); fclose(fp1); } void search() { fp1 = fopen(oname, "r"); printf("\nEnter the label u want to search in symtab : \t"); scanf("%s", x); while(fscanf(fp1, "%s%x", label, &locctr) != EOF) { if (strcmp(label,x) == 0) { printf("%s\t%x\n", label, locctr); flag=0; break; } else flag=1; } fclose(fp1); if (flag == 1) printf("\n Invalid Symbol\n"); } }

Input File
(File name - symin.txt): 1 2 3 4 5 6 0 0 RLOOP 0 ZERO INPUT LDA LDX TD JEQ WORD BYTE ZERO ZERO INPUT RLOOP 0 X 'F1'

OUTPUT
Design of Symbol Table ........................................ Enter the File Name : symin.txt 1 0 LDA ZERO 2 0 LDX ZERO 3 RLOOP TD INPUT 4 0 JEQ RLOOP 5 ZERO WORD 0 6 INPUT BYTE X'F1' 1 - Create 2 - Insert 3 - Search 4 - Modify 5 - Display Enter the choice :

Enter the starting memory address : 1000 Enter the name of the output file : symout.txt Do u want to continue : Y 1 - Create 2 - Insert 3 - Search 4 - Modify 5 - Display Enter the choice : 5 CONTENTS IN SYMBOL TABLE are RLOOP 1006 ZERO 100c INPUT 100f Do u want to continue : y 1 - Create

2 - Insert 3 - Search 4 - Modify 5 Display

Enter the choice : 2 Enter the label & its address which u want to insert into symtab HLT 76 Do u want to continue : Y 1 - Create 2 - Insert 3 - Search 4 - Modify 5 - Display Enter the choice : 5 CONTENTS IN SYMBOL TABLE are RLOOP 1006 ZERO 100c INPUT 100f HLT 76 Do u want to continue : y 1 - Create 2 - Insert 3 - Search 4 - Modify 5 - Display Enter the choice : 3 Enter the label u want to search in symtab : INPUT INPUT100f Do u want to continue : Y 1 - Create 2 - Insert 3 - Search 4 - Modify 5 - Display Enter the choice : 3 Enter the label u want to search in symtab : CCC Invalid Symbol Do u want to continue : N

Result:
Thus the symbol table was created to implement creation, insertion, searching and displaying operands and their respective opcodes.

3060820 5014 PASS 1 OF TWO-PASS ASSEMBLER Program: (Program name pass1.c)


#include<stdio.h> #include<conio.h> #include<string.h> FILE *fp_src, *fp_inter, *fp_symtab; char srcfile[25], label[10], opcode[10], operand[10]; int hexaddr=0x1000, startaddr=0, length; void main() { startaddr=hexaddr; clrscr(); printf ("\n\t\t\tPass 1 of Two-Pass Assembler"); printf ("\n\t\t\t----------------------------\n"); printf("Enter the file name which contains the source assembly language program :\n"); scanf("%s",srcfile); printf("\n\t\t\tThe Source program\n\n"); fp_src=fopen(srcfile,"r"); while(fscanf(fp_src,"%s%s%s",label,opcode,operand)!=EOF) { printf("%s\t%s\t\t%s\n",label,opcode,operand); } fclose(fp_src); printf("\n\t\t\tIntermediate file \n\n");

fp_src=fopen(srcfile,"r"); fp_inter = fopen("inter.txt","w"); fp_symtab=fopen("symtab.txt","w"); fscanf(fp_src,"%s%s%s",label,opcode,operand); if((strcmp(opcode,"START"))==0) { printf("%x\t%s\t%s\t%s\n",hexaddr,label,opcode,operand); fprintf(fp_inter,"%x\t%s\t%s\t %s\n",hexaddr,label,opcode,operand); } while(fscanf(fp_src,"%s%s%s",label,opcode,operand)!=EOF) { if(strcmp(opcode,"END")==0) printf("\t%s\t%s\t%s\n",label,opcode,operand); else { printf("%x\t%s\t%s\t %s\n",hexaddr,label,opcode,operand); fprintf(fp_inter,"%x\t%s\t%s\t %s\n",hexaddr,label,opcode,operand); } if(strcmp(label,"0")!=0) fprintf(fp_symtab,"%s\t%x\n",label,hexaddr); if(strcmp(opcode,"WORD")==0) hexaddr=hexaddr+3; else if(strcmp(opcode,"BYTE")==0) hexaddr=hexaddr+1; else if(strcmp(opcode,"END")!=0) hexaddr=hexaddr+3; } fclose(fp_src); fclose(fp_symtab); printf("\n\t\t\tSymbol Table\n"); fp_symtab=fopen("symtab.txt","r"); length=hexaddr-startaddr; while(fscanf(fp_symtab,"%s%x",label,&hexaddr)!=EOF) { printf("%s\t%x\n",label,hexaddr); } fclose(fp_symtab); printf("\nThe length of the program is %d\n",length); }

Input File
File name source.txt READ 0 0 RLOOP 0 ZERO INPUT 0 START LDA LDX TD JEQ WORD BYTE END 1000 ZERO ZERO INPUT RLOOP 0 X 'F1' 1000

OUTPUT
Pass 1 of Two-Pass Assembler -------------------------------------Enter the file name which contains the source assembly language program : source.txt The Source program READ 0 0 RLOOP 0 ZERO INPUT 0 START LDX LDA TD JEQ WORD BYTE END 1000 ZERO ZERO INPUT RLOOP 0 X 'F1' 1000 Intermediate file 1000 1000 1003 1006 1009 100c READ 0 0 RLOOP 0 ZERO START LDX LDA TD JEQ WORD 1000 ZERO ZERO INPUT RLOOP 0

100f

INPUT 0 1006 100c 100f

BYTE END

X 'F1' 1000 Symbol Table

RLOOP ZERO INPUT

The length of the program is 16

Result:
Thus the pass1 of two-pass assembler is implemented by creating symbol table and intermediate file.

3060820 5014 PASS 2 OF TWO PASS ASSEMBLER Program: (Program name pass2asm.c)
#include<stdio.h> #include<conio.h> #include<string.h> void main() { FILE *hexsht_fp,*inter_fp,*symtab_fp; char hexsheet[15],label[10],opcode[10],operand[10],sourcefile[15]; char mnemonic[7],objcode[7],symbol[7]; int locctr,startaddr,saddr,length; clrscr(); printf("\t\t\tPass 2 of Two-Pass Assembler\n"); printf("\t\t\t--------------------------------------\n"); printf("\nEnter The Hexcode Sheet File : \t"); scanf("%s",hexsheet); printf("Contents in the Hexcode Sheet File\n"); hexsht_fp=fopen(hexsheet,"r");

while(fscanf(hexsht_fp,"%s%s",mnemonic,objcode)!=EOF) fprintf(stdout,"\t%s\t%s\n",mnemonic,objcode); fclose(hexsht_fp); printf("Contents in the SYMTAB\n"); symtab_fp=fopen("symtab.txt","r"); while(fscanf(symtab_fp,"%s%x",symbol,&locctr)!=EOF) printf("\t%s\t%x\n",symbol,locctr); fclose(symtab_fp); printf("\nEnter the name of the intermediate File : \t"); scanf("%s",sourcefile); inter_fp=fopen(sourcefile,"r"); while(fscanf(inter_fp,"%x%s%s%s",&locctr,label,opcode,operand)!=EOF) { if (strcmp(label,"END")==0) printf("\t\t%s\t%s\t%s\n","0",label,opcode); else printf("\t%x\t%s\t%s\t%s\n",locctr,label,opcode,operand); } fclose(inter_fp); printf("\nEnter The Program Length : \t"); scanf("%d",&length);

inter_fp=fopen(sourcefile,"r"); while(fscanf(inter_fp,"%x%s%s%s",&locctr,label,opcode,operand)!=EOF) { if(strcmp(opcode,"START")==0) { startaddr=locctr; printf("Header Record\n"); printf("H\t%s\t%x\t%x\n",label,locctr,length); printf("Text Record\n"); printf("T\t%x\t%x",locctr,length); } hexsht_fp=fopen(hexsheet,"r"); while(fscanf(hexsht_fp,"%s%s",mnemonic,objcode)!=EOF) {

if(strcmp(opcode,mnemonic)==0) printf(\t%s",objcode); } fclose(hexsht_fp); symtab_fp=fopen("symtab.txt","r"); while(fscanf(symtab_fp,"%s%x",symbol,&saddr)!=EOF) { if(strcmp(operand,symbol)==0) printf("%x",saddr); } fclose(symtab_fp); } fclose(inter_fp); printf("\nEnd record\n"); printf("E\t%x\n",startaddr); }

Input Files
File name - hexsheet.txt LDA 3A LDX 3B TD 1A JEQ 4A File name - symtab.txt RLOOP 1006 ZERO 100c INPUT 100f File name - inter.txt 1000 READ START 1000 0 LDX 1003 0 LDA 1006 RLOOP TD 1009 0 JEQ 100c ZERO WORD 100f INPUT BYTE 0 END

1000 ZERO ZERO INPUT RLOOP 0 X'F1' 1000

OUTPUT
Pass 2 of Two-Pass Assembler -------------------------------------Enter The Hexcode Sheet File : hexsheet.txt Contents in the Hexcode Sheet File LDX 3A LDA 3B TD 1A JEQ 4A Contents in the SYMTAB RLOOP 1006 ZERO 100c INPUT 100f Enter the name of the intermediate File : inter.txt 1000 READ START 1000 1000 0 LDX ZERO 1003 0 LDA ZERO 1006 RLOOP TD INPUT 1009 0 JEQ RLOOP

100c ZERO WORD 100f INPUT BYTE 0 END Enter The Program Length : 16 Header Record H READ 1000 10 Text Record T 1000 10 3A100c End record E 1000

0 X'F1' 1000

3B100c

1A100f

4A1006

Result:
Thus the Pass 2 of two-pass assembler is implemented by creating object program from OPTAB, SYMTAB and intermediate file as input.

3060820 5014 SINGLE PASS ASSEMBLER Program: (Program name singlepass.c)


#include<stdio.h> #include<conio.h> #include<string.h> FILE *src_fp,*hexsheet_fp, *symtab_fp; char sfname[25], label[10],opcode[10],operand[10], hexname[25], mnemonics[10], symbol[10]; int code,startaddr,locctr,saddr, len; void main() { clrscr(); printf("\t\t\tSINGLE PASS ASSEMBLER\n");

printf("\t\t\t------------------------------------\n"); printf("\tINPUT\n"); printf("\t------\n"); printf("\nEnter the source file name : \t"); scanf("%s",sfname); src_fp=fopen(sfname,"r"); while(fscanf(src_fp,"%s%s%s",label,opcode,operand)!=EOF) printf("\t%s\t%s\t%s\n",label,opcode,operand); fclose(src_fp); printf("\n Enter the hexcode sheet file : \t"); scanf("%s", hexname); hexsheet_fp = fopen(hexname,"r"); while(fscanf(hexsheet_fp,"%s%x",mnemonics,&code)!=EOF) printf("\t%s\t%x\n",mnemonics,code); fclose(hexsheet_fp); printf("\nEnter the starting memory address : \t"); scanf("%x",&startaddr); locctr = startaddr; src_fp=fopen(sfname,"r"); symtab_fp = fopen("symtab.txt","w"); fscanf(src_fp,"%s%s%s",label,opcode,operand); while(fscanf(src_fp,"%s%s%s",label,opcode,operand)!=EOF) { if(strcmp(label,"0")!=0) fprintf(symtab_fp,"%s\t%x\n", label,locctr); if(strcmp(opcode,"WORD")==0) locctr=locctr+3; else if(strcmp(opcode,"BYTE")==0) locctr=locctr+1; else if(strcmp(opcode,"END")==0) locctr = locctr; else locctr=locctr+3; } fclose(symtab_fp); fclose(src_fp); printf("\t\t\tOUTPUT\n"); printf("\t\t\t------\n"); printf("Symbol Table: \n"); symtab_fp = fopen("symtab.txt","r"); while(fscanf(symtab_fp,"%s%x",symbol,&saddr) != EOF)

printf("\n\t%s\t%x",symbol,saddr ); fclose(symtab_fp); len = locctr - startaddr; printf("\n\nObject Program: \n"); src_fp=fopen(sfname,"r"); fscanf(src_fp,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) printf("\nH\t%s\t%x\t%x",label,startaddr,len); printf("\nT\t%x\t%x\t",startaddr,len); while(fscanf(src_fp,"%s%s%s",label,opcode,operand)!=EOF) { if((strcmp(opcode,"BYTE")!=0) && (strcmp(opcode,"WORD")!=0) && (strcmp(opco de,"END")!=0)) { hexsheet_fp = fopen(hexname,"r"); while(fscanf(hexsheet_fp,"%s%x",mnemonics,&code)!=EOF) { if(strcmp(opcode,mnemonics)==0) { printf("%x",code); symtab_fp = fopen("symtab.txt","r"); while(fscanf(symtab_fp,"%s%x",symbol,&saddr)!= EOF) { if(strcmp(operand, symbol)==0) printf("%x\t", saddr); } fclose(symtab_fp); } } fclose(hexsheet_fp); } else if(strcmp(opcode,"END") ==0) printf("\nE\t%x\n",startaddr); } fclose(src_fp); }

Input Files
File name source.txt

READ START 0 LDX 0 LDA RLOOP TD 0 JEQ ZERO WORD INPUT BYTE 0 END File name - hexsheet.txt LDA 3A LDX 3B TD 1A JEQ 4A

1000 ZERO ZERO INPUT RLOOP 0 X 'F1' 1000

OUTPUT
SINGLE PASS ASSEMBLER ------------------------------------INPUT --------Enter the source file name : source.txt READ START 1000 0 LDA ZERO

0 LDX ZERO RLOOP TD INPUT 0 JEQ RLOOP ZERO WORD 0 INPUT BYTE X'F1' 0 END 1000 Enter the hexcode sheet file : hexsheet.txt LDA 3a LDX 3b TD 1a JEQ 4a Enter the starting memory address : 1000 OUTPUT -----------Symbol Table: RLOOP 1006 ZERO 100c INPUT 100f Object Program: H T E READ 1000 1000 1000 10 10 3a100c 3b100c 1a100f 4a1006

Result:
Thus the single pass assembler is implemented by creating symbol table and object program.

3060820 5014 MACRO PROCESSOR Program :


#include<stdio.h> #include<conio.h> #define add printf("\n\n\t\t ADDITION\n");\ printf("\n Enter the two values:");\ scanf("\t%d\t%d",&x,&y);\ z=x+y;\

printf("ANSWER is %d",z); #define sub printf("\n\n\t\tSUBTRACTION\n");\ printf("\n Enter the two values:");\ scanf("\t%d\t%d",&x,&y);\ z=x-y;\ printf("ANSWER is %d",z); #define mul printf("\n\n\t\t MULTIPLICATION\n");\ printf("\n Enter the two values:");\ scanf("\t%d\t%d",&x,&y);\ z=x*y;\ printf("ANSWERis %d",z); #define div printf("\n\n\t\t DIVISION\n");\ printf("\n Enter the two values:");\ scanf("\t%d\t%d",&x,&y);\ z=x/y;\ printf("ANSWER is %d",z);

void main() { int x,y,z; clrscr(); add; sub; mul; div; }

Result :
Thus the macros for addition, subtraction, multiplication and division was created .

3060820 5014 ABSOLUTE LOADER Program:


#include<stdio.h> #include<conio.h>

#include<string.h> #include<stdlib.h> struct object_code { int locctr; char byte[5]; }; struct object_code code[200]; void main() { FILE *in_fp,*out_fp; char input[15]; int i,textloc,tlen, loc, len,n=0,count=0, inc=0, tloc=0,num=0; clrscr(); printf("\t\t\t\tABSOLUTE LOADER\n"); printf("\t\t\t\t----------------\n"); printf("\nThe object program to be loaded in Main Memeory is: "); printf("\n--------------------------------------------\n\n"); in_fp=fopen("loadin.txt","r"); while( (ch = fgetc(in_fp)) != EOF) printf("%c", ch); fclose(in_fp); printf(" \nLoaded Program : "); printf(" \n------------------\n\n"); in_fp=fopen("loadin.txt","r"); out_fp=fopen("loadout.txt","w"); rewind(in_fp); rewind(out_fp); fscanf(in_fp,"%s",input); if(strcmp(input,"H")==0) { for(i=0;i<4;i++) { if(i==1) fscanf(in_fp,"%x",&loc); else fscanf(in_fp,"%s",input); } } tloc=loc;

while(strcmp(input,"E")!=0) { if(strcmp(input,"T")==0) { fscanf(in_fp,"%x",&textloc); for(i=0;i<(textloc-(tloc+tlen));i++) { strcpy(code[inc].byte,"xx"); code[inc++].locctr=loc++; } fscanf(in_fp,"%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(in_fp,"%s",input); } n=0; i=0; count=0; fprintf(out_fp,"%x\t",code[i].locctr); printf("%x\t",code[i].locctr); for(i=0;i<inc;i++) { fprintf(out_fp,"%s",code[i].byte); printf("%s",code[i].byte); n++; if(n>3) { fprintf(out_fp,"\t"); printf("\t");

n=0; count++; } if(count>3) { fprintf(out_fp,"\n%x\t",code[i+1].locctr); printf("\n%x\t",code[i+1].locctr); count=0; } } }

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

OUTPUT
ABSOLUTE LOADER ------------------------------The object program to be loaded in main memory is: ---------------------------------------------------------------H T T T T T E COPY 002000 282030 0C2039 00201E 4C0000 002039 30303F 2C305E 002057 201000 002071 4FA039 15 002000 002000 1E 302015 00202D 15 454F46 1E D8305D 38303F A 19 DC3079 00107A 142033 483061 2C2036 200003 242030 282030 102036 342030 2C2036 483039 3C2003 483061 100000 302030 303057 4C0000 E03079 383064 102036 00202A 182033 E0305D 53A039 F1 303064 4C0000

Loaded Program : ---------------------2000 2010 2020 2030 2040 2050 2060 2070 2080 14203348 30613C20 36483061 100000xx 305D3030 392C305E 00xxxxxx xx342030 2C203638 30391020 0300202A 1820334C xxxxxxxx 3FD8305D 38303F10 xxxxxxxx E0307930 30644C00 36282030 0C203900 0000454F xx242030 28203030 20364C00 xxxxxxxx 30644FA0 0015 30201548 202D2C20 46200003 302030E0 305753A0 00F12010 xxxxxxxx 39DC3079

Result:
Thus the program for absolute loader is implemented by loading the object program from secondary memory into main memory at the location mentioned in the Header Record of object program.

3060820 5014
PASS 1 OF DIRECT LINKING LOADER

Program:
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #define MAX 10 struct estab { char csect[10]; char sym_name[10]; long int add; int length; } table[MAX]; void main() { FILE *fp1,*fp2; char input[10]; long int i,count=0,start,length,loc; clrscr(); printf("\t\t\tPass1 of Direct Linking Loader\n"); printf("\t\t\t-------------------------------\n"); fp1=fopen("link1in.txt","r"); fp2=fopen("link1out.txt","w"); printf("Enter the location where the program has to be loaded: "); scanf("%lx",&start); printf ("The intermediate file of pass1 of DLL is:\n\n"); fprintf(fp2,"CSECT\tSymname\tAddress\tLength\n"); printf("CSECT\tSymname\tAddress\tLength\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,"\0");

fscanf(fp1,"%s",input); table[count].add=atoi(input)+start; fscanf(fp1,"%s",input); length=atoi(input); table[count++].length=atoi(input); fscanf(fp1,"%s",input); } if(strcmp(input,"D")==0) { fscanf(fp1,"%s%lx",input,&loc); while((strcmp(input,"R")!=0)) { strcpy(table[count].csect,"\0"); strcpy(table[count].sym_name,input); table[count].add=loc+start; table[count++].length=0; fscanf(fp1,"%s%lx",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\t%s\t\t%lx\t\t%d\n", table[i].csect, table[i].sym_name, table[i].add, table[i].length); printf("%s\t\t%s\t\t%lx\t\t%d\n", table[i].csect, table[i].sym_name, table[i].add, table[i].length); } fclose(fp2); }

Input File
(File name link1in.txt): H PROGA 000000 000070

D LISTA 000040 R LISTB ENDB T 000020 10 T 000054 16 100014 ffffc0 M 000024 05 M 000054 06 M 000058 06 M 000064 06 E 000000 H D R T T M M M M M M E H D R T T M M M M M M E PROGB LISTB LISTA 000036 05100000 000070 05100020 000037 000044 000070 000074 000078 000082 000000 000000 000060 11 18

ENDA 000054 LISTC ENDC 03201D 77100004 100014 15100006 +LISTB +LISTC +ENDC +LISTB 000088 ENDB ENDA

150014 00002f

000070 LISTC 03100000 100000 05100006 100000 +LISTA +ENDA +ENDA +ENDC +ENDC +ENDA

ENDC 772027

05100030 05 05 06 06 06 06

PROGC 000000 000057 LISTC 000030 ENDC 000042 LISTA ENDA LISTB ENDB 000018 12 03100000 77100004 000042 15 100030 100008 100000 100000 000019 05 +LISTA 000023 05 +LISTB 000027 05 +ENDA 000048 06 +LISTA 000051 06 +ENDA 000054 06 +LISTB 000000

05100000 100011

OUTPUT
Pass1 of Direct Linking Loader -------------------------------------Enter the location where the program has to be loaded: 1000 The intermediate file of Pass1 of DLL is: CSECT PROGA PROGB LISTB ENDB PROGC LISTC ENDC Symname LISTA ENDA Address 1000 1040 1054 1046 10a6 10b6 109e 10ce 10e0 Length 70 0 0 88 0 0 57 0 0

Result:
Thus the Pass 1 of Direct Linking Loader was implemented by creating ESTAB containing Control Section names, their starting address, length and External Definitions and External References.

30608205014 Pass 2 of Direct Linking Loader Program:


#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> struct ext_table { char csect[10]; char sname[10]; int padd; int plen; } estab[20]; struct object_code { char code[15]; int add; } obcode[500]; void main() { FILE *fp1,*fp2,*fp3; int i,j,x,y, pstart, exeloc, start, textloc, loc, length, location; int mloc[30], textlen, mlen[30], n=0, num=0, inc=0, count=0, record=0, long int newadd; char *add1,operation,lbl[10],input[10],label[30][10],address[10]; clrscr(); printf("\t\t\tPass2 of Direct Linking Loader\n"); printf("\t\t\t------------------------------\n"); printf("The Linked and Loaded Object Program looks like: \n\n"); fp1=fopen("link2in.txt","r");

fp2=fopen("link1out.txt","r"); fp3=fopen("link2out.txt","w"); rewind(fp1); rewind(fp2); rewind(fp3); while(!feof(fp2)) { fscanf(fp2,"%s%s%d%d", estab[num].csect, estab[num].sname, &estab[num].padd, &estab[num].plen); num++; } exeloc=estab[0].padd; loc=exeloc; start=loc; 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,"%d",&textloc); textloc=textloc+pstart; for(i=0;i<(textloc-loc);i++) { strcpy(obcode[inc].code,"xx"); obcode[inc++].add=start++; } fscanf(fp1,"%d",&textlen); loc=textloc+textlen;

} else if(strcmp(input,"M")==0) { fscanf(fp1,"%d",&mloc[record]); mloc[record]=mloc[record]+pstart; fscanf(fp1,"%d",&mlen[record]); 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;

} for(i=0;i<num;i++) if(strcmp(lbl,estab[i].sname)==0) break; switch(operation) { case '+':newadd=strtol(address,&add1,10)+(long int)estab[i].padd; break; case '-':newadd=strtol(address,&add1,10)-(long int)estab[i].padd; break; } ltoa(newadd,address,10); x=0; y=0; while(count>0) { obcode[loc].code[x++]=address[y++]; if(x>1) { x=0; loc++; count--; } } } count=0; n=0; fprintf(fp3,"%d\t",obcode[0].add); printf( "%d\t",obcode[0].add); for(i=0;i<inc;i++) { fprintf(fp3,"%s",obcode[i].code); printf( "%s",obcode[i].code); n++; if(n>3) { fprintf(fp3,"\t"); printf("\t"); n=0; count++; }

if(count>3) { fprintf(fp3,"\n%d\t",obcode[i+1].add); printf( "\n%d\t", obcode[i+1].add); count=0; } } }

Input Files
File Name - link2in.txt H PROGA 000000 000070 D LISTA 000040 ENDA 000054 R LISTB ENDB LISTC ENDC T 000020 10 03201D 77100004 T 000054 16 100014 15100006 100014 ffffc0 M 000024 05 +LISTB M 000054 06 +LISTC M 000058 06 +ENDC M 000064 06 +LISTB E 000000 H D R T T M M M PROGB LISTB LISTA 000036 05100000 000070 05100020 000037 000044 000070 000000 000060 11 18 05100030 05 05 06 000088 ENDB ENDA 000070 LISTC 03100000 100000 05100006 100000 +LISTA +ENDA +ENDA

150014 00002f

ENDC 772027

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

000074 000078 000082 000000

06 06 06

+ENDC +ENDC +ENDA

PROGC 000000 000057 LISTC 000030 ENDC 000042 LISTA ENDA LISTB ENDB 000018 12 03100000 77100004 000042 15 100030 100008 100000 100000 000019 05 +LISTA 000023 05 +LISTB 000027 05 +ENDA 000048 06 +LISTA 000051 06 +ENDA 000054 06 +LISTB 000000

05100000 100011

File Name - estab.txt CSECT PROGA PROGB LISTB ENDB PROGC LISTC ENDC Symname LISTA ENDA Address 1000 1040 1054 1046 10a6 10b6 109e 10ce 10e0 Length 70 0 0 88 0 0 57 0 0

OUTPUT
0 16 32 48 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx

64 80 96 112 128 144 160 176 192 208 224

xxxxxxxx 0xxxxxx 04150014 xxxxxxxx 10000600 77202705 xxxx0xx 00051000 03100000 xxxxxxxx 11100000

xxxxxxxx 0xxxxxx xxxxxxxx xxxxxxxx 22f1000 100000xx xxxx0xx 06051000 77100004 xxxxxxxx 100000

xxxxxxxx xxxx0320 xx0xxxx xxxxxxxx 14ffffc0 xxxxxxxx xxxx0xx 20051000 05100000 10003010

0xxxxxx 1D771000 0xxxx0 101415 03100000 xxxx0xx xxxx1000 30100000 xxxxxxxx 00081000

Result:
Thus the Pass 1 of Direct Linking Loader was implemented by creating ESTAB containing Control Section names, their starting address, length and External Definitions and External References.

30608205014 TEXT EDITOR Program:


#include <stdio.h> #include <string.h> #include <conio.h> void insert(); void append(); void erase(); void display(); FILE *fp; char c, fname[20]; void main() { clrscr(); printf("\n\t\t\tTEXT EDITOR\n\n"); printf("\nEnter the File name: \t"); scanf("%s", fname); while(1) {

int ch; printf("\n1.Insert \n2.Append \n3.Erase \n4.Display \n5.Exit"); printf("\nEnter your Choice :\t"); scanf ("%d", &ch); switch (ch) { case 1: insert (); break; case 2: append (); break; case 3: erase (); break; case 4: display (); break; case 5: exit (0); } } } void insert() { printf("\nEnter the text to be inserted (press ctl+z to end) : \n "); fp = fopen(fname, "w"); while( (c=getchar()) != EOF ) putc(c,fp); fclose(fp); } void append() { fp = fopen ( fname, "a"); printf("\n Enter the text to be appended (press ctrl+z to end) : \n"); while ( ( c=getchar() ) != EOF) putc(c,fp); fclose (fp); } void erase() { fp = fopen ( fname, "w"); fflush (fp); printf("\n The file contents have been Deleted completely \n"); } void display() { fp = fopen ( fname, "r"); rewind (fp); printf("\n The text in the file is : \n");

while( (c=getc(fp)) != EOF ) printf("%c", c); fclose(fp); }

OUTPUT
TEXT EDITOR Enter the File name: 1.Insert 2.Append 3.Erase 4.Display 5.Exit Enter your Choice : test.txt

Enter the text to be inserted (press ctl+z to end) : This is a program to demonstrate text editor operations like insertion, deletion and display of text. ^Z 1.Insert 2.Append

3.Erase 4.Display 5.Exit Enter your Choice : The text in the file is :

This is a program to demonstrate text editor operations like insertion, deletion and display of text. 1.Insert 2.Append 3.Erase 4.Display 5.Exit Enter your Choice :

Enter the text to be appended (press ctrl+z to end) : HALT ^Z 1.Insert 2.Append 3.Erase 4.Display 5.Exit Enter your Choice : The text in the file is : This is a program to demonstrate text editor operations like insertion, deletion and display of text. HALT 1.Insert 2.Append 3.Erase 4.Display 5.Exit 4

Enter your Choice :

The file contents have been Deleted completely 1.Insert 2.Append 3.Erase 4.Display 5.Exit Enter your Choice : The text in the file is : 1.Insert 2.Append 3.Erase 4.Display 5.Exit Enter your Choice : 5

Result :
Thus the operations of text editor like, insertion, appending, deletion and displaying the contents of a file were implemented.

You might also like