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

display 4. . insert 2. delete 3. delete 3. search 5. 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. modify 6.Enter code 1. display 4. insert 2. modify 6. search 5. exit 1 Enter the type and symbol: double b Enter code 1.

leng. fprintf are used. fscanf and to print the contents of another file.h> struct source { char label[10]. struct intermediate { int addr . char label[10]. char op[]. }sy. FILE *a.n. 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. char op[10]. symbol. struct symbol { int addr. *d.h> #include<conio. assign the files that are to be read and also assign the files that are to be written.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. struct opcode { int length. intermediate files are displayed STEP 10: Stop the program PROGRAM #include<stdio. char op[10]. void main() { int i. char label[10]. char mnem[10]. }in. mnemonics and opcode which are to be copied from one file to another.addr. . char mnem[10]. char mnem[10]. STEP 5: To read the contents of another file. }s. }o. STEP 6: The files contain label.h> #include<stdlib. *b. *c. the source.

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

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. .

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

&in.sy.label). fscanf(a.sy. strcpy(sy.label).s.addr.label."\n%d\t%s\t%s\t%s\n".op).op). .{ fscanf(b.op)==0) { strcpy(sy. fcloseall().label.in.in. getch(). if(strcmp(in.sy."%s\t\n".addr=in."*\t").mnem.op).sy. } INPUT FILES source3. printf("\n%d\t%s\n".label.addr.label).s."\n%d\t%s\n".label). } else { printf("*\t"). } } rewind(a). printf("%s\t\n".addr.label.mnem.addr. the program was executed successfully and thus the output was verified. fprintf(c.s.label.of instructions: 3 * CLOOP 502 A * B RESULT Thus the single value is passed through assembler.s.txt 0 FIRST START LOOP 500 F1 ADD A 502 A INC B OUTPUT Enter the no.s.in. rewind(b).sy. fprintf(c. sy.label). fprintf(c.in.txt FIRST START CLOOP F1 ADD A A INC B inter3."\n%s\t%s\t%s\n".sy.

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

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

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. .

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

opcode.addr). rewind(o1).m."%d". printf("\n").loc).op)."%d\t".opcode)."\n%d %s %d"."\n").m.txt 0000 COPY START C 1001 F1 ADD A 1045 A INC B 1200 NULL END F1 opcode4. } INPUT FILES inter4.mnem)==0) m.&o.opcode). } if(strcmp(in.i++) { fscanf(in1. fprintf(m1.mnem."%s %d". fprintf(m1. fprintf(m1.addr.length.rewind(m1).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.label.o.&sy.op.in."%d\t".addr=sy.mnem. fscanf(o1. printf("%d\t".sy. printf("%d\t".o. } } rewind(in1).sy.m.addr).m.opcode=o. m. if(strcmp(in. printf("%d\t".mnem.mnem.label)==0) { m."END")!=0) { if(strcmp(in.addr). fprintf(m1.m.opcode).&in."%d %s %s %s".loc=in. .addr. fscanf(sy1.txt 0 START 56 2 ADD 14 4 INC 45 symbol4.i<3.m.&o.in.label.loc). getch(). for(i=0.addr. rewind(sy1).in.

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

. } INPUT FILES inter6. getch().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.fcloseall().

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

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

} INPUT Enter the macro definition Enter the instruction MACRO Continue…[y/n] y Enter the instruction 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.str). } } fclose(fp). } else break.fwrite(&mm[z].sizeof(mm[z]). .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.pos.1. } getch().SEEK_SET).fp). puts(mm[z]. fseek(fp.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.txt MACRO SUM 25.

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

} if(strcmp(input. getch(). strcpy(table[count]. fclose(fp2). fprintf(fp2.i++) printf("%s\t%s\t\t%x\t\t%x\n". fscanf(fp1.sym_name. } while(strcmp(input.table[i].table[i].csect.input)."%s%x".table[i].add=loc+start.length).input.input).table[i]. table[count++].&loc).sym_name."%s\t%s\t\t%x\t\t%x\n".sym_name."R")!=0) { strcpy(table[count].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 .csect.add."**")."%s".input). } INPUT FILES LINKIN.if(strcmp(input. } for(i=0. fclose(fp1)."D")==0) { fscanf(fp1. while(strcmp(input.table[i].csect.table[i].length=0.&loc). fscanf(fp1."%s%x".i<count.add."E")!=0) fscanf(fp1.length).input).table[i]. start=start+length.input.table[i]."%s"."T")!=0) fscanf(fp1."T")==0) while(strcmp(input."%s". table[count].

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. .

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

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

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. .

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

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

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

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. } INPUT FILES DLL_IN.ENDA 003050 PROGB .0030e2 000051 .003000 000063 .txt H PROGA 000000 00003A D LISTA 000030 ENDA 000050 .ENDB 0030d3 PROGC . .LISTB 0030c3 .txt PROGA .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.} getch().LISTA 003030 .003063 00007f .ENDC 003124 – OUTPUT MEMORY. 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 .LISTC 003112 . 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 .

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

"%s\t%s\t%s\n".oper)."%s\t%s\t%s\n".ilab. fscanf(DEFTAB."%s\t%s\t%s\n".iopd.ilab.oper). .oper).ilab."%s%s%s". } } if(flag==0) fprintf(fp2."%s%s%s".while(!feof(DEFTAB)) { fprintf(fp2. getch().ilab.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.oper).iopd.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.ilab.iopd.iopd.oper). } fscanf(fp1. } INPUT FILES MACROIN.iopd. } fprintf(fp2. } break.

num.key. store the label in the table at the corresponding location.int). show label address if found. PROGRAM #include<stdio.IMPLEMENTATION OF SYMBOL TABLE WITH HASHING AIM To write a program to implement a symbol table with hashing in C language. STEP 4: Using switch case statement. Using if condition.i++) a[i]=0. switch(ch) { case 1: do { printf("\n Enter the address: "). int create(int). clrscr(). printf("\n Enter the label: "). Then applying the hash value. take input from user for address and label. STEP 2: Define the structure for symbol table with character label and integer address.txt STEP 7: For search. STEP 5: For creating table. lprob(a.Search in symbol table\n"). void main() { int a[MAX].i. scanf("%d". STEP 8: Stop the program.ch. do { printf("\n Enter your choice:\n1. void lprob(int[].num).h> #define MAX 11 char l[10]. ALGORITHM STEP 1: Start the program and include the header files.key. add a menu for choosing between creating table and searching values in table.&num).i<MAX. STEP 3: Declare the functions for creating displaying and setting the values in the table. get the label to be found from user. char label[10].l).h> #include<conio.h> #include<stdlib. scanf("%d". void display(int[]). . char ans.&ch). otherwise display label not present in table. void search().h> #include<string. for(i=0. }sy[11].Create a symbol table\n2. STEP 6: Display the table and print them to the file symbol.int. key=create(num). scanf("%s". struct symb { int add.

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

i<MAX.add!=0) fscanf(fp1.s."r"). } void search() { FILE *fp1.i<MAX. if(sy[i]. printf("\n Enter the label: "). } for(i=0. int i. sy[key].. int set=0. is present in the symbol tablew address: %d\n". fprintf(fp.la)==0) { set=1. fp1=fopen("symbol.i++) { printf("\n %d\t%d\t%s".%s.i++) { if(sy[i]. s=sy[i].sy[i].add.i<key&&flag==0. } fclose(fp).l).&j.label).txt".label.s). } } } void display(int a[MAX]) { FILE *fp. } } } if(set==1) printf("\n The label . for(i=0.} for(i=0. fp=fopen("symbol.label..la.sy[i].add=num.add). flag=1.i.i++) if(a[i]==0) { a[i]=num.sy[i].sy[i].la)."w").. strcpy(sy[key].i."%d%d"."\n%d%d%s".sy[i]. printf("\n Hashvalue Address Label").label). for(i=0.i<MAX. scanf("%s".i.add.i++) { fscanf(fp1.. else ."%s".add!=0) { if(strcmp(sy[i].txt". printf("\n The symbol table is: ").add.label). break. int j.&sy[i]. char la[10].

.printf("\n The label is not present in the symbol table\n").Search in symbol table 2 Enter the label: sub The label …sub… is present in the symbol table address: 1005 OUTPUT symbol.Create a symbol table 2. } INPUT Enter your choice: 1.Create a symbol table 2.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.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.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.