Académique Documents
Professionnel Documents
Culture Documents
STAFF INCHARGE
HOD
PRINCIPAL
CS2308
0 0 3 2
Implement a symbol table with functions to create, insert, modify, search, and display. 1. Implement pass one of a two pass assembler. 2. Implement pass two of a two pass assembler. Implement a single pass assembler. Implement a two pass macro processor Implement a single pass macro processor. 3. Implement an absolute loader. 4. Implement a relocating loader. 5. Implement pass one of a direct-linking loader. 6. Implement pass two of a direct-linking loader. 7. Implement a simple text editor with features like insertion / deletion of a character, word, and sentence. 8. Implement a symbol table with suitable hashing (For loader exercises, output the snap shot of the main memory as it would be, after the loading has taken place)
Implement a symbol table with functions to create, insert, modify, search, and display.
Aim: To write a C program to implement a Symbol Table Algorithm: 1 Start the program for performing insert, display, delete, search and modify option in symbol table. 2 Define the structure of the Symbol Table 3 Enter the choice for performing the operations in the symbol Table 4 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. 5 If the entered choice is 2, the symbols present in the symbol table are displayed. 6 If the entered choice is 3, the symbol to be deleted is searched in the symbol table. 7 If it is not found in the symbol table it displays Label Not found. Else, the symbol is deleted. 8 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 Code program in c implement symbol table
# include <stdio.h> # include <conio.h> # include <alloc.h> # include <string.h> # define null 0 int size=0; void insert(); void del(); int search(char lab[]); void modify(); void display(); struct symbtab { char label[10]; int addr; struct symtab *next; }; struct symbtab *first,*last;
void main() { int op; int y; char la[10]; clrscr(); do { printf("\nSYMBOL TABLE IMPLEMENTATION\n"); printf("1. INSERT\n"); printf("2. DISPLAY\n"); printf("3. DELETE\n"); printf("4. SEARCH\n"); printf("5. MODIFY\n"); printf("6. END\n"); printf("Enter your option : "); scanf("%d",&op); switch(op) { case 1: insert(); display(); break; case 2: display(); break; case 3: del(); display(); break; case 4: printf("Enter the label to be searched : "); scanf("%s",la); y=search(la); if(y==1) { printf("The label is already in the symbol Table"); } else { printf("The label is not found in the symbol table"); } break; case 5: modify(); display(); break; case 6: break; } }
while(op<6); getch(); } void insert() { int n; char l[10]; printf("Enter the label : "); scanf("%s",l); n=search(l); if(n==1) { printf("The label already exists. Duplicate cant be inserted\n"); } else { struct symbtab *p; p=malloc(sizeof(struct symbtab)); strcpy(p->label,l); printf("Enter the address : "); scanf("%d",&p->addr); p->next=null; if(size==0) { first=p; last=p; } else { last->next=p; last=p; } size++; } } void display() { int i; struct symbtab *p; p=first; printf("LABEL\tADDRESS\n"); for(i=0;i<size;i++) { printf("%s\t%d\n",p->label,p->addr); p=p->next; } } int search(char lab[]) { int i,flag=0; struct symbtab *p;
p=first; for(i=0;i<size;i++) { if(strcmp(p->label,lab)==0) { flag=1; } p=p->next; } return flag; } void modify() { char l[10],nl[10]; int add, choice, i, s; struct symbtab *p; p=first; printf("What do you want to modify?\n"); printf("1. Only the label\n"); printf("2. Only the address of a particular label\n"); printf("3. Both the label and address\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the old label\n"); scanf("%s",l); printf("Enter the new label\n"); scanf("%s",nl); s=search(l); if(s==0) { printf("NO such label"); } else { for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) { strcpy(p->label,nl); } p=p->next; } } break; case 2: printf("Enter the label whose address is to modified\n"); scanf("%s",l); printf("Enter the new address\n");
scanf("%d",&add); s=search(l); if(s==0) { printf("NO such label"); } else { for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) { p->addr=add; } p=p->next; } } break; case 3: printf("Enter the old label : "); scanf("%s",l); printf("Enter the new label : "); scanf("%s",nl); printf("Enter the new address : "); scanf("%d",&add); s=search(l); if(s==0) { printf("NO such label"); } else { for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) { strcpy(p->label,nl); p->addr=add; } p=p->next; } } break; } } void del() { int a; char l[10]; struct symbtab *p,*q; p=first;
printf("Enter the label to be deleted\n"); scanf("%s",l); a=search(l); if(a==0) { printf("Label not found\n"); } else { if(strcmp(first->label,l)==0) { first=first->next; } else if(strcmp(last->label,l)==0) { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; } p->next=null; last=p; } else { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; } p->next=q->next; } size--; } }
{ fprintf(fp3,"%d",locctr); if(strcmp(label,"**")!=0) fprintf(fp2,"%s\t%d\n",label,locctr); strcpy(code[j],mnemonic[j]); while(strcmp(mnemonic[j],"END")!=0) { if(strcmp(opcode,mnemonic[j])==0) { locctr+=3; break; } strcpy(code[j],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," %s %s %s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); length=locctr-start; } fprintf(fp3,"%d %s %s %s\n",locctr,label,opcode,operand); fcloseall(); getch(); printf("\n\n contents of the input file \n\n"); fp1=fopen("INPUT.DAT","r"); ch1=fgetc(fp1); while(ch1!=EOF) { printf("%c",ch1); ch1=fgetc(fp1); } getch(); printf("\n\n contents of the intermediate file\n\n"); fp3=fopen("OUTPUT.DAT","r"); ch2=fgetc(fp3); while(ch2!=EOF) { printf("%c",ch2); ch2=fgetc(fp3); } getch(); printf("\n\n word count from intermediate file\n"); wordcount(); printf("\n\n length of the intermediate file : %d",length); getch(); printf("\n\n contents of the symbol table\n\n"); fp2=fopen("SYMTAB.DAT","r"); ch3=fgetc(fp2); while(ch3!=EOF) { printf("%c",ch3); ch3=fgetc(fp2); }
fcloseall(); getch(); } void wordcount() { FILE *fp4; char c; int word=0,l=1; fp4=fopen("OUTPUT.DAT","r"); c=fgetc(fp4); while(c!=EOF) { if(c==' ') word+=1; if(c=='\n') { word+=1; printf("\n number of words in line %d: %d",l,word); l++; word=0; } c=fgetc(fp4); } }
Aim To implement pass two algorithm of pass two assembler Algorithm 1 Start the program 2 Initialize all the variables 3 Open a file by name fp1=fopen("assmlist.dat","w"); fp2=fopen("symtab.dat","r"); fp3=fopen("intermediate.dat","r"); fp4=fopen("optab.dat","r"); 4 Read the content of the file 5 If opcode is BYTE 6 if(strcmp(opcode,"BYTE")==0) Then fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand); Else if opcode is WORD else if(strcmp(opcode,"WORD")==0) then fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a); Else perform else if((strcmp(opcode,"RESB")==0)(strcmp(opcode,"RESW")==0)) fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand); if it is not math anything else fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",address,label,opcode,operand,code); 7 Finally terminate the of pass two of pass two assembler Source code program in c Implementation of Pass Two Assembler Algorithm for Two Pass Assembler
#include<stdio.h> #include<conio.h>
#include<stdlib.h> #include<string.h> void main() { char a[10],ad[10],label[10],opcode[10],operand[10],symbol[10],ch1,ch2,ch3,ch4; int i,address,st,add,len,actual_len,finaddr,prevaddr,j=0,dif; char mnemonic[15][15]={"LDA","STA","LDCH","STCH"}; char code[15][15]={"33","44","53","57"}; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("listfile.dat","w"); fp2=fopen("sim.dat","r"); fp3=fopen("int.dat","r"); fp4=fopen("ocode.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("int.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; dif=prevaddr-st; fprintf(fp4,"T^00%d^%d",address,dif); } while(strcmp(opcode,"END")!=0) { 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); } 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); } fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand); fprintf(fp4,"\nE^00%d",finaddr); printf("\n intermediate file is converted into object code"); fcloseall(); printf("\n\n The content of the the intermediate file:\n\n"); fp3=fopen("output.dat","r"); ch1=fgetc(fp3); while(ch1!=EOF) { printf("%c",ch1); ch1=fgetc(fp3); } printf("\n\n The content of the symbol table:\n\n"); fp2=fopen("sim.dat","r"); ch2=fgetc(fp2); while(ch2!=EOF) { printf("%c",ch2); ch2=fgetc(fp2); } printf("\n\n The content of the output file:\n\n"); fp1=fopen("listfile.dat","r"); ch3=fgetc(fp1); while(ch3!=EOF) { printf("%c",ch3); ch3=fgetc(fp1); } printf("\n\n Object code file:\n\n"); fp4=fopen("ocode.dat","r"); ch4=fgetc(fp4); while(ch4!=EOF) { printf("%c",ch4); ch4=fgetc(fp4); } fcloseall(); getch(); }
if it is not math anything else fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",address,label,opcode,operand,code); 17 both passes will get executed of single pass assembler and program will generate object code file. Source program in c Implementation of Single Pass Assembler
#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void pass1(); void pass2(); void indisp(); void outdisp(); FILE *fp1,*fp2,*fp3,*fp4; void main() { clrscr(); printf("\t\t ****** SINGLE PASS ASSEMBLER ******\n"); indisp(); sleep(3); pass1(); sleep(3); pass2(); sleep(3); outdisp(); sleep(3); getch(); } void pass1() { char opcode[10],operand[10],label[10],code[10][10]; char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"}; int locctr,start,length,j=0; fp1=fopen("INPUT25.DAT","r"); fp2=fopen("SYMTAB25.DAT","w"); fp3=fopen("INTER25.DAT","w"); fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { start=atoi(operand); locctr=start; fprintf(fp3,"%s %s %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[j],mnemonic[j]);
while(strcmp(mnemonic[j],"END")!=0) { if(strcmp(opcode,mnemonic[j])==0) { locctr+=3; break; } strcpy(code[j],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," %s %s %s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); } fprintf(fp3,"%d %s %s %s\n",locctr,label,opcode,operand); fcloseall(); length=locctr-start; printf("\n\n Symbol Table is generated..........."); printf("\n Intermediate file is generated......."); printf("\n Program length of the intermediate file is %d",length); } void pass2() { char a[10],ad[10],label[10],opcode[10],operand[10],symbol[10]; 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"}; fp1=fopen("listfile25.dat","w"); fp2=fopen("SYMTAB25.dat","r"); fp3=fopen("INTER25.dat","r"); fp4=fopen("ocode25.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("INTER25.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) { 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,"*")==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); } fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand); fprintf(fp4,"\n E^00%d",st); printf("\n Listing file is generated.........."); printf("\n Object code file is generated.........."); fcloseall(); } void indisp() { char ch1; printf("\nThe contents of Input file\n"); fp1=fopen("INPUT25.DAT","r"); ch1=fgetc(fp1); while(ch1!=EOF) { printf("%c",ch1); ch1=fgetc(fp1); } } void outdisp() { char ch4; printf("\n\n Object code file\n");
OUTPUT
Source code program in C Implementation of Macro Processor #include<stdio.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); f3=fopen("deftab.dat","r"); f4=fopen("expand.dat","w"); f5=fopen("argment.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'&INDEV'")==0) strcpy(dopr,"=X'F1'"); if(strcmp(dopr,"BUFADR,X")==0) strcpy(dopr,"BUFFER,X"); if(strcmp(dopr,"RECLTH")==0) strcpy(dopr,"LENGTH"); if(c==1) { 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(); 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("argment.DAT","r"); ch=fgetc(f5); while(ch!=EOF) { printf("%c",ch); ch=fgetc(f5); } fcloseall(); getch(); } OUTPUT
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(); 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 Output from FILE2.DAT\n\n"); fp2=fopen("FILE2.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; } count=0; }
SOURCE CODE # include <stdio.h> # include <conio.h> # include <string.h> # include <stdlib.h> # include <math.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,ch1; int start,inp,len,i,address,opcode,addr,actualadd,tlen; FILE *fp1,*fp2; clrscr(); printf("Enter the actual starting address : "); scanf("%x",&start); fp1=fopen("rein.dat","r"); fp2=fopen("reout.dat","w"); fscanf(fp1,"%s",input); fprintf(fp2,"Address\t\tContent\n"); fprintf(fp2,"----------------------------\n"); while(strcmp(input,"E")!=0) { if(strcmp(input,"H")==0) { fscanf(fp1,"%x",add); 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); } } fcloseall(); printf("\n\nOutput from REOUT.DAT\n"); fp2=fopen("REOUT.DAT","r"); ch1=fgetc(fp2); while(ch1!=EOF) { printf("%c",ch1); ch1=fgetc(fp2); } fclose(fp2); getch(); } 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"); 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 //REIN.DAT H 001000 00107A T 001000 1E FFC 14 0033 48 1039 10 0036 28 0030 30 0015 48 1061 3C 0003 20 002A 1C 0039 30 002D T 002500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003 E 000000 OUTPUT
Algorithm 1 Star the program for linking loader 2 Assign the necessary variable and the header variable 3 Open the two file 3.1 In fp1 for link input file for read privilege 3.2 And fp2 for load map file for write privilege 4 Read the character until the input is not equal to END 5 And the check the character in if condition input is H then 6 Get the name from fp1 6.1 Copy the name between csname=name 6.2 And extsym=** 6.3 Else if the character is D 6.4 Then get the input variable 7 In these if condition perform the while loop the read character until the input is not
equal to R 7.1 The copy csnmae=** 7.2 And extsym=input 7.3 Then add address =add+csaddr 7.4 And length is equal to zero 8 And perform the operation (see the Source code) 9 Finally terminate the program SOURCE PROGRAM IN C IMPLEMENTATION OF PASS 1 DIRECT LINKING LOADER ALGORITHM
# include <stdio.h> # include <conio.h> # include <string.h> # define MAX 20 struct estab { char csname[10]; char extsym[10]; int address; int length; }es[MAX]; void main() { char input[10],name[10],symbol[10]; int count=0,progaddr,csaddr,add,len; FILE *fp1,*fp2; clrscr(); fp1=fopen("linkinput.dat","r"); fp2=fopen("loadmap.dat","w"); printf("Enter the location where the program has to be loaded : "); scanf("%d",&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,"%d",&add); es[count].address=add+csaddr; fscanf(fp1,"%d",&len);
es[count].length=len; fprintf(fp2,"%s\t%s\t\t%d\t%d\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,"%d",&add); // printf("CSADDR = %d",csaddr); es[count].address=add+csaddr; es[count].length=0; fprintf(fp2,"%s\t%s\t\t%d\t%d\n",es[count].csname,es[count].extsym,es[count].address,es[count].length); count++; 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,"--------------------------------------\n"); fclose(fp1); fclose(fp2); printf("FINISHED\n"); getch(); }
INPUT FILE : LINKINPUT.DAT H PROGA 0000 1000 D LISTA 0040 ENDA 0054 R LISTB ENDB LISTC ENDC T 0020 141033 465555 678909 568787 345678 T 0054 000014 789087 776555 876666 456666 M 0054 06 +LISTC E 0000
H PROGB 0000 2000 D LISTB 0040 ENDB 0054 R LISTA ENDA LISTC ENDC T 0020 141033 465555 678909 568787 345678 T 0054 000000 789087 776555 876666 456666 M 0054 06 +ENDA M 0054 06 -LISTA M 0054 06 +LISTC E 0000 H PROGC 0000 3000 D LISTC 0040 ENDC 0054 R LISTA ENDA LISTC ENDB T 0020 141033 465555 678909 568787 345678 T 0054 000020 789087 776555 876666 456666 M 0054 06 +ENDA M 0054 06 -LISTA M 0054 06 +PROGC E 0000 END OUTPUT: Enter the location where the program has to be loaded : 5000 LOADMAP.DAT CS_NAME EXT_SYM_NAME ADDRESS LENGTH -----------------------------------------------PROGA ** 5000 1000 ** LISTA 5040 0 ** ENDA 5054 0 PROGB ** 6000 2000 ** LISTB 6040 0 ** ENDB 6054 0 PROGC ** 8000 3000 ** LISTC 8040 0 ** ENDC 8054 0
SOURCE CODE #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct exttable { char csect[20]; char 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("GANinP.dat","r"); fp2=fopen("L1OUT25.dat","r"); fp3=fopen("L2OUT1.dat","w"); while(!feof(fp2)) { fscanf(fp2,"%s %s %x %x",estab[num].csect,estab[num].sname, &estab[num].padd,&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]); 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].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++) { 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 Output from L2OUT1.DAT\n\n"); printf("\n Address\t\t\t Contents\n\n"); printf("\n-----------------------------------------------------------\n\n"); fp3=fopen("L2OUT1.dat","r"); ch=fgetc(fp3); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp3); } fclose(fp3); getch(); }
INPUT //L2IN25.DAT H 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 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 //L1OUT25.DAT PROGA ** 4000 63 ** LISTA 4040 ** ENDA 4054 PROGB ** 4063 7F ** LISTB 40C3 ** ENDB 40D3 PROGC ** 40E2 51 ** LISTC 4112 ** ENDC 4124
OUTPUT
10 Implement a simple text editor with features like insertion / deletion of a character, word, and sentence
Aim: Algorithm:
1. Display options new, open and exit and get choice. 2. If choice 1 , call new. 3. If choice 2, call open. 4. else, exit. 5 new() Get the file name and open it in write mode. Get the text from the user to write it. 6 open() Get the file name from user. If present then open it in append mode. Display options modify, search, back and get choice. If choice 1, call modify. If choice 2, call search. Exit 7 modify() Get the text to add the existing file. Write it into the file and save the file. 8 Search() Get the string to search. If it is present then print word found. Else, print word not found.
# include <fstream.h> char filename[15]; char buff[1000]; int curx,cury,count; void cur_pos() { curx=wherex(); cury=wherey(); textcolor(12); textbackground(9); gotoxy(35,1); cout<<"\n"; cout<<"##############################################\n"; cout<<"\n"; cout<<"\t\tTEXT EDITOR\n"; cout<<"##############################################\n"; cout<<"\n Type your text and then press ESC key\n"; gotoxy(100,500); cprintf("%2d%2d",cury,curx); gotoxy(curx,cury); } void main() { char ch,c; ofstream outfile; ifstream infile; clrscr(); cur_pos(); curx=wherex(); cury=wherey(); while(c!=27) { c=getch(); switch(c) { case 80: //down arrow gotoxy(curx,cury+1); break; case 77: //right side gotoxy(curx+1,cury); break; case 72: //up arrow gotoxy(curx,cury-1); break; case 75: //left side gotoxy(curx-1,cury); break; case 32: //space printf(" "); buff[count++]=' ';
break; case 13: //new line gotoxy(1,cury+1); buff[count++]='\n'; break; default: textcolor(13); if((c>=65 && c<=122) (c>48 && c<57)) cprintf("%c",c); break; } buff[count++]=c; cur_pos(); } cprintf("\n\nDo you want to save? (y/n)"); scanf("%c",&c); if((c=='y')(c=='Y')) { cprintf("\n\nEnter the file name with extension in 8 characters only"); scanf("%s",filename); outfile.open(filename,ios::out); outfile<<buff; outfile.close(); cout<<"\nDo you want to open? (y/n) \n"; ch=getch(); if((ch=='y')(ch=='Y')) { cprintf("\n\nEnter the file name to open"); scanf("%s",filename); infile.open(filename,ios::in); infile.get(buff,count,'*'); gotoxy(90,1); printf("%s",buff); getch(); infile.close(); } } }
OPEN FILE
EDIT FILE:
11. Symbol Table Using Hashing Aim To Implement a Symbol Table using Hashing Function.
/* IMPLEMENTATION OF SYMBOL TABLE USING HASHING */
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #define MAX 10 char l[10]; struct symb { int add; char label[10];
}sy[11]; void search(); void main() { int a[MAX],num,key,i,ch; char ans; int create(int); void lprob(int[],int,int); void display(int[]); clrscr(); for(i=0;i<MAX;i++) sy[i].add=-1; do { printf("\nEnter your choice:1.create a symbol table\n2.exit"); scanf("%d",&ch); switch(ch) { case 1: do { printf("\nEnter the address :"); scanf("%d",&num); key=create(num); printf("Enter the label:"); scanf("%s",l); lprob(a,key,num); printf("\ncontinue(y/n)?"); ans=getche(); } while(ans=='y'); display(a); break; case 2: exit(0); }}while(ch<=2); getch(); } int create(int num) { int key; key=num%10; return key; } void lprob(int a[MAX],int key,int num) { int flag,i,count=0; void display(int a[]); flag=0; if(sy[key].add==-1) { a[key]=num; sy[key].add=num; strcpy(sy[key].label,l); } else
{ i=0; while(i<MAX) { if(sy[i].add!=-1) count++; i++; } if(count==MAX) { printf("\nHash table is full"); display(a); getch(); exit(1); } for(i=key+1;i<MAX;i++) if(sy[i].add==-1) { a[i]=num; flag=1; sy[i].add=num; strcpy(sy[i].label,l); break; } for(i=0;i<key&&flag==0;i++) if(sy[i].add==0) { a[i]=num; flag=1; sy[key].add=num; strcpy(sy[key].label,l); break; }}} void display(int a[MAX]) { FILE *fp; int i; fp=fopen("symbol.txt","w"); printf("\nThe symbol Table is"); printf("\nHashvalues\taddress label"); for(i=0;i<MAX;i++) { printf("\n%d\t%d\t%s",i,sy[i].add,sy[i].label); fprintf(fp,"\n%d%d%s",i,sy[i].add,sy[i].label); } fclose(fp); }
Enter the address :0 Enter the label:a continue(y/n)?y Enter the address :4 Enter the label:f continue(y/n)?y Enter the address :5 Enter the label:g continue(y/n)?y Enter the address :5 Enter the label:k continue(y/n)?n The symbol Table is Hashvalues address 0 0 1 -1 2 -1 3 -1 4 4 5 5 6 5 7 -1 8 -1 9 -1 Enter your choice 1.create a symbol table 2.exit 2
label a
f g k