Anda di halaman 1dari 35

SYMBOL TABLE OPERATION AIM To write a c program using the data types and the symbols for creating

symbol table operation. ALGORITHM STEP 1: Start the program STEP 2: Declare the function and include the header files STEP 3: In main function, condition for the operation are used STEP 4: In insert function, top and other data types are used to insert the symbols. STEP 5: In delete function, any position can be deleted STEP 6: In display function, the contents of the program are displayed STEP 7: Modification and search operations can be done STEP 8: Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> struct a { char t[20]; char s[20]; }; int top=0; void main() { int code; struct a arr[3]; void insert(struct a arr[3]); void display(struct a arr[3]); void del(struct a arr[3]); void search(struct a arr[3]); void modify(struct a arr[3]); clrscr(); while(1) { printf("\n Enter code\n1.Insert\n2.Delete\n3.Display\n4.search\n5.Modify\n6.Exit\n"); scanf("%d",&code); if(code==1) insert(arr); else if(code==2) del(arr); else if(code==3) display(arr); else if(code==4) search(arr); else if(code==5) modify(arr); else if(code==6) exit(0);

} } void insert(struct a arr[3]) { char type[20],symbol[20]; int i; printf("\n Enter the type and symbol"); scanf("%s %s",type,symbol); for(i=0;i<top;++i) { if((strcmp(arr[i].t,type)==0)&&(strcmp(arr[i].s,symbol)==0)) { printf("\n symbol already exists"); return; } } strcpy(arr[top].t,type); strcpy(arr[top].s,symbol); ++top; } void display(struct a arr[3]) { int i; if(top>0) { printf("\n SYMBOL TABLE"); printf("\nSYMBOL\t\tTYPE"); for(i=0;i<top;++i) printf("%s \t\t%s\n",arr[i].t,arr[i].s); return; } else printf("\n Empty Table"); return; } void del(struct a arr[3]) { int pos; { if(top<=0) { printf("\n Table is empty"); return; } else if(top>0) printf("\nWhich position do u want to delete"); scanf("%d",&pos); strcpy(arr[pos].t,0); strcpy(arr[pos].s,0); } return; } void search(struct a arr[3]) { int i;

if(top<=0) { printf("\n Table is empty"); return; } else { char type1[10],symbol1[10]; printf("\n Enter type and symbol"); scanf("%s %s",type1,symbol1); for(i=0;i<top;i++) { if((strcmp(arr[i].t,type1)==0)&&(strcmp(arr[i].s,symbol1)==0)) { printf("\n symbol exists and the position is %d",i); return; } } printf("\n symbol does not exists"); } return; } void modify(struct a arr[3]) { if(top<=0) { printf("\n Table is empty"); return; } else { int pos; char type2[10],symbol2[10]; printf("\n which position do u want to change"); scanf("%d",&pos); printf("\n Enter the symbol and type to modify"); scanf("%s %s",type2,symbol2); strcpy(arr[pos].t,type2); strcpy(arr[pos].s,symbol2); } return; } OUTPUT Enter code 1. insert 2. delete 3. display 4. search 5. modify 6. exit 1 Enter the type and symbol: int a

Enter code 1. insert 2. delete 3. display 4. search 5. modify 6. exit 1 Enter the type and symbol: double b Enter code 1. insert 2. delete 3. display 4. search 5. modify 6. exit 3 SYMBOL TYPE int a double b Enter code 4 Enter the type and symbol: float b Symbol exists and the position is 1 Enter code 5 Which position do you want to change 0 Enter the symbol and type to modify int c Enter code 3 SYMBOL TABLE int c float b Enter code 2 Enter the position to be deleted: 0 Enter code 3 RESULT Thus a program for symbol table operation is successfully written and the output is verified.

PASS 1 IN TWO PASS ASSEMBLER AIM To write a c program for generating the pass 1 in two pass assembler ALGORITHM STEP 1: Start the program STEP 2: Declare the function and include the header files STEP 3: Declare the variables with their data types STEP 4: In main function, assign the files that are to be read and also assign the files that are to be written. STEP 5: To read the contents of another file, fscanf and to print the contents of another file, fprintf are used. STEP 6: The files contain label, mnemonics and opcode which are to be copied from one file to another. STEP 7: The intermediate file that are belonging to the system are displayed STEP 8: An string function atoi is used to compare the contents of the string STEP 9: Then, the source, symbol, intermediate files are displayed STEP 10: Stop the program PROGRAM #include<stdio.h> #include<conio.h> #include<stdlib.h> struct source { char label[10]; char mnem[10]; char op[10]; }s; struct opcode { int length; char mnem[10]; char op[]; }o; struct intermediate { int addr ; char label[10]; char mnem[10]; char op[10]; }in; struct symbol { int addr; char label[10]; }sy; void main() { int i,n,addr,leng; FILE *a, *b, *c, *d;

clrscr(); a=fopen("source.txt","r"); rewind(a); b=fopen("opcode.txt","r"); rewind(b); c=fopen("inter.txt","w"); rewind(c); d=fopen("symbol.txt","r"); rewind(d); printf("\n Enter the value of n"); scanf("%d",&n); in.addr=0; for(i=0;i<n;i++) { fscanf(a,"\t%s\t%s\t%s", s.label, s.mnem, s.op); printf("\n source is \n"); printf("\t%s\t%s\t%s", s.label, s.mnem, s.op); if(strcmp(s.mnem, "END")!=0) { sy.addr=in.addr; printf("\n %d", sy.addr); strcpy(in.label, s.label); strcpy(sy.label, in.label); strcpy(in.mnem, s.mnem); strcpy(in.op, s.op); printf("\n \t intermediate file is"); fprintf(c,"\n%d\t%s\t%s\t%s\n", in.addr, in.label, in.mnem, in.op); printf("%d\t%s\t%s\t%s\n", in.addr, in.label, in.mnem, in.op); fscanf(b,"\n%d\t%s\t%s\n", &o.length, &o.mnem, &o.op); printf("\n opcode \n"); printf("\n%ds\t%s\t%s", o.length, o.mnem, o.op); printf("\n symbol is \n"); printf("\t%d\t%s", sy.addr, sy.label); fprintf(d,"\n%d\t%s\n", sy.addr, sy.label); addr=atoi(s.op); if(strcmp(s.mnem, o.mnem)==0) addr=o.length; in.addr=in.addr+addr; } } fcloseall(); printf("\n pass 1 is completed"); getch(); } INPUT FILES source.txt COPY START 500 15 ADD 2A A BYTE 10 NULL END COPY opcode.txt 2 ADD 3F 1 INC 4F 1 END 04

OUTPUT Enter the value of n: 4 Source is 0 COPY START 500

intermdiate file is 0 COPY START 500

opcode is 2 ADD 2F

symbol is 15 ADD 2A 500

intermediate file is 500 15 ADD 2A

opcode is 1 INC 4F

symbol is 500 15

source is A 502 intermediate file is 502 A BYTE 10 BYTE 10

opcode is 1 END 04

symbol is 502 A

source is NULL END COPY

pass 1 is completed RESULT Thus the c program for generating the pass 1 in two pass assembler has been executed successfully and the output is verified.

SINGLE PASS ASSEMBLER AIM To write a c program for generating the single pass assembler. ALGORITHM STEP 1: Start the program. STEP 2: Declare the variables with their data-types. STEP 3: Declare the functions and include the header files. STEP 4: In main, assign the files to be read and also assign the files to be written. STEP 5: To read the contents from another file, fscanf is used and to print contents to another file, fprintf is used. STEP 6: The files contain label, mnemonics and opcode which are copied from one file to another. STEP 7: The no. of instructions that are to be typed are entered. STEP 8: It is used to print the label and mnemonic code of single pass assembler. STEP 9: Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<stdlib.h> struct source { char label[10]; char mnem[10]; char op[10]; }s; struct inter { int addr; char label[10]; char mnem[10]; char op[10]; }in; struct symbol { int addr; char label[10]; }sy; void main() { FILE *a,*b,*c; int i,n; clrscr(); a=fopen("source3.txt","r"); rewind(a); b=fopen("inter3.txt","r"); rewind(b); c=fopen("symbol3.txt","w"); rewind(c); fscanf(b,"\n%d\t%s\t%s\t%s\n",&in.addr,in.label,in.mnem,in.op); fscanf(a,"\n%s\t%s\t%s\n",s.label,s.mnem,s.op); fseek(a,01,0); printf("\nEnter the number of instructions"); scanf("%d",&n); for(i=0;i<n;i++)

{ fscanf(b,"\n%d\t%s\t%s\t%s\n",&in.addr,in.label,in.mnem,in.op); fscanf(a,"\n%s\t%s\t%s\n",s.label,s.mnem,s.op); if(strcmp(in.label,s.op)==0) { strcpy(sy.label,in.label); sy.addr=in.addr; printf("\n%d\t%s\n",sy.addr,sy.label); fprintf(c,"\n%d\t%s\n",sy.addr,sy.label); } else { printf("*\t"); fprintf(c,"*\t"); strcpy(sy.label,s.op); printf("%s\t\n",sy.label); fprintf(c,"%s\t\n",sy.label); } } rewind(a); rewind(b); fcloseall(); getch(); } INPUT FILES source3.txt FIRST START CLOOP F1 ADD A A INC B inter3.txt 0 FIRST START LOOP 500 F1 ADD A 502 A INC B OUTPUT Enter the no.of instructions: 3 * CLOOP 502 A * B

RESULT Thus the single value is passed through assembler, the program was executed successfully and thus the output was verified.

TEXT EDITOR AIM To write a program in C for text editor using ASCII codes. ALGORITHM STEP 1: Declare the header files. STEP 2: In the main, declare the required variables. STEP 3: The text file is opened from bin using r+ to read, write and append. STEP 4: The key function of certain required keys are given in switch case using their ASCII codes. STEP 5: The gotoxy() command is used to get the cursor to the selected line. STEP 6: The changes are saved to the text file by using write in fopen. STEP 7: The changes are saved and the program is stopped. PROGRAM #include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> #include<string.h> void main() { FILE *fp; int i=0,n=0,line=0,x=1,y=1,len=0,j=0; char s[100][100],temp[100],ch,choice='y'; clrscr(); if((fp=fopen("in1.txt","r+"))==NULL) { printf("\n File cannot be opened"); exit(1); } while(!feof(fp)) { fgets(s[i],79,fp); n=++i; } clrscr(); flushall(); printf("in1.txt"); for(i=0;i<n;i++) puts(s[i]); while(choice=='y') { printf("\n Enter the number to be changed: "); scanf("%d",&line); strcpy(temp,s[line-1]); clrscr(); puts(temp); len=strlen(temp); gotoxy(x,y); ch=getch(); while(ch!=13) { switch(ch) { case 77: x++; gotoxy(x,y);

break; case 75: x--; gotoxy(x,y); break; case 83: for(i=x-1;j<len;i++,j++) temp[j]=' '; temp[i]=temp[j]; clrscr(); puts(temp); gotoxy(x,y); break; case 27: exit(1); default: if(isalnum(ch)) { for(i=len+1,j=len;j>x;i--,j--) temp[i]=temp[j]; clrscr(); len++; temp[x]=ch; puts(temp); x++; gotoxy(x,y); } } ch=getch(); } clrscr(); printf("\n Do u want to save change(y/n):"); ch=getch(); if(ch=='Y'||ch=='y') strcpy(s[line-1],temp); for(i=0;i<n;i++) puts(s[i]); fclose(fp); fp=fopen("in1.txt","w"); for(i=0;i<n;i++) fputs(s[i],fp); getch(); printf("\n Do u want to continue the open press(y/n): "); scanf("%s",&choice); } getch(); } INPUT FILE in1.txt Hai Hello This is System Software lab OUTPUT in1.txt Hai Hello

This is System Software lab Enter the number to be changed: 2 Welcome Do you want to save changes(y/n): y Hai Welcome This is System Software lab Do you want to continue the open press(y/n): n

RESULT Thus a program is written successfully for text editor using ASCII codes in C and the output is verified.

ABSOLUTE LOADER AIM To write a C program to show the functionality of absolute loader. ALGORITHM STEP 1: Start the program. STEP 2: Declare the variables, function and header files. STEP 3: Open file and copy the details to the temp file. STEP 4: printf and scanf the files by specifying the files. STEP 5: Compare the strings of mnem and compare the mnemonics of o and in file. STEP 6: Print the location and address of memory and compare opcode of inter file and label of symbol file. STEP 7: Print the address and save the output in the memory file. STEP 8: Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<stdlib.h> struct optab { int length; char mnem[10]; int opcode; }o; struct symtab { char label[10]; int addr; }sy; struct inter { int addr; char label[10]; char mnem[10]; char op[10]; }in; struct Memory { int loc; int opcode; int addr; }m; void main() { int i; FILE *o1,*sy1,*in1,*m1; clrscr(); o1=fopen("opcode4.txt","r"); rewind(o1); sy1=fopen("symbol4.txt","r"); rewind(sy1); in1=fopen("inter4.txt","r"); rewind(in1); m1=fopen("memory4.txt","w");

rewind(m1); for(i=0;i<3;i++) { fscanf(in1,"%d %s %s %s",&in.addr,in.label,in.mnem,in.op); fscanf(sy1,"%s %d",sy.label,&sy.addr); fscanf(o1,"\n%d %s %d",&o.length,o.mnem,&o.opcode); if(strcmp(in.mnem,"END")!=0) { if(strcmp(in.mnem,o.mnem)==0) m.loc=in.addr; fprintf(m1,"%d\t",m.loc); printf("%d\t",m.loc); m.opcode=o.opcode; fprintf(m1,"%d",m.opcode); printf("%d\t",m.opcode); } if(strcmp(in.op,sy.label)==0) { m.addr=sy.addr; fprintf(m1,"%d\t",m.addr); printf("%d\t",m.addr); printf("\n"); fprintf(m1,"\n"); } } rewind(in1); rewind(o1); rewind(sy1); getch(); } INPUT FILES inter4.txt 0000 COPY START C 1001 F1 ADD A 1045 A INC B 1200 NULL END F1 opcode4.txt 0 START 56 2 ADD 14 4 INC 45 symbol4.txt C 3000 A 1044 B 1200 OUTPUT 0 56 3000 1001 14 1044 1045 45 1200 RESULT Thus the program to perform absolute loader operation was successfully executed and the output was verified.

RELOCATING LOADER AIM To write a program in c to display the functionality of relocating loader. ALGORITHM STEP1: Start the program and declare the header files. STEP2: Define the structure of inter with elements addr, label, mnem, op and bit. STEP3: Define he structure of reloc with integers addr and bit,and arrays label, mnem and op. STEP4: In main,derine the two text files for read and write operations. STEP5: The starting address is got from the user and the label, mnemonics and opcode are got from the input file inter6.txt. STEP6: The data is written to reloc6.txt and output is displayed. STEP7: Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> struct inter { int addr; char label[10]; char mnem[10]; char op[10]; int bit; }in; struct reloc { cnt addr; char label[10]; char mnem[10]; char op[10]; int bit; }re; void main() { int I,n; FILE *a,*b; clrscr(); a=fopen(inter6.txt,r); rewind(a); b=fopen(reloc6.txt,r); rewind(b); printf(Enter the starting address); scanf(%d,&n); for(i=0;i<4;i++) { fscanf(a,%d\t%s\t%s\t%s\t%d\t\n,&in.addr,in.label,in.mnem,in.op,&in.bit); if(in.bit==1) { in.addr=in.addr+n; fprintf(b,%d\t%s\t%s\t%d\t\n,&in.addr,in.mnem,in.op,&in.bit); printf(%d\t%s\t%s\t%s\t%d\t\n,&in.addr,in.label,in.mnem,in.op,&in.bit); } } rewind(a); rewind(b);

fcloseall(); getch(); } INPUT FILES inter6.txt 0 CLOOP START 500 500 F1 ADD 502 F2 INC 504 NULL END OUTPUT Enter the starting address: 3000 3500 F1 ADD A 3502 F2 INC B 3504 NULL END CLOOP

0 A B CLOOP 1 1 1 1 1 1

RESULT Thus the program to display functionality of relocating loader is completed successfully and output was verified.

TWO PASS MACROPROCESSOR AIM To implement a two pass macro-processor in C language. ALGORITHM STEP 1: Start the program. STEP 2: Create a file named MACRO.txt. STEP 3: Get the macro name and get the macro instructions. STEP 4: Solve the macro and its definition in its file and close the file. STEP 5: Enter the instructions of the macro. STEP 6: The macro will automatically substitute the arguments in the MARO.txt file. STEP 7: Close the file and stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> struct Macro { char str[50]; }mm[10]; void main() { FILE *fp; char ch,mname[50],arg1[50],temp[50],arg2[50]; int l,i,flag=0,j,k,pos,z=0,cnt=0,y; clrscr(); fp=fopen("macro.txt","w"); printf("\n Enter the macro definition \n"); do { printf("\n Enter the instruction\n"); gets(mm[z].str); fwrite(&mm[z],sizeof(mm[z]),1,fp); fflush(stdin); printf("\n Continue...[y/n]"); ch=getch(); if(ch=='y'||ch=='Y') z++; }while(ch=='y'||ch=='Y'); fclose(fp); fflush(stdin); z=0; printf("\n Enter the macro name \n"); gets(mname); fflush(stdin); fp=fopen("macro.txt","r+"); fread(&mm[z],sizeof(mm[z]),1,fp); fflush(stdin); if(strcmp(mm[z].str,"MACRO")==0) { while(!feof(fp)) { z++; if(flag==0) { fread(&mm[z],sizeof(mm[z]),1,fp);

l=strlen(mm[z].str); printf("\n String 1= \n"); puts(mm[z].str); for(i=0;i<l;i++) { if(mm[z].str[i]!=' ') temp[i]=mm[z].str[i]; else break; } temp[i]='\0'; if(strcmp(temp,mname)==0) { printf("\n Enter the two actual arguments"); gets(arg1); gets(arg2); strcat(temp," "); strcat(temp,arg1); strcat(temp,","); strcat(temp,arg2); strcpy(mm[z].str,temp); pos=ftell(fp); fseek(fp,pos-sizeof(mm[z]),SEEK_SET); fwrite(&mm[z],sizeof(mm[z]),1,fp); fseek(fp,pos,SEEK_SET); flag++; } else printf("\n Macro not found"); } else { fread(&mm[z],sizeof(mm[z]),1,fp); printf("\n String 2=\n"); puts(mm[z].str); if(strcmp(mm[z].str,"MEND")!=0) { l=strlen(mm[z].str); for(i=0;i<l;i++) { if(mm[z].str[i]==' ') temp[i]=mm[z].str[i]; else break; } temp[i]='\0'; strcpy(mm[z].str,temp); if(cnt%2==0) strcat(mm[z].str,arg1); else strcat(mm[z].str,arg2); cnt++; pos=ftell(fp); printf("pos\t%d",pos); fseek(fp,pos-sizeof(mm[z]),SEEK_SET);

fwrite(&mm[z],sizeof(mm[z]),1,fp); puts(mm[z].str); fseek(fp,pos,SEEK_SET); } else break; } } fclose(fp); } getch(); } INPUT Enter the macro definition Enter the instruction MACRO Continue[y/n] y Enter the instruction ADD A,B Continue[y/n] y Enter the instruction LDA A Continue[y/n] y Enter the instruction ADD B Continue[y/n] y Enter the instruction MEND Continue[y/n] n Enter the macro name SUM String 1= ADD A,B Enter the two actual arguments 25 45 String 2= LDA A pos 15025 String 2= ADD B pos 20045 String 2= MEND OUTPUT MACRO.txt MACRO SUM 25,45 A 25 B 45 MEND RESULT Thus the program to implement a two pass macro-processor in C program was successfully executed and the output was verified.

IMPLEMENTATION OF PASS 1 OF A DIRECT LINKING LOADER AIM To write a program using C to implement pass 1 of a direct linking loader. ALGORITHM STEP 1: Enter the location where program has to be loaded. STEP 2: Assign the address got from the user as the first control section address. STEP 3: Read the header record of the control section. a. From the details of the header read store the control section length in a variable. b. Enter the control section name with its address into the external symbol table. STEP 4: For each symbol in the subsequent D records, the symbol must be entered into the table along with its address, added along with the corresponding control section until an end record is reached. STEP 5: Assign the starting address of next control section as the address of the current control section plus the length of the control section. STEP 6: Repeat the process from step 3 to 5 until there are no more records. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct estab { char csect[10]; char sym_name[10]; int add,length; }table[10]; void main() { char input[10]; int i,count=0,start,length,loc; FILE *fp1,*fp2; clrscr(); fp1=fopen("LINKIN.txt","r"); fp2=fopen("LINKOUT.txt","w"); printf("\n Enter the location where the program had to be loaded"); scanf("%x",&start); fprintf(fp2,"csect\tsym_name\taddress\t\tlength\n"); printf("csect\tsym_name\taddredd\t\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,"**"); fscanf(fp1,"%s",input); table[count].add=atoi(input)+start; fscanf(fp1,"%x",&length); table[count++].length=length; fscanf(fp1,"%s",input); }

if(strcmp(input,"D")==0) { fscanf(fp1,"%s%x",input,&loc); while(strcmp(input,"R")!=0) { strcpy(table[count].csect,"**"); strcpy(table[count].sym_name,input); table[count].add=loc+start; table[count++].length=0; fscanf(fp1,"%s%x",input,&loc); } while(strcmp(input,"T")!=0) fscanf(fp1,"%s",input); } if(strcmp(input,"T")==0) while(strcmp(input,"E")!=0) fscanf(fp1,"%s",input); fscanf(fp1,"%s",input); start=start+length; } for(i=0;i<count;i++) printf("%s\t%s\t\t%x\t\t%x\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length); fprintf(fp2,"%s\t%s\t\t%x\t\t%x\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length); fclose(fp1); fclose(fp2); getch(); } INPUT FILES LINKIN.txt H PROGA 000000 000070 D LISTA 000040 ENDA 000054 R LISTB ENDB LISTC ENDC T 000020 10 032010 77100004 15001 T 000054 16 100014 15100006 00002F 10014 M 000024 05 +LISTB M 000054 06 +LISTC M 000058 06 +ENDC M 000064 06 +ENDC E 000000 H PROGB 000000 000088 D LISTB 000060 ENDB 000070 R LISTA ENDA LISTC ENDC T 000036 11 0310000 772027 0510030 T 000070 18 100000 05100006 0510020 0510030 M 000037 05 +LISTA M 000044 05 +ENDA M 000070 06 +ENDA M 000074 06 +ENDC M 000078 06 +ENDC M 000082 06 +ENDA E 000000 H PROGA 000000 000057 D LISTC 000030 ENDC 000042 R LISTA ENDA LISTB ENDB T 000018 12 03100000 77100004 05100000

T 000042 15 100030 100008 100011 100000 M 000019 05 +LISTA M 000023 05 +LISTB M 000027 05 +ENDA M 000048 06 +LISTA M 000051 06 +ENDA M 000054 06 +LISTB E 000000 OUTPUT Enter the location where the program has to be loaded: 2000 Csect sym_name address length PROGA ** 2000 70 ** LISTA 2040 0 ** ENDA 2054 0 PROGB ** 2070 88 ** LISTB 20d0 0 ** ENDB 20e0 0 PROGC ** 20f8 57 ** LISTC 2128 0 ** ENDC 213a 0

RESULT Thus the program using C to implement pass 1 of a direct linking loader has been written and the output is verified.

PASS TWO IN TWO PASS ASSEMBLER AIM To write a c program to perform pass 2 in two pass assembler. ALGORITHM STEP 1: Start the program and declare header files. STEP 2: Define the structure of source with label, mnem, op, structure of opcode with mnem, op and integer value length, inter with label, mnem, op and integer value addr, symbol with label and addr integer value and output with op and integer values addr and open. STEP 3: In main function, initialize five file pointers, four for input files and one for output file. STEP 4: Define the accessing methods of all five files. STEP 5: Read the values from the input files. STEP 6: With a for loop compare the values between the files and write the output to the output file. STEP 7: Print the results and stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<stdlib.h> struct source { char label[10]; char mnem[10]; char op[10]; }s; struct opcode { int length; char mnem[10]; char op[10]; }o; struct intermediate { int addr; char label[10]; char mnem[10]; char op[10]; }in; struct symbol { int addr; char label[10]; }sy; struct output { int addr; char op[10]; int oper; }out; void main() { int i,n; FILE *a,*b,*c,*d,*e; clrscr(); a=fopen(source.txt,r);

rewind(a); b=fopen(inter.txt,r); rewind(b); c=fopen(symbol.txt,r); rewind(c); d=fopen(opcode.txt,r); rewind(d); e=fopen(output.txt,w); rewind(e); fscanf(b,\n%d\t%s\t%s\t%s\n,&in.addr,&in.label,&in.mnem,&in.op); fscanf(d,\n%d\t%s\t%s\n,&o.length,&o.mnem,&o.op); fscanf(a,\n%s\t%s\t%s\n,&s.label,&s.mnem,&s.op); printf(\n enter the number of instructions:); scanf(%d,&n); fseek(d,0,0); fseek(c,0,0); printf(\n address\t opcode\toperand\n); for(i=1;i<n;i++) { fscanf(b,\n%d\t%s\t%s\t%s\n,&in.addr,&in.label,&in.mnem,&in.op); fscanf(d,\n%d\t%s\t%s\n,&o.length,&o.mnem,&o.op); fscanf(c,\n%d\t%s\n,&sy.addr,&sy.label); fscanf(a,\n%s\t%s\t%s\n,&s.label,&s.mnem,&s.op); if(strcmp(in.mnem,o.mnem)==0) { out.addr=in.addr; strcpy(out.op,o.op); if(strcmp(s.op,sy.label)==0) { fprintf(e,\n%d\t%s\t%d\n,out.addr,out.op,out.oper); fprintf(\n%d\t%s\t%d\n,out.addr,out.op,out.oper); } } rewind(b); rewind(d); rewind(c); fcloseall(); getch(); } INPUT FILES source.txt 0 START 500 FIRST ADD A A BYTE 1 B BYTE 1 NULL END FIRST inter.txt 0 CLOOP START 500 500 FIRST ADD A 502 F1 INC B 504 A BYTE 1 505 B BYTE 1 506 NULL END CLOOP

symbol.txt 504 505 0 A B FIRST OPCODE.TXT 2 ADD 3E 2 INC 4F 1 END 04 OUTPUT Enter the number of instructions:6 address opcode operand 500 3E 504 502 4F 504 506 04 0

RESULT Thus the c program for pass 2 in two pass assembler was executed successfully and the output was verified.

IMPLEMENTATION OF PASS 2 OF A DIRECT LINKING LOADER AIM To write a program using C to implement pass 2 of a direct linking loader. ALGORITHM STEP 1: Enter the location where program has to be loaded. STEP 2: Assign the address got from the user as the first control section address. STEP 3: Read the header record of the control section. a. From the details of the header read store the control section length in a variable. b. Enter the control section name with its address into the external symbol table. STEP 4: For each symbol in the subsequent D records, the symbol must be entered into the table along with its address, added along with the corresponding control section until an end record is reached. STEP 5: Assign the starting address of next control section as the address of the current control section plus the length of the control section. STEP 6: Repeat the process from step 3 to 5 until there are no more records. PROGRAM #include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7,*fp8; char rec[20],name[20],len[20],t[20],string[1000],mod[40][40],est1[20],est2[20],est4[20],sign[40] [1],temp; int ptn1[20][20],ptn2[20][20]; int l,h=0,lent[20],i,st,m1,m2,start[20],add[20],k=0,est3,z1=0,val[40],ptn[40],offset[40],j,num,progstart; fp1=fopen("DLL_IN.txt","r"); fp2=fopen("ESTAB.txt","r"); fp3=fopen("ntemp.txt","w"); fp4=fopen("memory.txt","w"); fp6=fopen("six.txt","w"); fscanf(fp1,"%s%s%x%s",rec,name,&st,len); for(l=0;l<3;l++) { if(l==1) fp3=fopen("ntempb.txt","w"); if(l==2) fp3=fopen("ntempc.txt","w"); fscanf(fp1,"%s",t); do{ if(strcmp(t,"T")==0) { fscanf(fp1,"%x%x",&start[h],&lent[h]); if(h!=0) { for(i=0;i<(start[h]-(start[h-1]+lent[h-1]));i++) fprintf(fp3,"x"); } h++; fscanf(fp1,"%s",t); do{ fprintf(fp3,"%s",t); fscanf(fp1,"%s",t);

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

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

} getch(); } INPUT FILES DLL_IN.txt H PROGA 000000 00003A D LISTA 000030 ENDA 000050 . R LISTB LISTC ENDC T 000000 1D 172027 4B100000 032023 290000 332007 4B100000 3F2FEC 032016 0F2016 T 00001D 0D 010003 0F200A 4B100000 3E2000 M 000004 05 + LISTB M 000011 05 + LISTC E 000000 H PROGB 000000 00002E D LISTB 000060 ENDB 000070 . R LISTA ENDA T 000000 1D B410 B400 B440 77201F E3201B 332FFA DB2015 A004 332009 57900000 B850 T 000020 0E 3B2FE9 13100000 4F0000 F1000000 M 000007 05 + LISTA M 000022 05 + ENDA E 000000 H PROGC 000000 00001C D LISTC 000030 ENDC 000042 . R LISTA ENDA T 000000 1C B410 77100000 E32012 332FFA 53900000 DF2008 B850 3B2FEE 4F000005 M 000006 05 + LISTA M 000013 06 + ENDA E 000000 ESTAB.txt PROGA - 003000 000063 - LISTA 003030 - ENDA 003050 PROGB - 003063 00007f - LISTB 0030c3 - ENDB 0030d3 PROGC - 0030e2 000051 - LISTC 003112 - ENDC 003124 OUTPUT MEMORY.txt 3000 1720274B 1030c303 20232900 00332007 3010 4B103112 3F2FEC03 20160F20 16010003 3020 0F200A4B 1000003E 2000B410 B400B440 3030 77205013 201B332F FADB2015 A0043320 3040 09579000 00B850xx x3B2FE91 30305004 3050 F0000F10 00000B41 07710000 0E050423 3060 32FFA539 00000DF2 008B0034 02FEE4F0 3070 00005 RESULT Thus pass two of direct-linking loader is implemented in C language and the output is verified.

SINGLE PASS MACROPROCESSOR AIM To implement a single pass macro-processor in C language. ALGORITHM STEP 1: Get the statement from the i/p file. STEP 2: If the statement has the directive MACRO then the number of macro n will be increased by1. STEP 3: Repeat the step 1 and step 2 until an end of file is encountered. STEP 4: Open n number of macro files in write mode and rewind the input file pointer. STEP 5: If the directive is MACRO then do the following Write the body of each macro of function Write the remaining line directly to the expanded file. STEP 6: Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { int n,flag,i; char ilab[20],iopd[20],oper[20],NAMTAB[20][20]; FILE *fp1,*fp2,*DEFTAB; clrscr(); fp1=fopen("MACROIN.txt","r"); fp2=fopen("MACROOUT.txt","w"); n=0; rewind(fp1); fscanf(fp1,"%s%s%s",ilab,iopd,oper); while(!feof(fp1)) { if(strcmp(iopd,"MACRO")==0) { strcpy(NAMTAB[n],ilab); DEFTAB=fopen(NAMTAB[n],"w"); fscanf(fp1,"%s%s%s",ilab,iopd,oper); while(strcmp(iopd,"MEND")!=0) { fprintf(DEFTAB,"%s\t%s\t%s\n",ilab,iopd,oper); fscanf(fp1,"%s%s%s",ilab,iopd,oper); } fclose(DEFTAB); n++; } else { flag=0; for(i=0;i<n;i++) { if(strcmp(iopd,NAMTAB[i])==0) { flag=1; DEFTAB=fopen(NAMTAB[i],"r"); fscanf(DEFTAB,"%s%s%s\n",ilab,iopd,oper);

while(!feof(DEFTAB)) { fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper); fscanf(DEFTAB,"%s%s%s",ilab,iopd,oper); } break; } } if(flag==0) fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper); } fscanf(fp1,"%s%s%s",ilab,iopd,oper); } fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper); getch(); } INPUT FILES MACROIN.txt M1 MACRO ** ** LDA N1 ** ADD N2 ** STA N3 ** MEND ** M2 MACRO ** ** LDA N1 ** SUB N2 ** STA N4 ** MEND ** M3 MACRO ** ** LDA N1 ** MUL N2 ** STA N5 ** MEND ** ** START 1000 ** M3 ** ** M2 ** ** M1 ** ** END ** OUTPUT MACROOUT.txt ** START 1000 ** LDA N1 ** MUL N2 ** STA N5 ** LDA N1 ** SUB N2 ** STA N4 ** LDA N1 ** ADD N2 ** STA N3 ** END ** RESULT Thus the program to implement a single pass macro-processor was successfully executed and the output was verified.

IMPLEMENTATION OF SYMBOL TABLE WITH HASHING AIM To write a program to implement a symbol table with hashing in C language. ALGORITHM STEP 1: Start the program and include the header files. STEP 2: Define the structure for symbol table with character label and integer address. STEP 3: Declare the functions for creating displaying and setting the values in the table. STEP 4: Using switch case statement, add a menu for choosing between creating table and searching values in table.

STEP 5: For creating table, take input from user for address and label. Then applying the hash value, store the label in the table at the corresponding location.
STEP 6: Display the table and print them to the file symbol.txt STEP 7: For search, get the label to be found from user. Using if condition, show label address if found, otherwise display label not present in table. STEP 8: Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #define MAX 11 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++) a[i]=0; do { printf("\n Enter your choice:\n1.Create a symbol table\n2.Search in symbol table\n"); scanf("%d",&ch); switch(ch) { case 1: do { printf("\n Enter the address: "); scanf("%d",&num); key=create(num); printf("\n Enter the label: "); scanf("%s",l); lprob(a,key,num);

printf("\n Continue(y/n)? "); ans=getch(); } while(ans=='y'); display(a); break; case 2: search(); break; } } while(ch<=2); getch(); } int create(int num) { int key; key=num%11; return key; } void lprob(int a[MAX],int key,int num) { int flag,i,count=0; void display(int a[]); flag=0; if(a[key]==0) { a[key]=num; sy[key].add=num; strcpy(sy[key].label,l); } else { i=0; while(i<MAX) { if(a[i]!=0) count++; i++; } if(count==MAX) { printf("\n Hash table is full"); display(a); getch(); exit(1); } for(i=key+1;i<MAX;i++) if(a[i]==0) { a[i]=num; flag=1; sy[key].add=num; strcpy(sy[key].label,l); break;

} for(i=0;i<key&&flag==0;i++) if(a[i]==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("\n The symbol table is: "); printf("\n Hashvalue Address 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); } void search() { FILE *fp1; char la[10]; int set=0,s; int j,i; printf("\n Enter the label: "); scanf("%s",la); fp1=fopen("symbol.txt","r"); for(i=0;i<MAX;i++) { fscanf(fp1,"%d%d",&j,&sy[i].add); if(sy[i].add!=0) fscanf(fp1,"%s",sy[i].label); } for(i=0;i<MAX;i++) { if(sy[i].add!=0) { if(strcmp(sy[i].label,la)==0) { set=1; s=sy[i].add; } } } if(set==1) printf("\n The label ...%s... is present in the symbol tablew address: %d\n",la,s); else

printf("\n The label is not present in the symbol table\n"); } INPUT Enter your choice: 1.Create a symbol table 2.Search in symbol table 1 Enter the address: 1001 Enter the label: add Continue(y/n)? y Enter the address: 1005 Enter the label: sub Continue(y/n)? y Enter the address: 1007 Enter the label: mul Continue(y/n)? n The symbol table is: Hash Value Address 0 1001 1 0 2 0 3 0 4 1005 5 0 6 1007 7 0 8 0 9 0 10 0

Label add

sub mul

Enter your choice: 1.Create a symbol table 2.Search in symbol table 2 Enter the label: sub The label sub is present in the symbol table address: 1005 OUTPUT symbol.txt 0 001 add 1 0 2 0 3 0 4 1005 sub 5 0 6 1007 mul 7 0 8 0 9 0 10 0 RESULT Thus the program to implement a symbol table with hashing in C language was successfully executed and the output was verified.

Anda mungkin juga menyukai