You are on page 1of 49

1

SYSTEM SOFTWARE
CS2308

LAB MANUAL

COMPUTER SCIENCE & ENGINEERING KARPAGA VINAYAGA COLLEGE OF ENGG AND TECHNOLOGY MADURANTAKAM

STAFF REFERENCE

S.PRABHU. MTech LECTURER

Prepared by : S.PRABHU Lecturer KVCET

2 INDEX
Sn 1 Program Title Pag e Sign

Symbol Table

2

One Pass Assembler

3

Two Pass Assembler

4

Single Pass Assembler

5

Single Pass Macro Processor Absolute Loader

6

7

Relocating Loader

8

One Pass Direct-Linking Loader

9

Two Pass Direct-Linking Loader

10

Simple Text Editor

11

Two Pass Macro Processor

12

Symbol Table With Hashing QUESTIONS IT2308 SYSTEM SOFTWARE LAB

Prepared by : S.PRABHU Lecturer KVCET

3

1.

Implement a symbol table with functions to create, insert, modify, search, and display.

2.

Implement pass one of a two pass assembler.

3.

Implement pass two of a two pass assembler.

4.

Implement a single pass assembler.

5.

Implement a two pass macro processor

6.

Implement a single pass macro processor.

7.

Implement an absolute loader.

8.

Implement a relocating loader.

9.

Implement pass one of a direct-linking loader.

10.

Implement pass two of a direct-linking loader.

11.

Implement a simple text editor with features like insertion / deletion of a character, word, and sentence.

12.

Implement a symbol table with suitable hashing

IMPLEMENTATION OF SYMBOL TABLE
EX. NO : 1 DATE:

Prepared by : S.PRABHU Lecturer KVCET

4

Aim:
To write a C program to implement Symbol Table for performing insert, display, delete, search and modify.

Algorithm:
Step 1:Start the program for performing insert, display, delete, search and modify option in symbol table. Step 2: Define the structure of the Symbol Table. Step 3: Enter the choice for performing the operations in the symbol Table. Step 4: If the entered choice is 1, search the symbol table for the symbol to be inserted. If the symbol is already present, it displays “Duplicate Symbol”. Else, insert the symbol and the corresponding address in the symbol table. Step 5: If the entered choice is 2, the symbols present in the symbol table are displayed. Step 6: If the entered choice is 3, the symbol to be deleted is searched in the symbol table. If it is not found in the symbol table it displays “Label Not found”. Else, the symbol is deleted. Step 7: If the entered choice is 5, the symbol to be modified is searched in the symbol table. The label or address or both can be modified.

Source Code:
# include <stdio.h> # include <conio.h> # include <alloc.h> # include <string.h> # define null 0 int size=0; void insert(); void del(); int search(char lab[]); void modify(); void display(); struct symbtab { char label[10]; int addr;

//Function declarations//

Prepared by : S.PRABHU Lecturer KVCET

display(). printf("5. case 3: del(). struct symbtab *first.&op). } } while(op<6). case 4: printf("Enter the label to be searched : "). scanf("%s". DISPLAY\n"). DELETE\n"). case 2: display(). }. int y. printf("3. INSERT\n"). display(). break. break. case 6: break. break. printf("Enter your option : "). char la[10].la). END\n").PRABHU Lecturer KVCET . getch(). y=search(la). printf("1. scanf("%d". if(y==1) { printf("The label is already in the symbol Table"). printf("4. printf("6. case 5: modify(). do { printf("\nSYMBOL TABLE IMPLEMENTATION\n"). switch(op) { case 1: insert(). void main() //Main function Start// { int op. } else { printf("The label is not found in the symbol table"). } break.*last.5 struct symtab *next. } //Main function end// Prepared by : S. SEARCH\n"). break. display(). clrscr(). MODIFY\n"). printf("2.

} } void display() { int i.i++) { printf("%s\t%d\n". p=p->next.PRABHU Lecturer KVCET .i<size. } } int search(char lab[]) { int i. printf("Enter the label : ").p->addr). } else { struct symbtab *p. } else { last->next=p.l). p->next=null.l). struct symbtab *p.p->label. Duplicate cant be inserted\n"). struct symbtab *p. last=p. p=malloc(sizeof(struct symbtab)). scanf("%s".flag=0. n=search(l).6 void insert() //Function definitions// { int n. last=p. } size++. scanf("%d". if(n==1) { printf("The label already exists. printf("Enter the address : "). if(size==0) { first=p. p=first. for(i=0. printf("LABEL\tADDRESS\n"). strcpy(p->label.&p->addr). Prepared by : S. char l[10].

s=search(l). printf("2. int add.nl[10]. i. printf("Enter the new address\n"). Only the address of a particular label\n"). p=first. scanf("%s". } return flag.l). for(i=0. printf("Enter the new label\n").i++) { f(strcmp(p->label. printf("1. if(s==0) { printf("NO such label"). } p=p->next. struct symbtab *p. Only the label\n"). } } break. switch(choice) { case 1: printf("Enter the old label\n"). Prepared by : S. s. printf("3.&choice). choice. } void modify( ) { char l[10].l).lab)==0) { flag=1.i++) { if(strcmp(p->label.i<size. scanf("%s".nl). printf("Enter your choice : ").7 p=first. scanf("%s".PRABHU Lecturer KVCET . Both the label and address\n").nl). scanf("%d". case 2: printf("Enter the label whose address is to modified\n"). } p=p->next. printf("What do you want to modify?\n").i<size. } else { for(i=0.l)==0) { strcpy(p->label.

Prepared by : S.i<size. case 3: printf("Enter the old label : ").i++) { if(strcmp(p->label. scanf("%d".l)==0) { p->addr=add.8 scanf("%d". } } break.i++) { if(strcmp(p->label.&add).PRABHU Lecturer KVCET . } else { for(i=0. scanf("%s".nl).*q. p->addr=add. char l[10]. } else { for(i=0. struct symbtab *p.i<size. s=search(l). printf("Enter the new address : ").l). } } void del( ) { int a. } } break.&add). } p=p->next. p=first. if(s==0) { printf("NO such label").l)==0) { strcpy(p->label. scanf("%s". printf("Enter the new label : "). s=search(l). if(s==0) { printf("NO such label").nl). } p=p->next.

} p->next=q->next. if(a==0) { printf("Label not found\n").9 printf("Enter the label to be deleted\n"). INSERT 2.PRABHU Lecturer KVCET . q=q->next. DISPLAY 3. } else { if(strcmp(first->label. q=q->next. } else if(strcmp(last->label.l)!=0) { p=p->next. } p->next=null. } size--. while(strcmp(q->label.l)==0) { q=p->next. SEARCH Prepared by : S. } else { q=p->next. } } Sample Output: SYMBOL TABLE IMPLEMENTATION 1.l)!=0) { p=p->next. a=search(l). scanf("%s". while(strcmp(q->label.l)==0) { first=first->next. DELETE 4. last=p.l).

DELETE 4. DISPLAY 3. INSERT 2. MODIFY 6. SEARCH 5. MODIFY Enter your option :1 Enter the label :B Enter the address :10 The variable should be inserted. INSERT 2. SYMBOL TABLE IMPLEMENTATION 1.10 5. SEARCH 5. DISPLAY Prepared by : S.PRABHU Lecturer KVCET . Enter your option: 2 Lable A B address 5 10 SYMBOL TABLE IMPLEMENTATION 1. DISPLAY 3. MODIFY Enter your option: 1 Enter the label: B Enter the address: 10 The label already exists. DELETE 4. END Enter your option: 1 Enter the label :A Enter the address :5 The variable should be inserted. Duplicate cant be inserted SYMBOL TABLE IMPLEMENTATION 1. INSERT 2.

MODIFY Enter your option: 5 What do you want to modify 1. DELETE 4.11 3. No : 2 Date: Prepared by : S.PRABHU Lecturer KVCET . Only the address of a particular label 3. Only the label 2. Both the label and address Enter your choice: 1 Enter old label:B Enter new label:G Enter your option: 2 Lable A B G address 5 10 10 RESULT: Thus the program is executed successfully and the result is obtained. SEARCH 5. IMPLEMENTATION OF PASS ONE OF TWO PASS ASSEMBLER Ex.

fp4=fopen("OPTAB. then the location counter is incremented by 3."w").*fp2. the location counter is incremented by3.label[10]. the location counter value is incremented by operand value."r"). Step 7: If the opcode is present.operand[10].h> #include<conio. the operand value is multiplied by 3 and then the location counter is incremented.h> #include<string. Algorithm: Step 1: Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode Step 2: Read the source program Step 3: If the opcode read in the source program is START."r").DAT". FILE *fp1. the location counter is auto incremented. fp3=fopen("OUTPUT. Step 6: Check whether the opcode read is present in the operation code table. Step 4: Else the location counter is initialized to 0. Prepared by : S."w"). fp2=fopen("SYMTAB.*fp3. Step 10: If the opcode read is RESB. Step 12: The length of the source program is found using the location counter value.mnemonic[10].PRABHU Lecturer KVCET . Step 8: If the opcode read is WORD. the variable location counter is initialized with the operand value.length. Source Code: #include<stdio.h> #include<stdlib. Step 11: If the opcode read is BYTE.12 Aim: To write a C program to implement PASS ONE of a two pass assembler.h> void main() { char opcode[10].code[10].*fp4. Step 5: The source program is read line by line until the reach of opcode END. clrscr().DAT".DAT". fp1=fopen("INPUT.DAT".start. Step 9: If the opcode read is RESW. int locctr.

} fscanf(fp4.locctr).13 fscanf(fp1.mnemonic)."%s". fprintf(fp3."%s"."%s%s%s". locctr=start."\t%s\t%s\t%s\n"."RESW")==0) locctr+=(3*(atoi(operand))). else if(strcmp(opcode."%s%s%s"."WORD")==0) locctr+=3. while(strcmp(mnemonic. break.operand). if(strcmp(label.label.opcode. fscanf(fp4. else if(strcmp(opcode. } if(strcmp(opcode."END")!=0) { fprintf(fp3.mnemonic)==0) { locctr+=3.opcode."START")==0) { start=atoi(operand).mnemonic). } else locctr=0.label.PRABHU Lecturer KVCET .operand). while(strcmp(opcode. rewind(fp4). else if(strcmp(opcode.locctr)."**")!=0) fprintf(fp2."RESB")==0) locctr+=(atoi(operand)).opcode.operand). Prepared by : S.label. fscanf(fp1. if(strcmp(opcode."%s\t%d\n".label."END")!=0) { if(strcmp(opcode."BYTE")==0) ++locctr."%d\t".

opcode. } fprintf(fp3."%s%s%s".label. } INPUT : INPUT."%d\t%s\t%s\t%s\n". getch().DAT MAIN START 2000 BEGIN LDA NUM1 ** STA NUM2 ** LDCH CHAR1 ** STCH CHAR2 NUM1 WORD 5 NUM2 RESW 1 CHAR1 BYTE C'A' CHAR2 RESB 1 ** END BEGIN OPTAB. fclose(fp4). printf("\n Length of the program is %d". fclose(fp2)."%s\t%s\t%s\n". fclose(fp3). length=locctr-start.locctr. fscanf(fp1.operand).length).opcode.opcode.14 fprintf(fp3. fclose(fp1).DAT ADD ADDR SUB SUBR MUL MULR DIV DIVR LDA LDB LDX LDCH STA STB STX STCH 18 90 1C 94 20 98 24 9C 00 68 04 50 0C 78 10 54 Prepared by : S.operand).operand).label.label.PRABHU Lecturer KVCET .

DAT MAIN 2000 2003 2006 2009 2012 2015 2018 2019 2020 START 2000 BEGIN LDA NUM1 ** STA NUM2 ** LDCH CHAR1 ** STCH CHAR2 NUM1 WORD 5 NUM2 RESW 1 CHAR1 BYTE C'A' CHAR2 RESB 1 ** END BEGIN SYMTAB.PRABHU Lecturer KVCET . No : 3 Date: Prepared by : S.15 TIX 2C J 2C JSUB 48 RSUB 4C JEQ 30 JLT 38 JGT 34 START * END * OUTPUT: OUTPUT.DAT BEGIN 2000 NUM1 2012 NUM2 2015 CHAR1 CHAR2 2018 2019 RESULT: Thus the program is executed successfully and the result is obtained. IMPLEMENTATION PROGRAM FOR PASS 2 OF TWO PASS ASSEMBLER Ex.

*fp2.16 Aim: To implement of pass two of two pass assembler.h> void main() { char symbol[20].DAT". operand into a file if it is not match anything then write address.PRABHU Lecturer KVCET . operand into a file Else if opcode is WORD then write address."w").operand). opcode. operand into a file Else if opcode is “RESB” or “RESW” write address.label.loc.opcode.DAT". label.code[20].flag. fp2=fopen("TWOOUT1. clrscr(). Algorithm: Step 1:Start the program Step 2:Initialize all the variables Step 3:Open a file Step 4:Read the content of the file Step 5:If opcode is BYTE then write address."%s%s%s". fp1=fopen("OUT1.add[20]."r").operand[20]."r"). label. operand into a file Step 6:Finally terminate the of pass two of pass two assembler. FILE *fp1. int locctr."\t%s\t%s\t%s\n".h> #include<string.flag1.h> #include<stdlib. fp3=fopen("OPTAB1.*fp4.operand).DAT".opcode[20].mnemonic[20].DAT". opcode.label.h> #include<conio. objectcode[20].label[20]. fp4=fopen("SYMTAB1."r")."START")==0) { fprintf(fp2. opcode. if(strcmp(opcode.character.*fp3. Prepared by : S. SOURCE CODE: #include<stdio. fscanf(fp1. label.opcode. label. opcode.

"*")!=0)) { flag=1.label.mnemonic."WORD")==0) { if(operand[0]=='C'||operand[0]=='X') { character=operand[2]. } } if(flag1==1) { itoa(loc.add). } if(flag==1) { flag1=0. rewind(fp3).operand).add."%d%s%s%s"."END")!=0) { if((strcmp(opcode.10). itoa(character.17 fscanf(fp1.add. } else { itoa(atoi(operand). if(strcmp(symbol."%s%d".&locctr.16).strcat(code.add)."%s%s". Prepared by : S.operand)==0) { flag1=1. } } else if(strcmp(opcode. strcpy(objectcode."END")!=0) { flag=0. strcpy(objectcode. } fscanf(fp3. } while(strcmp(opcode. rewind(fp4). break. while(!feof(fp4)) { fscanf(fp4.mnemonic."BYTE")==0||strcmp(opcode.10). while(strcmp(mnemonic.code).add. break.&loc)."%s%s".code).PRABHU Lecturer KVCET . fscanf(fp3.mnemonic)==0)&&(strcmp(code.symbol. strcpy(objectcode.add)).opcode.

label.label.DAT BEGIN 2000 NUM1 2012 NUM2 2015 CHAR1 2018 CHAR2 2019 2000 LDA STA LDCH STCH WORD 5 RESW BYTE RESB BEGIN NUM1 NUM2 CHAR1 CHAR2 1 C'A' 1 OPTAB1.label. fclose(fp1). } INPUT OUTPUT: OUT1."%d\t%s\t%s\t%s\t%s\n".PRABHU Lecturer KVCET .operand).operand). fclose(fp2).locctr. fscanf(fp1."\t%s\t%s\t%s\n".objectcode).DAT ADD 18 ADDR 90 SUB 1C SUBR 94 MUL 20 Prepared by : S.18 } } else strcpy(objectcode. fclose(fp3). printf("\n Output").&locctr."%d%s%s%s"."\0").DAT MAIN START 2000 BEGIN 2003 ** 2006 ** 2009 ** 2012 NUM1 2015 NUM2 2018 CHAR1 2019 CHAR2 2020 ** END SYMTAB1.operand.opcode. fprintf(fp2. fclose(fp4). getch(). } fprintf(fp2.opcode.opcode.

DAT MAIN 2000 2003 2006 2009 2012 2015 2018 2019 ** START 2000 BEGIN LDA NUM1 002012 ** STA NUM2 0C2015 ** LDCH CHAR1 502018 ** STCH CHAR2 542019 NUM1 WORD 5 5 NUM2 RESW 1 CHAR1 BYTE C'A' CHAR2 RESB 1 END BEGIN 41 RESULT: Thus the program is executed successfully and the result is obtained.19 MULR 98 DIV 24 DIVR 9C LDA 00 LDB 68 LDX 04 LDCH 50 STA 0C STB 78 STX 10 STCH 54 TIX 2C J 2C JSUB 48 RSUB 4C JEQ 30 JLT 38 JGT 34 START END * * OUTOBJ.PRABHU Lecturer KVCET . No : 4 Date: Prepared by : S. IMPLEMENTATION OF SINGLE PASS ASSEMBLER Ex.

Initialize all the variables Step 3. Step 6: Finally terminate the of pass two of pass two assembler.opcode.DAT".operand).locctr.Open a files for read and write.PRABHU Lecturer KVCET .h> #include<conio. int f.label[10].h> #include<string. void main() { int f1. fp3=fopen("SPOUTEX4."r"). fp1=fopen("INPUTEX4."r").mnemonic[10]. struct symtab { char sym[10]. int loc.label.mnemcode[10]. Algorithm: Step 1: Start the program Step 2.flag."w").operand[10]. Prepared by : S."%s%s%s". struct input table[MAX]. fscanf(fp1.*fp3. }.table[i]. FILE *fp1. fp2=fopen("OPTABEX4.ref. Step 4: Read the content from the input file. Write the object code with address to the output file.h> #define MAX 20 struct input { char opcode[10].table[i]."START")==0) { locctr=atoi(table[i].h> #include<stdlib.20 Aim: To implement of Single pass an assembler.x.operand). SOURCE CODE: #include<stdio.DAT".table[i]. }. struct symtab symtbl[MAX].j=1.code[10].i=1.DAT".opcode.*fp2. Step 5. if(strcmp(table[i].val. char add[10]. clrscr().

21 i++."END")!=0) { if(strcmp(table[i].PRABHU Lecturer KVCET . if(symtbl[x]. Prepared by : S.ref].sym. } } rewind(fp2).opcode.val.code. } } if(flag!=1) { strcpy(symtbl[j].x++) { flag=0.sym."**")!=0) { for(x=1.label). } } if(f1==0) { strcpy(symtbl[j].opcode.val=locctr.code)==0) { strcpy(table[i]. locctr+=3.mnemcode). if((strcmp(symtbl[x]. j++.sym."%s%s%s".f=0.x++) { f1=0. while(strcmp(table[i].x<=j. break.loc=symtbl[x].x<j.label)==0)&&(symtbl[x]. for(x=1.operand).operand). if(strcmp(table[i].table[i].mnemcode). fscanf(fp1.symtbl[x]. symtbl[x].val=locctr. while(strcmp(code.f=0.table[i].loc=locctr.opcode.mnemonic. fscanf(fp2.table[i]. f1=1.operand.table[i]. symtbl[j]. break.f==0) table[i].table[i].table[i]. symtbl[j]. table[symtbl[x]."%s%s".sym)==0) { flag=1.label.label. } else locctr=0."END")!=0) { if(strcmp(table[i].f==1)) { symtbl[x].

} INPUT OUTPUT: INPUTE.strcat(table[x].sym.symtbl[x].x++) fprintf(fp3. } if(strcmp(table[i]. strcpy(table[i]. getch().operand)."%s\t%s\t%s\t %s\n". } } fscanf(fp2.label. } else if(strcmp(table[i].f=1."WORD")==0) { locctr+=3.operand))). } else if(strcmp(table[i].10))).table[x].loc=atoi('\0'). j++. for(x=1. strcpy(table[i].table[x]. } i++.ref=i.opcode. if((table[i].loc=atoi(table[i]. table[i].table[x].operand[0]=='C')||(table[i]. strcpy(table[i].a dd. symtbl[j].loc=atoi('\0').operand[0]=='X')) table[i].x++) printf("%s\t%d\n".operand[2].opcode.'\0').x<=j.table[i]. else table[i].symtbl[x].operand)). } else if(strcmp(table[i].PRABHU Lecturer KVCET .opcode.table[i].opcode.loc=locctr.code."RESW")==0) { locctr+=(3*(atoi(table[i].mnemonic.val)."%s%s"."%s%s%s".label.DAT MAIN BEGIN START JSUB 2000 LOOP1 Prepared by : S.'\0')."BYTE")==0) { ++locctr. table[i].'\0').opcode.22 symtbl[j].itoa(table[x].mnemonic. } for(x=1."RESB")==0) { locctr+=(atoi(table[i].table[i].loc.mnemonic.mnemcode).x<=i. fscanf(fp1.operand.operand).opcode.loc=(int)table[i]. table[i].mnemonic.

DAT MAIN BEGIN ** NUM1 NUM2 CHAR1 CHAR2 LOOP1 LDA ** STA LOOP2 LDCH ** STCH ** END OPTAB.PRABHU Lecturer KVCET START JSUB JSUB WORD RESW BYTE C'A' RESB 4 NUM1 NUM2 CHAR1 CHAR2 BEGIN 0 2000 0 LOOP1 482023 LOOP2 482029 5 5 3 0 65 0 002006 0C2009 502018 542019 JSUB WORD RESW BYTE RESB LDA STA LDCH STCH END LOOP2 5 3 C'A' 4 NUM1 NUM2 CHAR1 CHAR2 BEGIN .DAT ADD ADDR SUB SUBR MUL MULR DIV DIVR LDA LDB LDX LDCH STA STB STX STCH TIX J JSUB RSUB JEQ JLT JGT 18 90 1C 94 20 98 24 9C 00 68 04 50 0C 78 10 54 2C 2C 48 4C 30 38 34 Prepared by : S.23 ** NUM1 NUM2 CHAR1 CHAR2 LOOP1 ** LOOP2 ** ** SPOUT.

IMPLEMENTATION OF MACROPROCESSOR Ex.24 START * END * RESULT: Thus the program is executed successfully and the result is obtained. Prepared by : S. No : 5 Date: Aim : To perform the Simple Macro processor using c program .PRABHU Lecturer KVCET .

flag. clrscr(). fp2=fopen("MACOUT.PRABHU Lecturer KVCET .25 Algorithm: Step 1: Start the macro processor program Step 2: Include the necessary header files and variable Step 3: Open the three files Step 4: Step 5: Step 6: Step 7: Step 8: Step 9: Read the variable until the opcode is not is equal to zero Then check if the opcode is equal to Macro Then Copy macro name=label Get the variable label .operand In these if condition perform the while loop until opcode is not equal to MEND Copy the variable else if opcode is equal to macro name Perform the for loop from 0 to length write label. opcode. operand in to a file Step 11: Finally terminate the program Step 10: else if it is not match SOURCE CODE: #include<stdio. FILE *fp1.h> void main() { int n.opcode . fscanf(fp1. rewind(fp1). n=0.DAT".DAT"."%s%s%s".*fp2.oper[20].*deftab.i.h> #include<stdlib.oper).ilab.h> #include<conio."r"). char ilab[20].iopd[20].iopd. while(!feof(fp1)) Prepared by : S.h> #include<string. fp1=fopen("MACIN."w").namtab[20][20].

n++.PRABHU Lecturer KVCET .ilab."r")."%s\t%s\t%s\n".iopd.ilab."%s%s%s". deftab=fopen(namtab[n].iopd."%s%s%s".iopd. } break.iopd.oper). Prepared by : S.oper).namtab[i])==0) { flag=1.i++) { if(strcmp(iopd. fscanf(deftab."MEND")!=0) { fprintf(deftab.oper). for(i=0.iopd. fscanf(fp1."%s\t%s\t%s\n"."%s\t%s\t%s\n"."w"). deftab=fopen(namtab[i].oper).ilab.ilab).oper).iopd.ilab. while(strcmp(iopd. } else { flag=0.oper).ilab. while(!feof(deftab)) { fprintf(fp2.i<n.26 { if(strcmp(iopd.ilab.iopd. fscanf(deftab.ilab."MACRO")==0) { strcpy(namtab[n].oper)."%s%s%s"."%s%s%s". } fclose(deftab). fscanf(fp1. } } if(flag==0) fprintf(fp2.

iopd.27 } fscanf(fp1."%s%s%s". } getch().oper).PRABHU Lecturer KVCET .DAT ** ** START 1000 LDA N1 Prepared by : S. } INPUT OUTPUT: MACIN.ilab.DAT M1 ** ** ** ** M2 ** ** ** ** M3 ** ** ** ** ** ** ** ** ** MACRO LDA ADD STA N1 N2 N3 ** ** MEND ** MACRO LDA SUB STA N1 N2 N4 ** MEND ** MACRO LDA MUL STA N1 N2 N5 MEND ** START 1000 M3 M2 M1 END ** ** ** ** MACOUT.

PRABHU Lecturer KVCET .28 ** ** ** ** ** ** ** ** MUL STA LDA SUB STA LDA ADD STA N2 N5 N1 N2 N4 N1 N2 N3 RESULT: Thus the program is executed successfully and the result is obtained. No : 6 Date: Prepared by : S. IMPLEMENTATION OF RELOCATING LOADER Ex.

len. struct objectcode code[500].in[20]."r"). }. fp2=fopen("RLOADOT1.textloc.inc=0. else Add the addr and star value. void main() { char input[20]. printf("\n Enter the location to be loaded:"). and input Step 8: Else if the character is T Step 9: Get the variable address and bitmask And perform the for loop for starting zero to up to len Get the opcode ."w"). fp1=fopen("RLOADIN.h> #include<string.DAT".h> struct objectcode { int locctr. char byte[5].h> #include<conio. Finally terminate the program. Prepared by : S.29 Aim: To perform the Relocation Loader using c program.nloc. int loc. int i. SOURCE CODE #include<stdio. length.tloc=0.PRABHU Lecturer KVCET .num=0.*fp2.DAT".j.tlen=0.reloc. FILE *fp1.k.addr and assign relocbit to bitmask If relocabit is zero Then Assign actualadd to addr. Algorithm: Step 1: Start the program Step 2: Include the necessary header file and variable Step 3: Open the two file for read and write. clrscr().h> #include<stdlib. Step 4: Read the content Step 5: Using while loop perform the loop until character is not equal to E Step 6: If the character is H Step 7: Get the variable add.rloc[20].newloc[20].

if(num>1) { code[inc].locctr=loc."xx").i++) code[inc].byte. } fscanf(fp1."H")==0) { for(i=0."%s". loc++. reloc=atoi(in)+atoi(rloc).rloc).30 scanf("%s".&textloc). i++.PRABHU Lecturer KVCET . else fscanf(fp1. Prepared by : S. code[inc++]. textloc=textloc+atoi(rloc).i<4. } } in[j]='\0'.i++) { strcpy(code[inc]. j=0. for(i=0.10). for(i=0."%d". } } tloc=loc+atoi(rloc).&tlen).i<textloc-(tloc+tlen). fscanf(fp1. tloc=textloc. loc=atoi(rloc). itoa(reloc.i++) { if(i==1) fscanf(fp1.input).i<2.input). while(strcmp(input.newloc. inc++."E")!=0) { if(strcmp(input. j++. if(strcmp(input."%d".byte[num++]=input[i]. if(i==2) { while(i!=len) { in[j]=input[i]."%d".locctr=loc++."%s".&loc)."T")==0) { fscanf(fp1. } else { len=strlen(input).

loc++."%d\t%s\n".k<j. fclose(fp1). inc++.31 num=0.PRABHU Lecturer KVCET . fclose(fp2)."%s".DAT H T T E COPY 000000 20 000000 12 000012 0C0015 500018 540019 000016 06 000005 000041 000000 RLOADOT1.locctr=loc. if(num>1) { code[inc]. num=0.input). } for(k=0.byte). } } } fscanf(fp1.DAT 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 00 20 12 0C 20 15 50 20 18 54 20 19 xx xx xx xx 00 Prepared by : S. } SAMPLE INPUT OUTPUT: RLOADIN.code[i].i<inc. } for(i=0.locctr.k++) { code[inc].i++) fprintf(fp2.code[i].byte[num++]=newloc[k]. getch().

32 2017 2018 2019 2020 2021 20 05 00 20 41 RESULT: Thus the program is executed successfully and the result is obtained. No : 7 Date: Prepared by : S. IMPLEMENTATION OF ABSOLUTE LOADER Ex.PRABHU Lecturer KVCET .

Step 7:Finally terminate the program SOURCE CODE: # include <stdio. Step 2: Assign the required variable Step 3:Open the files for read and write Step 4:Read the content Step 5:Using while loop perform the loop until character is not equal to E Step 6:Then compare the character is equal to H then read start from input file Like that get length.*fp2.h> void main() { char input[10]. Prepared by : S. fp2=fopen("output."%d".input)."w")."E")!=0) { if(strcmp(input. and input Else if the character is T Then perform the frprintf in fp1 for input file Else if it is not H or T Then perform the fprintf in fp2 for output file for ."%s".33 Aim: To implement Absolute loader using c. fscanf(fp1.length. while(strcmp(input.&start).h> # include <conio. fp1=fopen("input. int start. FILE *fp1."r").address. Algorithm: Step 1: Start the program."H")==0) { fscanf(fp1.PRABHU Lecturer KVCET .h> # include <string. clrscr().dat".dat".

"%d\t%c%c\n".input[5]).input[0]."%s". fclose(fp2).DAT file: H 1000 232 T 1000 142033 483039 102036 T 2000 298300 230000 282030 302015 E OUTPUT: OUTPUT.PRABHU Lecturer KVCET ."%s". fprintf(fp2."T")==0) { fscanf(fp1. } else if(strcmp(input."%d"."%s". fprintf(fp2. getch().&address)."%d\t%c%c\n".input[4].(address+1).DAT file 1000 14 1001 20 Prepared by : S.(address+2). fscanf(fp1.input)."%d".input[1]). fprintf(fp2. } else { fprintf(fp2. fscanf(fp1. fscanf(fp1. fscanf(fp1.(address+1).(address+2). fprintf(fp2.&length).input[3]).input[2]. } INPUT: INPUT. } } fclose(fp1).34 fscanf(fp1.input[2]."%d\t%c%c\n".input[3]). printf("FINISHED")."%d\t%c%c\n".input[0].input). address+=3. address+=3.address.input). fprintf(fp2.input).input[5])."%s".address.input[1])."%d\t%c%c\n".input[4]."%d\t%c%c\n".

No : 8 Date: Prepared by : S.35 1002 33 1003 48 1004 30 1005 39 1006 10 1007 20 1008 36 2000 29 2001 83 2002 00 2003 23 2004 00 2005 00 2006 28 2007 20 2008 30 2009 30 2010 20 2011 15 RESULT: Thus the program is executed successfully and the result is obtained.PRABHU Lecturer KVCET . IMPLEMENTATION OF PASS1 OF LINKING LOADER Ex.

DAT”.”r”).*fp2. FILE *fp1.h> #include<stdlib.PRABHU Lecturer KVCET . char symname[10]. void main() { char input[10]. struct estab table[10].start. int i.h> #include<string.h> #include<conio.count=0. }. int add. added along with the corresponding control section addredd until an end record is reached.h> struct estab { char csect[10].length. fp1=fopen(“LINKIN1.36 AIM : To implement pass one of linking loader using c.loc. Prepared by : S. 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 input SOURCE CODE: #include<stdio. int length. clrscr(). Algorithm: Step 1: Enter the location where the 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 From the details of the header read store the control section length in variable 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 symbol table along with its address.

input). scanf(“%x”.&start).csect.”%s”.input).”%x”. start=start+length. } for(i=0. } if(strcmp(input.input.”T”)==0) while(strcmp(input.”**”).”H”)==0) { fscanf(fp1. fclose(fp2).”w”).PRABHU Lecturer KVCET .”%s%x”. table[i].”R”)!=0) { strcpy(table[count].table[i].”%s”.”D”)==0) { fscanf(fp1. getch().&length).DAT”. } Prepared by : S.input).input). strcpy(table[count]. strcpy(table[count].input. while(strcmp(input.length=0.”**”).input). fscanf(fp1.i++) fprintf(fp2.table[i]. fclose(fp1). } if(strcmp(input.add.”%s”. table[count++]. strcpy (table[count]. while(!feof(fp1)) { fscanf(fp1.&loc).”T”)!=0) fscanf(fp1.”%s”. table[count].”%s”. fscanf(fp1.”%s”. fscanf(fp1.”CSECT\tSYMNAME\tADDRESS\tLENGTH\n”).”%s\t%s\t%x\t%x\n”.37 fp2=fopen(“LINKOUT1. fprintf(fp2. rewind(fp1).symname.input). table[count].symname. if(strcmp(input.&loc).add=loc+start.input).length=length.i<count. table[count++].”E”)!=0) fscanf(fp1.input).”%s%x”. printf(“\n Enter the location where the program has to be loaded:”). } while(strcmp(input.length).symname.”%s”.csect. fscanf(fp1. fscanf(fp1.table[i].add=atoi(input)+start.csect.input).

DAT CSECT SYMNAME PROGA ** ** LISTA 2040 ** ENDA 2054 PROGB ** ADDRESS 2000 70 0 0 2070 88 LENGTH Prepared by : S.PRABHU Lecturer KVCET .DAT H D R T T M M M E H D R T T M M M M M E H D R T T M M M M M M E PROGA 000000 000070 LISTA 000040 ENDA 000054 LISTB ENDAB LISTC ENDC 000020 10 03201D77100004 150012 000054 16 100014 15100006 000021 100014 000024 05 +LISTB 000054 06 +LISTC 000064 06 +LISTB 000000 PROGB 000000 000088 LISTB 000060 ENDB 000070 LISTA ENDA LISTC ENDC 000036 11 03100000 772027 000070 18 100000 05100006 000037 05 +LISTA 000044 05 +ENDA 000070 06 +ENDA 000074 06 +ENDC 000082 06 +ENDA 000000 PROGC 000000 000057 LISTC 000030 ENDC 000042 LISTA ENDA LISTB ENDB 000018 12 03100000 77100004 000042 15 100030 100008 000019 05 +LISTA 000023 05 +LISTB 000027 05 +ENDA 000048 06 +LISTA 000051 06 +ENDA 000054 06 +LISTB 000000 05100000 05100020 0510030 05100000 100011 100000 LINKOUT1.38 SAMPLE INPUT OUTPUT: LINKIN1.

PASS2 OF LINKING LOADER Ex.PRABHU Lecturer KVCET .39 ** LISTB ** ENDB PROGC ** LISTC ** ENDC 20d0 20e0 ** 2128 213a 0 0 20f8 0 0 57 RESULT: Thus the program is executed successfully and the result is obtained. No : 9 Date: Prepared by : S.

and move the object code from the record to the memory location control sections address plus the specified address in the text record.h> #include<string. store the control section length in a variable Do the following process until an ‘end’ record is reached i. If the sequent records read is modification record ‘M’ then search for the modifying symbol name in the external symbol table created by pass 1 if it is found the add. CSADR. Algorithm: Step 1: Step 2: Step 3: Step 4: Assign the control section address in a variable. #include<stdio. Read the Header record From the information available in the header record read.PRABHU Lecturer KVCET . }. or subtract the corresponding symbol address found with the value starting at the location (CSADD plus the specified address in the modification record).h> #include<stdlib. Prepared by : S. If the subsequent records read is a text record ‘T’.h> #include<conio.40 Aim: To implement pass one of linking loader using c. int locctr. struct objectcode code[500]. and if the object code is in character form convert it into machine representation. ii. and repeat the entire process until there are no more input. Step 5: Add the control section length to the current control section address to find the address of the next control section.h> struct objectcode { char byte[15].

scanf("%d"."%d".oper).start."T")!=0). code[inc++]. char input[20].tlen=0."%d". int len.input)."%s".tloc."%s".DAT". fp2=fopen("INP4EX9. fscanf(fp1. tloc=loc.locctr=loc++. while(!feof(fp1)) { if(strcmp(input.*fp3. fscanf(fp1. fp1=fopen("INP3EX9. fscanf(fp1.newval.inc=0. fscanf(fp1.maddr.41 void main() { int loc."%s".&length).&value).symbol[20].input).x.length. } fscanf(fp1.PRABHU Lecturer KVCET .DAT".newloc[20].byte."%d".&start)."r")."%s".k.value."T")==0) { fscanf(fp1.modval[20]."H")==0) { fscanf(fp1.input).DAT". fscanf(fp1."w"). loc=atoi(input)+start. tloc=textloc. } while(strcmp(input."D")==0) { do { fscanf(fp1.num=0.i<textloc-(tloc+tlen).i."xx"). textloc=textloc+start."M")==0) { fscanf(fp1. printf("\n Enter the location to be loaded:"). } if(strcmp(input.input). clrscr().input)."%s". maddr=atoi(input)+start. } if(strcmp(input.i++) { strcpy(code[inc]."%s". fp3=fopen("PAS2OUT. fscanf(fp1.input).&textloc).reloc. FILE *fp1.in[20].*fp2.oper[5]. for(i=0.input)."%d". Prepared by : S. fscanf(fp1.&tlen).addr. } if(strcmp(input.j.textloc."%s"."%s"."r").

for(i=0. else newval=addr-value. } itoa(newval.byte[num++]=input[i].x++) { if(code[x]."%s". } Prepared by : S. } } fscanf(fp1.input).x<inc. } for(x=0.k++) { code[x]. start=start+length.42 fscanf(fp1.modval.byte[num++]=modval[k].k<j. while(!feof(fp2)) { if(strcmp(input. } fscanf(fp2. j=strlen(modval). num=0."%s".&addr). j=0."E")==0) { fscanf(fp1. rewind(fp2). j++.symbol)==0) { if(strcmp(oper. } else { len=strlen(input). if(num>1) { x++.i++) code[inc].PRABHU Lecturer KVCET .symbol.locctr==maddr) break."+")==0) newval=addr+value.input).&addr).10)."%s%d".input). fscanf(fp2. if(i==2) { while(i!=len) { in[j]=input[i].symbol."%s%d".i<2."%s". } if(strcmp(input. for(k=0. i++.

} for(k=0. inc++."\n%d\t%s". loc++.byte[num++]=newloc[k]. fclose(fp1).i<inc. loc++.i++) fprintf(fp3. num=0.newloc.byte).DAT: H D R T M E H D R T M E PROGA LISTA LISTB 000000 000004 000000 PROGB LISTB LISTA 000000 000001 000000 000000 000012 000006 ENDA 000007 ENDB 12 000016 100004 05 + LISTB 000000 000010 ENDA 12 05 000012 ENDB 000011 030005 LISTA 770027 050016 000012 150014 180012 INP4EX9. if(num>1) { code[inc].43 } in[j]='\0'.input). inc++.k<j.PRABHU Lecturer KVCET . getch().locctr=loc.k++) { code[inc].locctr=loc.10).DAT: LISTA 2006 ENDA 2007 Prepared by : S. } SAMPLE INPUT OUTPUT: INP3EX9. num=0. fclose(fp2). if(num>1) { code[inc]."%s". reloc=atoi(in)+start. } } } fscanf(fp1. fclose(fp3).locctr.code[i]. } for(i=0. itoa(reloc.code[i].

DAT: 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 00 20 16 10 20 13 15 20 14 18 20 12 03 20 01 77 20 39 05 20 28 00 20 24 RESULT: Thus the program is executed successfully and the result is obtained.PRABHU Lecturer KVCET .44 LISTB 2008 ENDB 2010 PAS2OUT. SIMPLE TEXT EDITOR Prepared by : S.

Algorithm: Step 1: Provide blank screen for the user to type the document Step 2: Instruct the user to enter the text using the editor Step 3: Display the entered character on the screen Step 4: Characters displayed on the screen are stored in a character array Step 5: Specify if the content has to be saved or not.h> #include<conio. Step 6: If SAVE option is selected a) Get the data file name from the user b) Open the file in write mode c) Move the content of the character array to the file and close the file Step 7: If OPEN option is selected a) Open the data file in the read mode b) Read one character at a time from the file and move it to the character array c) Repeat the above steps utile the end of file is reached d) Display the characters from the character array on the screen Step 8: Finally close the text editor and exit SOURCE CODE: #include<stdio.h> #include<string.h> #include<stdlib.h> Prepared by : S. No : 10 Date: Aim: To create a simple text editor using C.PRABHU Lecturer KVCET .45 Ex.

46 FILE *fp1. } } fclose(fp1).filename[12]."). printf("\n*********************************************************").filename). if(fp1==NULL) { fp1=fopen(filename.buff).. } } void openfile() Prepared by : S."w"). printf("\n\tFile not saved. printf("\n*********************************************************"). } else { printf("\n Enter new filename:"). char buff[2000]. void newfile() { char c. ch=getch(). goto loop."r"). loop: scanf("%s".overwrite?(y/n)")."%s". printf("\nType the text and press TAB key to terminate\n"). } else { printf("\nFile already exists. if(ch=='Y'||ch=='y') { fp1=fopen(filename. printf("\n warning!. clrscr().buff). cnt=0. printf("\n NEW FILE CREATION" ). ch=getch().PRABHU Lecturer KVCET . fprintf(fp1."w"). while((c=getchar())!='\t') buff[cnt++]=c. if(ch=='Y'||ch=='y') { printf("\nEnter the file name:"). fp1=fopen(filename. buff[cnt]='\0'.."%s". int cnt.ch. fprintf(fp1.save?(y/n)").

47 { char c. printf("\nEnter correct filename :"). case'E': case'e':exit(1). clrscr(). printf("\n FILE OPEN AND DISPLAY "). ch=getch(). putchar(c). while(!feof(fp1)) { c=getc(fp1). switch(ch) { case'N': case'n':newfile(). loop: scanf("%s". case'O': case'o':openfile(). } } Prepared by : S. printf("\n******************************************************").filename).PRABHU Lecturer KVCET . break.. printf("\n************************************************************. } fclose(fp1). printf("\n************************************************************. break. break. printf("\n******************************************************"). } getch(). printf("\n TEXT EDITOR "). } void main() { char ch."r"). printf("\n\tNew(N)\t\tOpen(O)\t\tExit(E)"). goto loop. if(fp1==NULL) { printf("\nFile does not exist. fp1=fopen(filename. } else { printf("\nFile content:\n"). printf("\nEnter the filename:")."). while(1) { clrscr(). printf("\n\nEnter the option:").

PRABHU Lecturer KVCET .file not saved.48 } OUTPUT: ***************************************************************** TEXT EDITOR ***************************************************************** new(N) enter the option:N type text and press tab to terminate AROCKIANEOPOLEANKLUMAR warning!..save(y/n) enter the file name:CSE ***************************************************************** TEXT EDITOR ***************************************************************** new(N) enter the option:O Enter the file name:CSE Filecontenet: AROCKIANEOPOLEANKUMAR ***************************************************************** TEXT EDITOR ***************************************************************** new(N) open(O) exit(E) open(O) exit(E) open(O) exit(E) Prepared by : S..

Prepared by : S.49 enter the option:E RESULT: Thus the program is executed successfully and the result is obtained.PRABHU Lecturer KVCET .