Ex No: 1 CREATION OF SYMBOL TABLE AIM: To write a C program to understand the working function of assembler in first pass creating symbol

table where the tables are entered in the first pass along with the corresponding addresses. ALGORITHM: STEP 1: Start the program execution. STEP 2: Create a structure for opcode table and assign the values. STEP 3: Create a structure for symbol table and assign the values. STEP 4: Create a structure for intermediate code table and assign the values. STEP 5: Write the opcode in separate file and machine code in another separate file. STEP 6: Open the opcode file and compare it with the given machine code and then generate opcode for corresponding source code. STEP 7: Check the forward reference in intermediate code and print the corresponding jump statement address. STEP 8: Compare machine code with the opcode.If any jump statement with backward reference is present, then print backward reference address. STEP 9: For symbol table, print the symbol and address of the symbol. STEP 10: Stop the program execution PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> struct table { char var[10]; int value; }; struct table tbl[20]; int i,j,n; void create(); void modify(); int search(char variable[],int n); void insert(); void display(); void main() { int ch,result=0; char v[10]; clrscr(); do {

printf("Enter ur choice:\n1.Create\n2.Insert\n3.Modify\n4.Search\n5.Display\n6.Exit"); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: insert(); break; case 3: modify(); break; case 4: printf("Enter the variabe to be searched\n"); scanf("%s",&v); result=search(v,n); if(result==0) printf("The variable does not belong to the table\n"); else printf("The location of variable is %d. The value of %s is %d", result,tbl[result].var,tbl[result].value); break; case 5: display(); break; case 6: exit(1); } } while(ch!=6); getch(); } void create() { printf("Enter the number of entries\n"); scanf("%d",&n); printf("Enter the variable and the value:\n"); for(i=1;i<=n;i++) { scanf("%s%d",tbl[i].var,&tbl[i].value); check: if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9') { printf("The variable should start with an alphabet\nEnter the correct variable name\n"); scanf("%s%d",tbl[i].var,&tbl[i].value);

goto check; } check1: for(j=1;j<1;j++) { if(strcmp(tbl[i].var,tbl[j].var)==0) { printf("The variable already exists.\nEnter another variable\n"); scanf("%s%d",tbl[i].var,&tbl[i].value); goto check1; } } } printf("The table after creation is\n"); display(); } void insert() { if(i>=20) printf("Cannotinsert. Table is full"); else { n++; printf("Enter the variable and value\n"); scanf("%s%d",tbl[n].var,&tbl[n].value); check: if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9') { printf("The variable should start with alphabet\nEnter the correct variable name\n"); scanf("%s%d",tbl[i].var,&tbl[i].value); goto check; } check1: for(j=1;j<n;j++) { if(strcmp(tbl[j].var,tbl[i].var)==0) { printf("The variable already exist\nEnter another variable\n"); scanf("%s%d",tbl[i].var,&tbl[i].value); goto check1; } } printf("The table after insertion is\n"); display(); } }

void modify() { char variable[10]. scanf("%s".&tbl[i]. else { printf("The current value of the variable%s is %d. scanf("%s%d". int result=0.value).&tbl[result].var[0] <= '9') { printf("The variable should start with alphabet\n Enter the correct variable name\n").i<=n.n). Enter the new variable and its value". printf("Enter the variable to be modified\n").i<=n. check: if(tbl[i]. if(result==0) printf("%sdoes not belong to the table".var.tbl[i].tbl[i].&variable).tbl[i].var.int n) { int flag. else return 0. break.i++) printf("%s\t\t%d\n". } void display() { printf("Variable\t value\n").value).tbl[result]. } int search(char variable[]. for(i=1. scanf("%s%d". .var.variable). display(). goto check: } } printf("The table after modification is\n").i++) { if(strcmp(tbl[i].value).value).tbl[result].variable)==0) { flag=1. result=search(variable.var. for(i=1.tbl[result]. } } if(flag==1) return i.var.var[0]>='0' && tbl[i].

Search 5.Display 6.Modify 4.Modify 4.Search 5.Create 2.Exit 1 Enter the number of entries 2 Enter the variable and the value: A 26 B 42 The table after creation is Variable value A 26 B 42 Enter ur choice: 1.Create 2.Exit 3 Enter the variable to be modified .Display 6.Modify 4.Exit 2 Enter the variable and value D 10 The table after insertion is Variable value A 26 B 42 D 10 Enter ur choice: 1.Insert 3.Display 6.Insert 3.Search 5.Create 2.Insert 3.} OUTPUT: Enter ur choice: 1.

Search 5.Modify 4.Search 5.Exit 4 Enter the variabe to be searched A The location of variable is 1.Display 6.Create 2.Search 5. The value of A is 26 Enter ur choice 1.Create 2.Modify 4. Enter the new variable and its value C 20 The table after modification is Variable value A 26 B 42 C 20 Enter ur choice: 1.Insert 3.Insert 3.Create 2.Exit 6 RESULT: Thus the program .D The current value of the variableD is 10.Modify 4.Insert 3.Display 6.Display 6.Exit 5 Variable value A 26 B 42 C 20 Enter ur choice: 1.

Ex No: 2 PASS ONE OF TWO PASS ASSEMBLER AIM: To write a C program to implement pass one of two pass assembler. STEP 3: Check for specified table in the symbol table. STEP 6: For RESW."%s". }ps. STEP 8: Store the address value as program address STEP 9: Stop the program execution PROGRAM: #include<stdio.operand[10]. if(strcmp(str.instr[10].h> struct sourcefile { char label[10]. STEP 5: For word increment the value by 3. pse=fopen("pseudo. }s.h> #include<stdlib. ALGORITHM: STEP 1: Start the program execution. STEP 7: For BYTE. increment according to instructions.s)==0) { fclose(pse).s)."r").h> #include<conio.ps. char label[10]. STEP 4: If symbol is not found.ps. int find(char str[10]) { FILE *pse. }res.instr[10]. show any error message or add it to SYMTAB. struct pseudocd { char s[10]. while(!feof(pse)) { fscanf(pse. operand value is incremented by 3 into locctr. STEP 2: Create a symbol table. . struct source_result { int address.oper[10].txt".

while(!feof(ss)) { fscanf(ss.res.*ss1. fclose(ss).s.oper. } } fclose(pse). clrscr().label. fprintf(ss1.detect. return 0. } } printf("\nPass 1 Completed").address=addr.s.operand)-3.address+3.instr.instr.operand).address.instr. else if(strcmp(s.s.instr. else addr=res. getch()."START")==0) addr=atoi(s.res. . ss1=fopen("res.operand)).txt". } else { printf("Error Encounted").instr)."w+").res.s.res. strcpy(res.address+(3*atoi(s. if(detect==1) { if(strcmp(s. res. strcpy(res. else if(strcmp(s. strcpy(res.instr."RESB")==0) addr=res.label).address+3.instr).return 1. else if(strcmp(s."RESW")==0) addr=res.s."BYTE")==0) addr=res.operand).label.instr."%s%s%s".address+atoi(s.txt". detect=find(s.address+strlen(s.oper).operand)."WORD")==0) addr=res. } void main() { FILE *ss. ss=fopen("s. int addr.operand)."r+").instr."%d\t%s\t%s\t%s\n".instr. else if(strcmp(s. fclose(ss1).s.label.

getch(). STEP 4: Opcodes in ‘BYTE’ or ‘WORD’ is converted from constant to object code. } OUTPUT: Input: pseudo. .fclose(ss). STEP 3: Search SYMTAB for operand value. provided symbol is found in the operand field.txt NULL START 1000 A RESB 100 LOOP READ A B BYTE C"LOOP" C WORD 100 G RESW 5 NULL END NULL Output: Pass 1 Completed res. fclose(ss1). ALGORITHM: STEP 1: Start the program execution.txt START READ WORD BYTE RESW RESB END s. STEP 2: Assemble the instructions and generate object program. STEP 6: Stop the program execution.txt 0 NULL START 1000 1000 A RESB 100 1100 LOOP READ A 1103 B BYTE C"LOOP" 1107 C WORD 100 1110 G RESW 5 1125 NULL END NULL Ex No: 3 PASS TWO OF TWO PASS ASSEMBLER AIM: To write a C program to implement pass two of two pass assembler. STEP 5: Write object program and assembly listing.

opcode. }op. struct res { int a.&res. r=fopen("re.address."r").&res.*symb.code[3].txt". struct opd { int address.h> #include<string."%d%s%s".len. }opcode. rewind(symb). char label[10].txt". }s.h> #include<conio.&opcode."r"). char mnemonic[10].address=res.mnemonic."r").instr[10]. while(!feof(r)) { fscanf(r.j. }res.txt". .instr.mnemonic)==0) { op. symb=fopen("symbol. char c[10]. char code[10]. clrscr(). rewind(o).txt".operand[10]. found = 0. void main() { FILE *r."w").*o.*result.found=0.code).&res.&res.label. struct source_result { int address. char s1[10].address.&opcode. if(strcmp(res.&opcode. int i. o=fopen("opcode.PROGRAM: #include<stdio. result=fopen("output."%d%s%s%s".instr. while(!feof(o)) { fscanf(o.h> struct opc { int len.l.operand).

i--) fprintf(result. found=1. break.j++) fprintf(result.i>l. l=strlen(s1)-1. } OUTPUT: opcode.a."%d\n". fprintf(result. if(strcmp(res.c)==0) { fprintf(result.operand).operand. while(!feof(symb)) { fscanf(symb."NULL")==0) { fprintf(result."%d%s"."0000").strcpy(op. for(j=1.&s."%d\t%s". } } } printf("Pass 2 completed!"). } else if(res.txt 0 NULL START 1000 1000 A RESB 100 1100 LOOP READ A .txt 1 READ 1F 1 LDA 00 re.code).res.op."\n").&s.code.s1[j]). fcloseall(). getch().operand[0]=='#') { strcpy(s1."%c". fprintf(result.s.address. break.c).s.operand. } } if(found==0) continue.op.a).j<=l.code). for(i=4."0"). break.opcode. } else if(strcmp(res. break.

txt 1000 A 1100 LOOP 1107 C 1110 G 1125 CLOOP Output Pass 2 completed! output.cstr[15].txt 1100 1F1000 1125 000005 Ex No: 4 IMPLEMENTATION OF SINGLE PASS ASSEMBLER AIM: To write a C program to implement single pass assembler. PROGRAM : #include<stdio. clrscr(). int i.*f2. ALGORITHM: STEP 1: Start the program execution. while(1) { ch=getchar().1103 B BYTE C"INDIA" 1107 C WORD INDIA 1110 G RESW 5 1125 CLOOP LDA #5 1128 NULL END NULL symbol. char ch.q. f1=fopen("asin". some of the forward reference problem are existed.str1[10].r. STEP 2: Assembler simply generate object code as it scans the source code. STEP 4: When nearly half the program translation is over. STEP 3: If the instruction operand is a symbol text.num. STEP 5: Combine the process to the end of the program to fill forward reference property. the symbol table entries with ‘x’ are undefined. STEP 6: At the end of the program. the operands address is omitted.j. printf("Enter your assembly instructions\n").str[30].h> #include<stdlib."w"). has not yet been defined.str2[30].h> void main() { FILE *f1.h> #include<conio. STEP 7: Stop the program execution. .

f2).f2). } fclose(f1).i++) { str2[j]=str[i]."w")."add"))==0) { fputs("80\n". j++.str.f1).25."lda"))==0) { fputs("3a\t". break. } else if((strcmp(str1. } else if((strcmp(str1.i<strlen(str).f2)."hlt"))==0) { fputs("76\n". } else if((strcmp(str1. while(1) { fgets(str.f1).f2). f1=fopen("asin".f2). ."sub"))==0) { fputs("90\n".3). str1[3]='\0'. f2=fopen("asout". j=0."r"). num=atoi(str2) q=num/100."mov"))==0) { fputs("47\n". } else if((strcmp(str1. for(i=3. } else if((strcmp(str1. fputc(ch.if(ch=='*') break. } str2[j]='\0'.f2). strncpy(str1."sta"))==0) { fputs("32\t".f2). fputs(str2. if((strcmp(str1.

ALGORITHM: .f2). getch().10). f2=fopen("asout". } else { fputs("error\n". fclose(f2).cstr. } } fclose(f1).f2). fputs(itoa(q. ch=fgetc(f2). } OUTPUT: Input Enter your assembly instructions lda 5000 sub z sta 9988 hlt * Output THE OBJECT CODE CONTENTS 3a 00 50 90 32 88 99 76 Ex No: 5 IMPLEMENTATION OF MACROPROCESSOR AIM: To write a C program to implement MACROPROCESSOR."r").f2).r=num%100. if(r==0) fputs("00\t".f2). ch=fgetc(f2).f2). printf("\nTHE OBJECT CODE CONTENTS\n"). while(ch!=EOF) { putchar(ch). fputs("\t".10).cstr. } fclose(f2). else fputs(itoa(r.f2). fputs("\n".

STEP 1: Start the program execution. printf("enter the text filename \n"). if(strcmp(iopd."w").n). STEP 3: The instructions with ‘macro’.h> #include<stdlib.ilab. for(i=0. .oper).oper[20].ilab.add. while(!feof(fp1)) { fscanf(fp1. while(strcmp(iopd.h> void main() { char n1.h> #include<string. STEP 2: Macro instructions are included in a separate file. FILE *fp1.’call’ on them should not be printed in the output."r").h> #include<conio.iopd. fscanf(fp1. n=0.of macros=%d\n".fn[i]).oper). } printf("no. rewind(fp1)."%s%s%s". char fn[10][10].c1.sub etc with their values.iopd. while(!feof(fp1)) { fscanf(fp1.oper).store.i. p[i]=fopen(fn[i]. fp1=fopen("z:\macin."%s%s%s\n".iopd.’mend’. STEP 4: Print all other instructions such as start."%s%s%s".n."macro")==0) n++.load.ilab.iopd."%s%s%s".ilab. if(strcmp(iopd.oper).i++) { scanf("%s". n1=n. clrscr(). PROGRAM: #include<stdio.m[20][3].oper).iopd[20]. fscanf(fp1."mend")!=0) { fprintf(p[n].opd[20].*p[5].*fp2.ilab[20].ilab.txt"."macro")==0) { strcpy(m[n].i<n.oper).iopd."%s%s%s". } n=0. STEP 5: Stop the program execution.

"%s%s%s".oper).ilab."r").i<n1.b ** mend --** macro m2 ** lda b ** mend --- .iopd."%s%s%s\n".ilab."call")==0) { for(i=0. rewind(fp1).ilab. } fprintf(fp2. c1=0.oper).txt". fscanf(p[i].oper)."w").oper). fp2=fopen("z:\outm.oper).iopd. } } for(i=0.i<n1."%s%s%s".i++) { if(strcmp(m[i].} fclose(p[n]).i++) p[i]=fopen(fn[i]."%s%s%s". while(!feof(p[i])) { fprintf(fp2.ilab.txt ** macro m1 ** move a. } OUTPUT: Input: macin."%s%s%s". fscanf(fp1. n++. while(!feof(fp1)) { if(strcmp(iopd.oper)==0) { rewind(p[i]).oper).iopd. c1=1.ilab."%s%s%s\n". } } } if(c1!=1) fprintf(fp2.oper). fscanf(fp1.iopd."%s%s%s".iopd. fscanf(p[i].ilab.iopd.ilab.iopd. } break.

b Output: No.b mac2. STEP 8: Display the text address. ALGORITHM: STEP 1: Start the program execution. STEP 6: Display the starting address. of Macros =2 Enter the Text file Names Mac1.b ** mend--** macro m2 ** lda b ** mend --** start 1000 ** lda a ** move a.dat Mac2. STEP 2: Get the header record.dat outm.b mac1. STEP 9: Stop the program execution PROGRAM: #include <stdio.b callm1 ** lda b callm2 ** add a.txt ** macro m1 ** move a. STEP 5: Display the program name.h> #include <conio. STEP 7: Display the program length.dat ** lda b Ex No: 6 IMPLEMENTATION OF ABSOLUTE LOADER AIM: To write a C program to implement absolute loader.h> void main() .** start 1000 ** lda a ** call m1 ** call m2 ** add a.dat ** move a. STEP 3: Get the text record. STEP 4: Get the end record.

if(ch=='-') { f1=1.i<=5. int i=0.j++) stadd[j]=(ch=fgetc(myfile)).fn).stadd[15]. myfile=fopen("in. } while((!feof(myfile)) && f1==0) { while(f1==0) { ch = fgetc(myfile).txt". } } printf("Program name is %s \n".{ FILE *myfile. exit(0). char ch. if(myfile == NULL) { puts("Cannot open the file").k++) textrec[k]=(ch=fgetc(myfile)). for(i=0.k.j. while(!feof(myfile)) { ch=fgetc(myfile). i++. if(ch=='T') for(k=1. } if(ch != 'H' && f==1 && f1==0) { fn[i]=ch.f=0.j<=12.k<=80. clrscr().f1=0. break."r"). if((ch=='H' || ch=='h' ) && f==0) f=1. } } if(f1==1) for(j=0.i++) .textrec[80]. stadd[j]='\0'. printf("\n Starting Address "). fn[i]='\0'. textrec[k]='\0'.fn[30].

TXT HCOPY-001000-00107A T001000-141033-482039-001036-281030-301015-482061-00102D 001000 Output Program name is COPY Starting address 1000 Length of the Program 00107A Text Address 001000 141033 482039 001036 281030 301015 482061 00102D 001000 Ex No: 7 IMPLEMENTATION OF RELOCATION LOADER AIM: To write a C program to implement relocation loader. .i<=12.stadd[i]). } Input IN.where current string is relocating bit. STEP 3: Transfer the input array into output array. for(i=1. for(i=7.printf("%c". STEP 5: Relocating bit is subjected to required changes before transferring input to output and also move object code STEP 6: Stop the program execution. STEP 2: Enter the starting address location for relocating the object code. } getch().i<=80.textrec). ALGORITHM: STEP 1: Start the program execution.i++) printf("%c". STEP 4: Convert the current string array into binary form.textrec[i]). printf("\n \n Text Address %s \n". else printf("\n").stadd[i]).i++) { if((i%7)!=0) printf("%c". printf("\n Length of the Program ").

start=atoi(stloc). strcpy(output[i]. int len. while(strcmp(input[i]. FILE *fp1.10)."T")==0) { for(j=0.count=0."%s".input[i]). fscanf(fp1.inc=0. strcpy(output[i]. strcpy(output[i].address[20].input[i]).tlen=0.textloc.output[2]."NULL")."w"). while(strcmp(input[i]. fscanf(fp1. } itoa(start.dat".2).h> #include<conio. scanf("%s". tloc=start. itoa(bitmask.input[i]).h> #include<stdlib. textloc=atoi(output[i-2]). if(strcmp(input[i].tloc.stloc[10].binary[20]. }obcode[300].i=0."E")!=0) { strcpy(output[i].binary."%s". printf("Enter the location where the program has to be loaded:\n"). char add[10].input[i]). fscanf(fp1.stloc).j.j<3.input[i]).num=0. } bitmask=atoi(output[i]).j++) { i++."r")."T")!=0) { strcpy(output[i].h> #include<string.output[100][16].PROGRAM: #include<stdio. void main() { char input[100][16].input[i]).n.location. fp1=fopen("z:\ssinput.loc. clrscr().start.k. fp2=fopen("z:\ssoutput.dat". location=start.*fp2.h> struct object_code { int locctr."%s". .input[i]). i++.bitmask.

address. itoa(textloc.add.address). } k++. len=strlen(output[i]). for(j=2.j++) { address[num]=output[i][j]."xx"). } tlen=atoi(output[i-1]).n<len."%s".10).input[i]). strcat(output[i]. } else { if(binary[k]=='1') { num=0. if(num>1) { obcode[inc++]. len=strlen(output[i]).textloc=textloc+start. for(n=0. } strcpy(output[i]. } } } i++. num=0. output[i][j]='\0'. itoa(loc.locctr=location++.NULL).locctr=location++. strcpy(address.output[i-2]. num=0. for(n=0. num++. . tloc=textloc.n++) { strcpy(obcode[inc].10).j<len.add[num++]=output[i][n]. loc=loc+start.n++) { obcode[inc]. fscanf(fp1.n<(textloc-(tloc+tlen)). k=0. } loc=atoi(address).input[i]). obcode[inc++].

} } getch(). loc=loc+start.obcode[n]."%s". i=0. i=0."\n%d\t".i++. fscanf(fp1. n=0.locctr).dat 3000 14303543 40391030 36283030 30006548 3016 10613110 03200030 21103320 0033xx14 .n<inc.obcode[n+1]. if(i>3) { fprintf(fp2. count++. } if(count>3) { fprintf(fp2.input[i]).itoa(loc.add). i++. count=0."\t").obcode[n]. strcpy(output[i]. loc=atoi(input[i]). } OUTPUT: Input: ssinput.address.10))."%s". for(n=0."%d\t". fprintf(fp2.locctr).dat H COPY 000000 001073 T 000000 10 015 140035 431039 100036 280030 300065 481061 311003 200030 211033 200033 T 000011 19 045 140036 481060 380033 412000 454196 100005 20000 T 000031 15 135 140030 430030 141013 301044 241064 210030 301057 546275 212064 381045 T 000058 05 056 100036 520000 151000 301000 T 000065 19 080 340030 141079 301064 503039 152079 220030 3810064 432000 25 E 000000 Output: Enter the location where the program has to be loaded: 3000 ssoutput.n++) { fprintf(fp2. count=0.

h> #define MAX 10 struct estab { char csect[10]."r"). char input[10]. scanf("%lx". } table[MAX]. clrscr(). int length. STEP 7: Stop the program execution.h> #include<conio.h> #include<stdlib. STEP 5: Enter the symbol into the symbol table with address and also with control section address STEP 6: Address of the control section and control section length together is the starting address of next control section.3032 30364810 60383033 41500045 41961030 3048 052000xx 14303043 00301410 13301044 3064 24106421 30303040 57549275 21206438 3080 1045xxxx xxxxxxxx xxxxxxxx xxxx1030 3096 36523000 15400030 1000xxxx 34303014 3112 10793040 64503039 15207922 00303810 3128 06432000 25 Ex No: 8 IMPLEMENTATION OF PASS ONE OF DIRECT LINKING LOADER AIM: To write a C program to implement pass one of direct linking loader. STEP 2: Enter the location when the program has to be loaded.c".&start). void main() { FILE *fp1. STEP 4: Read the loader record of control section.*fp2. STEP 3: Assign the address got from the user as the first control section address. . fp1=fopen("link1in."w"). printf("Enter the location where the program has to be loaded"). char sym_name[10]. ALGORITHM: STEP 1: Start the program execution.length. long int add. long int i.loc. fp2=fopen("file1.c".count=0. PROGRAM: #include<stdio.start.

table[i]."%s". } while(strcmp(input."T")!=0) fscanf(fp1.input."T")==0) while(strcmp(input.table[i]. while((strcmp(input."%s".fprintf(fp2.&loc). } OUTPUT: Input File: link1in."%s". rewind(fp1).input)."CSECT\t\tSYMNAME\t\tADDRESS\tLENGTH\n")."%s". table[count++]."\0"). strcpy(table[count]. fscanf(fp1. fscanf(fp1.add=loc+start.csect. } for(i=0.add=atoi(input)+start. start=start+length.table[i]."\0"). if(strcmp(input."%s\t\t%s\t\t%lx\t\t %d\n".input). fscanf(fp1.i<count.sym_name.&loc)."E")!=0) fscanf(fp1."%s". fscanf(fp1."H")==0) { fscanf(fp1."%s".input).sym_name.input). table[count++]."%s".csect.input). length=atoi(input).length=atoi(input).c H PROGA 000000 000070 . table[count].input). strcpy(table[count].csect. table[count].add."R")!=0)) { strcpy(table[count].table[i]."%s". getch(). while(!feof(fp1)) { fscanf(fp1."%s%lx".sym_name. } if(strcmp(input.length).input).input).input)."%s%lx"."D")==0) { fscanf(fp1.input). strcpy(table[count].input.i++) fprintf(fp2.length=0. fscanf(fp1. } if(strcmp(input.

c CSECT SYMNAME ADDRESS LENGTH PROGA 3000 70 LISTA 3040 0 ENDA 3054 0 PROGB 3046 88 LISTB 30a6 0 ENDB 30b6 0 PROGC 309e 57 LISTC 30a1 0 .D LISTA 000040 ENDA 000054 R LISTB ENDB LISTC ENDC T 000020 10 03201D 77100004 150014 T 000054 16 100014 15100006 00002F 100014 FFFFC0 M 000024 05+LISTB M 000054 06+LISTC M 000058 06+ENDC M 000064 06+LISTB E 000000 H PROGB 000000 000088 D LISTB 000060 ENDB 000070 R LISTA ENDA LISTC ENDC T 000036 11 031000000 772027 05100000 T 000070 18 100000 05100000 05100020 05100030 100000 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 PROGC 000000 000057 D LISTC 00003 ENDC 00042 R LISTA ENDA LISTB ENDB T 000018 12 031000000 77100004 05100000 T 000042 15 100030 100008 1000011 100000 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 Enter the location where the program has to be loaded 3000 Output file file1.

Found symbol address is added or subtracted with corresponding symbol address and value at starting location. clrscr(). int i. STEP 5: In the modification record. int padd. fp1=fopen("link1. ALGORITHM: STEP 1: Start the program execution.address[10].x. PROGRAM: #include<conio. void main() { FILE *fp1.operation.c".location.ml en[30].input[10]. search for symbol to be modified from external table. character form is converted to machine representation.pstart.loc.count=0.c". int add. char *add1. char sname[10].*fp3.mloc[30]. } obcode[500]. int plen.*fp2."r").lbl[10].num=0.h> #include<stdio. fp2=fopen("link2.inc=0.record=0.h> #include<stdlib.label[30][10].exeloc. .ENDC 30e0 Ex No: 9 IMPLEMENTATION OF PASS TWO OF DIRECT LINKING LOADER AIM: To write a C program to implement pass two of direct linking loader.n=0. STEP 4: Move the object code from record to memory location. STEP 6: Stop the program execution.y.textloc.h> struct ext_table { char csect[10]. long int newadd.start. STEP 3: In the text record.h> #include<string."r"). struct object_code { char code[15]. STEP 2: Find the control section length from the header record.length. } estab[20].j.textlen.

padd. while(!feof(fp2)) { fscanf(fp2."%d".&textlen)."M")==0) { fscanf(fp1.estab[num]. rewind(fp3)."%d"."xx").&textloc).i++) if(strcmp(input. textloc=textloc+pstart.padd. . } do { if(strcmp(input. } while(strcmp(input.i<num. start=loc.code. rewind(fp2).&estab[num].csect. loc=textloc+textlen.i++) { strcpy(obcode[inc]. for(i=0. rewind(fp1).c". num++.i<(textloc-loc)."T")!=0) fscanf(fp1."%s".input).csect)==0) { pstart=estab[i]. } exeloc=estab[0]."w"). mloc[record]=mloc[record]+pstart.fp3=fopen("link3. loc=exeloc.estab[i]."%s". while(!feof(fp1)) { fscanf(fp1."T")==0) { fscanf(fp1."%d". for(i=0.&estab[num].padd.plen)."%s". break. } fscanf(fp1.&mloc[record]).input).input).sname. } else if(strcmp(input. obcode[inc++]. if(strcmp(input."H")==0) { fscanf(fp1.estab[num].add=start++."%s%s%d %d".

count++.label[record++]).n++) { operation=label[n][0]. x=0.i++) if(strcmp(lbl.input). } else { length=strlen(input)."\0"). } } } fscanf(fp1. location=mloc[n]-exeloc."%s". fscanf(fp1. x=0.i<length. if(x>1) { obcode[inc++].add=start++. while(length<mlen[n]) { strcat(address.sname)==0) break. length+=2. loc=location. for(i=0. } for(n=0.input).estab[i]. lbl[length-1]='\0'."%s"."E")==0) fscanf(fp1. switch(operation) { . length=0. } while(strcmp(input.&mlen[record]).code[x++]=input[i]."E")!=0).fscanf(fp1."%d". count=0.i<num. strcpy(address. for(i=1.i++) { obcode[inc].n<record. length=strlen(label[n])."%s". if(strcmp(input.i++) lbl[i-1]=label[n][i].code).obcode[location++].i<length. } for(i=0.

address.code[x++]=address[y++].10)-(long int)estab[i].i<inc. count--. } } } count=0.obcode[0]. } } getch(). y=0. count++.case '+':newadd=strtol(address. case '-':newadd=strtol(address. n=0.10)+(long int)estab[i]."\t"). fprintf(fp3.code).padd. break. n++."\n%d\t"."%s".obcode[i].add). loc++. count=0. for(i=0.obcode[i].padd."%d\t". } OUTPUT: Input: link1. while(count>0) { obcode[loc]. if(n>3) fprintf(fp3. break.10).i++) { fprintf(fp3. ltoa(newadd. n++. if(n>3) { fprintf(fp3."%s".obcode[i+1].&add1.add).c H PROGA 000000 000070 . } if(count>3) { fprintf(fp3. n=0.&add1. if(x>1) { x=0. x=0.code).

c CSECT SYMNAME ADDRESS LENGTH PROGA 4000 70 LISTA 4040 0 ENDA 4054 0 PROGB 4046 88 LISTB 40a6 0 ENDB 40b6 0 PROGC 409e 57 LISTC 40a1 0 ENDC 40e0 0 Output: link3.c .D LISTA 000040 ENDA 000054 R LISTB ENDB LISTC ENDC T 000020 10 03201D 77100004 150014 T 000054 16 100014 15100006 00002F 100014 FFFFC0 M 000024 05+LISTB M 000054 06+LISTC M 000058 06+ENDC M 000064 06+LISTB E 000000 H PROGB 000000 000088 D LISTB 000060 ENDB 000070 R LISTA ENDA LISTC ENDC T 000036 11 031000000 772027 05100000 T 000070 18 100000 05100000 05100020 05100030 100000 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 PROGC 000000 000057 D LISTC 00003 ENDC 00042 R LISTA ENDA LISTB ENDB T 000018 12 031000000 77100004 05100000 T 000042 15 100030 100008 1000011 100000 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 link2.

h> #include<stdio. get the file name to be edited. STEP 9: Stop the program execution. PROGRAM: //implementation of text editor #include<iostream. enter the new text. STEP 7: If choice is 4.0 xxxxxxxx 16 xxxxxxxx 32 xxxxxxxx 48 xxxxxxxx 64 xxxxxxxx 80 0xxxxxx 96 045150014 112 xxxxxxxx 128 10000600 144 77202705 160 xxxx0xx 176 00051000 192 03100000 208 xxxxxxxx 224 01100000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 0xxxxxx xxxxxxxx xxxxxxxx 22F1000 100000xx xxxx0xx 00051000 77100004 xxxxxxxx 100000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxx0320 xx0xxxx xxxxxxxx 14FFFFC0 xxxxxxxx xxxx0xx 20051000 05100000 10003010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 0xxxxxx 1D771000 0xxxx0 101415 03100000 xxxx0xx xxxx1000 30100000 xxxxxxxx 00081000 Ex No: 10 IMPLEMENTATION OF SIMPLE TEXT EDITOR AIM: To write a C program to implement simple text editor. create a new file.h> #include<fstream. void append(). STEP 8: If choice is 5.h> #include<conio. exit.h> #include<process. view the required file. STEP 2: List the menu. ALGORITHM: STEP 1: Start the program execution. STEP 5: If choice is 2. STEP 4: If choice is 1. void view(). STEP 3: Get the choice. delete the file. class texteditor { public: void create(). STEP 6: If choice is 3.h> #include<stdlib.c. .h> char fn[10].

open(fn).CREATE\n2. break.DELETE\n5. texteditor obj.del(). break. cout<<"\n1.VIEW\n3. fp1. cin>>choice. default: exit(0).append(). case 4: obj. cout<<"\nSAVE AND EXIT: CTRL &S\n". break. cout<<"\nENTER THE CONTENTS OF THE FILE:". cin>>fn. }. break. case 5: exit(0). while(1) { c=getch().view(). while(1) { clrscr().EXIT\nENTER YOUR CHOICE:". break. ofstream fp1.void del(). . case 2: obj.create().APPEND\n4. case 3: obj. void main() { int choice. } } } //Creation of file void texteditor::create() { cout<<"\nENTER THE FILE NAME:". switch(choice) { case 1: obj.

while(!fp1. } //Procedure for appending data to a file void texteditor::append() { cout<<"\nSAVE:CTRL &S". cin>>fn. ifstream fp1(fn). .close(). } else if(c==19) { cout<<"\nFILE SAVED AND CLOSED\n".30). cout<<str<<"\n". cout<<"\nENTER THE FILE NAME:".if(c==32) { cout<<' '. fp1<<c. break. fp1<<c. fp1<<"\n". getch(). } } } //Procedure for viewing the content of file void texteditor::view() { char str[30].close(). } else if(c==13) { cout<<"\n". cout<<"\nENTER THE FILE NAME:". } fp1.eof()) { fp1. getch().getline(str. } else { cout<<c. fp1.

cin>>fn. getch(). while(1) { c=getch(). cout<<"\n". cout<<"\nFILE DELETED SUCCESSFULLY\n". fp1<<c.close(). ofstream fp1(fn.CREATE 2.APPEND 4. cin>>fn. } } fp1. } else { cout<<c. } //Procedure for deleting a file void texteditor::del() { cout<<"\nENTER THE FILE NAME:". fp1<<c.DELETE 5. if(c==13) { c='\n'. } else if(c==19) { break. remove(fn).EXIT ENTER YOUR CHOICE:1 ENTER THE FILE NAME: ANAND ENTER THE CONTENTS OF THE FILE: SAVE AND EXIT: CTRL &S FRIENDS FILE SAVED AND CLOSED ENTER YOUR CHOICE:2 ENTER THE FILE NAME: ANAND .VIEW 3.ios::ate). } OUTPUT: 1.

FRIENDS ENTER YOUR CHOICE:3 SAVE:CTRL &S ENTER THE FILE NAME:ANAND JEYRAJ ENTER YOUR CHOICE:4 ENTER THE FILE NAME:ANAND FILE DELETED SUCCESSFULLY ENTER YOUR CHOICE:5 EXIT… .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

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