You are on page 1of 52

1.

IMPLEMENTATION OF A SYMBOL TABLE WITH FUNCTIONS TO CREATE, INSERT, MODIFY, SEARCH AND DISPLAY AIM: To write a C program for the implementation of symbol table with functions to create, insert, modify, search and display. Algorithm: Create a structure having member variable and values.

1. 2. 3. 4. 5. 6. 7. 8.

Display the options and get choice from user. If choice 1 call create. If choice 2 call insert. If choice 3 call modify If choice 4 call search. If choice 5 call display. Exit. Create()

1. Do till I less than 3. 2. Get input from the user for variables and values. 1. Insert() 3. Get number of records to insert. 10.1.1 Do till i is less than sum of I and number of recors to insert. 10.2 Get input for symbol name and value 1. Modify() Get record or symbol name to modify. If it is equal to any symbol name then print record found else print record not fund. Get the value for corresponding symbol name. 1. Search() Get the symbol name to modify. If it is present in table then print record found else print record not found.

4. 5. 6. 7. 8.

1. Display() 9. Do till k is less than total number of records. 10. Print the symbol names and values..

SOURCE CODE:

#include<stdio.h> #include<conio.h> #include<alloc.h> #include<string.h> #include<stdlib.h> #define NULL 0 int size=0; void Insert(); void Display(); void Delete(); int Search(char lab[]);void Modify(); struct SymbTab { char label[10],symbol[10]; int addr; struct SymbTab *next;}; struct SymbTab *first,*last; void main() { int op,y; char la[10]; clrscr(); do {

printf(\n\tSYMBOL TABLE IMPLEMENTATION\n); printf(\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.MODIFY \n\t6.END\n); printf(\n\tEnter your option : ); scanf(%d,&op); switch(op) { case 1: Insert(); break; case 2: Display(); break; case 3: Delete(); break; case 4: printf(\n\tEnter the label to be searched : ); scanf(%s,la); y=Search(la); printf(\n\tSearch Result:); if(y==1) printf(\n\tThe label is present in the symbol table\n); else printf(\n\tThe label is not present in the symbol table\n); break; case 5: Modify(); break; case 6: exit(0);

} }while(op<6); getch(); } void Insert() { int n; char l[10]; printf(\n\tEnter the label : ); scanf(%s,l); n=Search(l); if(n==1) printf(\n\tThe label exists already in the symbol table\n\tDuplicate cant be inserted); else { struct SymbTab *p; p=malloc(sizeof(struct SymbTab)); strcpy(p->label,l); printf(\n\tEnter the symbol : ); scanf(%s,p->symbol); printf(\n\tEnter the address : ); scanf(%d,&p->addr); p->next=NULL; if(size==0) { first=p; last=p; } else {

last->next=p; last=p; } size++; } printf(\n\tLabel inserted\n); } void Display() { int i; struct SymbTab *p; p=first; printf(\n\tLABEL\t\tSYMBOL\t\tADDRESS\n); for(i=0;i<size;i++) { printf(\t%s\t\t%s\t\t%d\n,p->label,p->symbol,p->addr); p=p->next; } } int Search(char lab[]) { int i,flag=0; struct SymbTab *p; p=first; for(i=0;i<size;i++) { if(strcmp(p->label,lab)==0) flag=1; p=p->next; } return flag;

} void Modify() { char l[10],nl[10]; int add,choice,i,s; struct SymbTab *p; p=first; printf(\n\tWhat do you want to modify?\n); printf(\n\t1.Only the label\n\t2.Only the address\n\t3.Both the label and address\n); printf(\tEnter your choice : ); scanf(%d,&choice); switch(choice) { case 1: printf(\n\tEnter the old label : ); scanf(%s,l); s=Search(l); if(s==0) printf(\n\tLabel not found\n); else { printf(\n\tEnter the new label : ); scanf(%s,nl); for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) strcpy(p->label,nl); p=p->next; } printf(\n\tAfter Modification:\n);

Display(); } break; case 2: printf(\n\tEnter the label where the address is to be modified : ); scanf(%s,l); s=Search(l); if(s==0) printf(\n\tLabel not found\n); else { printf(\n\tEnter the new address : ); scanf(%d,&add); for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) p->addr=add; p=p->next; } printf(\n\tAfter Modification:\n); Display(); } break; case 3: printf(\n\tEnter the old label : ); scanf(%s,l); s=Search(l); if(s==0) printf(\n\tLabel not found\n); else {

printf(\n\tEnter the new label : ); scanf(%s,nl); printf(\n\tEnter the new address : ); scanf(%d,&add); for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) { strcpy(p->label,nl); p->addr=add; } p=p->next; } printf(\n\tAfter Modification:\n); Display(); } break; } } void Delete() { int a; char l[10]; struct SymbTab *p,*q; p=first; printf(\n\tEnter the label to be deleted : ); scanf(%s,l); a=Search(l); if(a==0) printf(\n\tLabel not found\n); else

{ if(strcmp(first->label,l)==0) first=first->next; else if(strcmp(last->label,l)==0) { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; } p->next=NULL; last=p; } else { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; } p->next=q->next; } size; printf(\n\tAfter Deletion:\n); Display(); } }

2. IMPLEMENTATION OF PASS ONE OF A TWO PASS ASSEMBLER AIM: To write a "C" program for the implementation of pass one of a two pass. ALGORITHM: 1. Start the assembly process by processing the assembly program statements. 2. Location counter is initialized to the starting address if OPCODE = "START" else if it is initialized to 0.It is incremented depending upon the size of next consecutive instruction. 3. If there is any symbol present in lable field, location counter is entered into symbol table.The opcode table must be present in the memory during pass1. 4. Search the OPTAB.If it is found add 3 to locctr. 5. If not found in OPTAB, update the location value by checking whether the opcode is WORD,RESW,RESB or BYTE. 6. Write the program line with locctr value to intermediate file. 7. Read the next input line. 8. Steps3 to 7 is carried out till the end statement is reached. 9. Finally save the value of 1 locctr-starting address) and program length. 10. Stop.

SOURCE CODE: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char opcode[10],operand[10],label[10],code[10][10],ch; char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"}; int locctr,start,len,i=0,j=0; FILE *fp1,*fp2,*fp3; clrscr(); fp1=fopen("INPUT.DAT","r"); fp2=fopen("SYMTAB.DAT","w"); fp3=fopen("OUT.DAT","w"); fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) {

start=atoi(operand); locctr=start; fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); } else locctr=0; while(strcmp(opcode,"END")!=0) { fprintf(fp3,"%d",locctr); if(strcmp(label,"**")!=0) fprintf(fp2,"%s\t%d\n",label,locctr); strcpy(code[i],mnemonic[j]); while(strcmp(mnemonic[j],"END")!=0) { if(strcmp(opcode,mnemonic[j])==0) { locctr+=3; break; } strcpy(code[i],mnemonic[j]); j++; } if(strcmp(opcode,"WORD")==0) locctr+=3; else if(strcmp(opcode,"RESW")==0) locctr+=(3*(atoi(operand))); else if(strcmp(opcode,"RESB")==0) locctr+=(atoi(operand)); else if(strcmp(opcode,"BYTE")==0) ++locctr; fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); } fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand); fcloseall(); printf("\n\nThe contents of Input Table :\n\n"); fp1=fopen("INPUT.DAT","r"); ch=fgetc(fp1); while(ch!=EOF)

{ printf("%c",ch); ch=fgetc(fp1); } printf("\n\nThe contents of Output Table :\n\n\t"); fp3=fopen("OUT.DAT","r"); ch=fgetc(fp3); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp3); } len=locctr-start; printf("\nThe length of the program is %d.\n\n",len); printf("\n\nThe contents of Symbol Table :\n\n"); fp2=fopen("SYMTAB.DAT","r"); ch=fgetc(fp2); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp2); } fcloseall(); getch(); }

INPUT.DAT ** START 2000 ** LDA FIVE ** STA ALPHA ** LDCH CHARZ ** STCH C1 ALPHA RESW 1 FIVE WORD 5 CHARZ BYTE C'Z' C1 RESB 1 ** END **

3. IMPLEMENTATION OF PASS TWO OF A TWO PASS ASSEMBLER AIM: To write a "C" program for the implementation of pass two of a two pass assembler. ALGORITHM: 1. Read first line from the intermediate file. 2. Check to see if the opcode from the first line read is "START".If so then write label, opcode and operand field values of corresponding statement directly to final output files. 3. start the following processing for other lines in intermediate file if it is not a comment line until an "END" statement is reached. 4. Start writing labels LOCCTR opcode and operand fields of corresponding statement to the output file along with object code.The object code is found by assembling each statement opcode machine equivalent with label address. 5. If there is no symbol or label in the operand field, then the operand address is assigned as zero and it is assembled with object code of instruction 6. If OPCODE is BYTE,WORD,RESB etc are convert constants to oblect code close operand file and exit.

SOURCE CODE: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[10],ad[10],label[10],opcode[10],operand[10],symbol[10],ch; int st,diff,i,address,add,len,actual_len,finaddr,prevaddr,j=0; char mnemonic[15][15]={"LDA","STA","LDCH","STCH"}; char code[15][15]={"33","44","53","57"}; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("ASSMLIST.DAT","w"); fp2=fopen("SYMTAB.DAT","r"); fp3=fopen("INTERMED.DAT","r"); fp4=fopen("OBJCODE.DAT","w"); fscanf(fp3,"%s%s%s",label,opcode,operand); while(strcmp(opcode,"END")!=0) {

prevaddr=address; fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand); } finaddr=address; fclose(fp3); fp3=fopen("INTERMED.DAT","r"); fscanf(fp3,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand); fprintf(fp4,"H^%s^00%s^00%d\n",label,operand,finaddr); fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand); st=address; diff=prevaddr-st; fprintf(fp4,"T^00%d^%d",address,diff); } while(strcmp(opcode,"END")!=0) { if(strcmp(opcode,"BYTE")==0) { fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand); len=strlen(operand); actual_len=len-3; fprintf(fp4,"^"); for(i=2;i<(actual_len+2);i++) { itoa(operand[i],ad,16); fprintf(fp1,"%s",ad); fprintf(fp4,"%s",ad); } fprintf(fp1,"\n"); } else if(strcmp(opcode,"WORD")==0) { len=strlen(operand); itoa(atoi(operand),a,10); fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a); fprintf(fp4,"^00000%s",a); } else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0)) fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand); else { while(strcmp(opcode,mnemonic[j])!=0) j++;

if(strcmp(operand,"COPY")==0) fprintf(fp1,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code[j]); else { rewind(fp2); fscanf(fp2,"%s%d",symbol,&add); while(strcmp(operand,symbol)!=0) fscanf(fp2,"%s%d",symbol,&add); fprintf(fp1,"%d\t%s\t%s\t%s\t%s%d\n",address,label,opcode,operand,code[j],add ); fprintf(fp4,"^%s%d",code[j],add); } } fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand); } fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand); fprintf(fp4,"\nE^00%d",st); printf("\n Intermediate file is converted into object code"); fcloseall(); printf("\n\nThe contents of Intermediate file:\n\n\t"); fp3=fopen("INTERMED.DAT","r"); ch=fgetc(fp3); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp3); } printf("\n\nThe contents of Symbol Table :\n\n"); fp2=fopen("SYMTAB.DAT","r"); ch=fgetc(fp2); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp2); } printf("\n\nThe contents of Output file :\n\n"); fp1=fopen("ASSMLIST.DAT","r"); ch=fgetc(fp1); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp1); }

printf("\n\nThe contents of Object code file :\n\n"); fp4=fopen("OBJCODE.DAT","r"); ch=fgetc(fp4); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp4); } fcloseall(); getch(); } INPUT FILES: INTERMED.DAT COPY START 2000 2000 ** LDA FIVE 2003 ** STA ALPHA 2006 ** LDCH CHARZ 2009 ** STCH C1 2012 ALPHA RESW 1 2015 FIVE WORD 5 2018 CHARZ BYTE C'EOF' 2019 C1 RESB 1 2020 ** END ** SYMTAB.DAT ALPHA 2012 FIVE 2015 CHARZ 2018 C1 2019

4. IMPLEMENTATION OF A SINGLE PASS ASSEMBLER

AIM: To write a "C" program for the implementation of a single pass assembler. ALGORITHM: SOURCE CODE: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char opcode[10],operand[10],label[10],a[10],ad[10],symbol[10],ch; char code[10][10],code1[10][10]={"33","44","53","57"}; char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"}; char mnemonic1[10][10]={"LDA","STA","LDCH","STCH"}; int locctr,start,length,i=0,j=0,k,l=0; int st,diff,address,add,len,actual_len,finaddr,prevaddr; FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7; clrscr(); fp1=fopen("INPUT.DAT","r"); fp2=fopen("SYMTAB.DAT","w"); fp3=fopen("INETERMED.DAT","w"); fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { start=atoi(operand); locctr=start; fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); } else locctr=0; while(strcmp(opcode,"END")!=0) { fprintf(fp3,"%d",locctr); if(strcmp(label,"**")!=0) fprintf(fp2,"%s\t%d\n",label,locctr); strcpy(code[i],mnemonic[j]); while(strcmp(mnemonic[j],"END")!=0) {

if(strcmp(opcode,mnemonic[j])==0) { locctr+=3; break; } strcpy(code[i],mnemonic[j]); j++; } if(strcmp(opcode,"WORD")==0) locctr+=3; else if(strcmp(opcode,"RESW")==0) locctr+=(3*(atoi(operand))); else if(strcmp(opcode,"RESB")==0) locctr+=(atoi(operand)); else if(strcmp(opcode,"BYTE")==0) ++locctr; fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); } fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand); length=locctr-start; fcloseall(); printf("\n\nThe contents of Input file:\n\n"); fp1=fopen("INPUT.DAT","r"); ch=fgetc(fp1); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp1); } printf("\n\nLength of the input program is %d.",length); printf("\n\nThe contents of Symbol Table:\n\n"); fp2=fopen("SYMTAB.DAT","r"); ch=fgetc(fp2); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp2); } fcloseall(); fp4=fopen("ASSMLIST.DAT","w"); fp5=fopen("SYMTAB.DAT","r"); fp6=fopen("INTERMED.DAT","r"); fp7=fopen("OBJCODE.DAT","w"); fscanf(fp6,"%s%s%s",label,opcode,operand); while(strcmp(opcode,"END")!=0)

{ prevaddr=address; fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand); } finaddr=address; fclose(fp6); fp6=fopen("INTERMED.DAT","r"); fscanf(fp6,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand); fprintf(fp7,"H^%s^00%s^00%d\n",label,operand,finaddr); fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand); st=address; diff=prevaddr-st; fprintf(fp7,"T^00%d^%d",address,diff); } while(strcmp(opcode,"END")!=0) { if(strcmp(opcode,"BYTE")==0) { fprintf(fp4,"%d\t%s\t%s\t%s\t",address,label,opcode,operand); len=strlen(operand); actual_len=len-3; fprintf(fp7,"^"); for(k=2;k<(actual_len+2);k++) { itoa(operand[k],ad,16); fprintf(fp4,"%s",ad); fprintf(fp7,"%s",ad); } fprintf(fp4,"\n"); } else if(strcmp(opcode,"WORD")==0) { len=strlen(operand); itoa(atoi(operand),a,10); fprintf(fp4,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a); fprintf(fp7,"^00000%s",a); } else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0)) fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand); else { while(strcmp(opcode,mnemonic1[l])!=0) l++;

if(strcmp(operand,"COPY")==0) fprintf(fp4,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code1[l]); else { rewind(fp5); fscanf(fp5,"%s%d",symbol,&add); while(strcmp(operand,symbol)!=0) fscanf(fp5,"%s%d",symbol,&add); fprintf(fp4,"%d\t%s\t%s\t%s\t%s%d\n",address,label,opcode,operand,code1[l],ad d); fprintf(fp7,"^%s%d",code1[l],add); } } fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand); } fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand); fprintf(fp7,"\nE^00%d",st); printf("\nObject Program has been generated."); fcloseall(); printf("\n\nObject Program:\n\n"); fp7=fopen("OBJCODE.DAT","r"); ch=fgetc(fp7); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp7); } fcloseall(); getch(); } INPUT FILE: INPUT.DAT COPY START 2000 ** LDA FIVE ** STA ALPHA ** LDCH CHARZ ** STCH C1 ALPHA RESW 1 FIVE WORD 5 CHARZ BYTE C'EOF'

C1 RESB 1 ** END **

5. IMPLEMENTATION OF A MACRO PROCESSOR

AIM: To write a "C" program for the implementation of a macro. ALGORITHM: 1. Get the statement from input file. 2. From the line read, check if opcode is directive "MACRO" if so then number of macro "n" must be incremented. 3. Repeat step 1 and 2 until end of file is encountered. 4. Open 'n' number of files in write mode. These files will later hold body of 'n' macro respectively. 5. Rewind the input file pointer 6. If opcode is "macro" 7. Enter macro name present in operand file field into array named "m". 8. Write line to expanded output file. 9. Enter lines in body of each macro into corresponding files already opened in file. 10. Write body of each macro to be expanded output file also until "MEND" is reached. 11. Else if OPCODE is "CALL" the line must be a macro invocation statement so the macro body is retrieved from the corresponding file. 12. Write all the remaining lines directly to expanded lines.

SOURCE CODE: #include<studio.h> #include<stdlib.h> #include<conio.h> #include<string.h> FILE *f1,*f2,*f3,*f4,*f5; void main() { char lbl[20],opc[20],opr[20],mname[20],arg[20],check[20];char

ch,dlbl[20],dopc[20],dopr[20]; int c; clrscr(); f1=fopen("MACIN.DAT","r"); rewind(f1); f2=fopen("NAMETAB.DAT","r"); rewind(f2); f3=fopen("DEFTAB.DAT","r"); f4=fopen("EXPAND.DAT","w"); f5=fopen("ARGTAB.DAT","w"); while(!feof(f1)) { l1: fscanf(f1,"%s %s %s",lbl,opc,opr); if(strcmp(opc,mname)==0) c=1; if(strcmp(opc,"MACRO")==0) { while(strcmp(opc,"MEND")!=0) { fscanf(f1,"%s%s%s",lbl,opc,opr); continue; } goto l1; } rewind(f2); rewind(f3); fscanf(f2,"%s",mname); if(strcmp(opc,mname)==0) { fprintf(f5," %s",opr); rewind(f5); while(!feof(f3)) { fscanf(f3,"%s%s%s",dlbl,dopc,dopr); if(strcmp(dopc,"MEND")!=0) { if(strcmp(dopc,"MACRO")==0) { continue; } if(strcmp(dopr,"=X'?1'")==0) strcpy(dopr,"=X'F1'"); if(strcmp(dopr,"?2,X")==0) strcpy(dopr,"BUFFER,X"); if(strcmp(dopr,"?3")==0)

strcpy(dopr,"LENGTH"); if(c==1) { fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr); c=0; } fprintf(f4," %s\t%s\t%s\n",dlbl,dopc,dopr); } } goto l1; } fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr); } fcloseall(); printf("\n INPUT\n\n Macro Program before expanded \n"); printf(" ---------------------------------\n"); f1=fopen("MACIN.DAT","r"); ch=fgetc(f1); while(ch!=EOF) { printf("%c",ch); ch=fgetc(f1); } printf("\n Definition Table \n"); printf(" ---------------------------------\n"); f2=fopen("DEFTAB.DAT","r"); ch=fgetc(f2); while(ch!=EOF) { printf("%c",ch); ch=fgetc(f2); } printf("\n Name Table \n"); printf(" ---------------------------------\n"); f3=fopen("NAMETAB.DAT","r"); ch=fgetc(f3); while(ch!=EOF) { printf("%c",ch); ch=fgetc(f3); } getch(); clrscr(); printf("\n\n OUTPUT\n\n Macro Program after expanded \n"); printf(" ---------------------------------\n\n"); f4=fopen("EXPAND.DAT","r");

ch=fgetc(f4); while(ch!=EOF) { printf("%c",ch); ch=fgetc(f4); } printf("\n Argument Table \n"); printf(" ---------------------------------\n\n"); f5=fopen("ARGTAB.DAT","r"); ch=fgetc(f5); while(ch!=EOF) { printf("%c",ch); ch=fgetc(f5); } fcloseall(); getch(); } INPUT FILE: MACIN.DAT COPY START NULL RDBUFF MACRO INDEV,BUFADR,RECLTH NULL CLEAR X NULL CLEAR A NULL CLEAR S NULL +LDT #4096 NULL TD =X'&INDEV' NULL JEQ *-3 NULL RD =X'&INDEV' NULL COMPR A,S NULL JEQ *+11 NULL STCH BUFADR,X NULL TIXR T NULL JLT *-19 NULL STX RECLTH NULL MEND NULL FIRST STL RETADR CLOOP RDBUFF F1,BUFFER,LENGTH NULL LDA LENGTH NULL COMP #0 NULL JEQ ENDFIL

EOF BYTE C'EOF' THREE WORD 3 RETADR RESW 1 LENGTH RESW 1 BUFFER RESB 4096 NULL END FIRST DEFTAB.DAT COPY START NULL RDBUFF MACRO &INDEV,&BUFADR,&RECLTH NULL CLEAR X NULL CLEAR A NULL CLEAR S NULL +LDT #4096 NULL TD =X'?1' NULL JEQ *-3 NULL RD =X'?1' NULL COMPR A,S NULL JEQ *+11 NULL STCH ?2,X NULL TIXR T NULL JLT *-19 NULL STX ?3 NULL MEND NULL NAMETAB.DAT RDBUFF

6. IMPLEMENTATION OF AN ABSOLUTE LOADER

AIM: To write a "C" program for the implementation of an Absolute. ALGORITHM: 1. Read loader record and filter the starting location and other details. 2. Read the first text record 3. If the object code is in character form convert it to internal hexadecimal representation. 4. Move object code to specified location in memory. 5. Write the starting location counter value of block of object code and corresponding internal hexadecimal representation to the output file. 6. Read next text record from the input file 7. Close all opened files and exit. SOURCE CODE: #include<stdio.h> #include<conio.h> #include<string.h> char input[10],label[10],ch1,ch2; int addr, w=0, start, ptaddr, l, length=0, end, count=0, k, taddr, address, i=0; FILE *fp1,*fp2; void check(); void main() { clrscr(); fp1=fopen("INPUT.dat","r"); fp2=fopen("OUTPUT.dat","w"); fscanf(fp1,"%s",input); printf("\n\n\t\t\t\tABSOLUTE LOADER\n"); fprintf(fp2,"\n-------------------------------------------------------\n"); fprintf(fp2,"MEMORY ADDRESS\t\t\tCONTENTS"); fprintf(fp2,"\n-------------------------------------------------------\n"); while(strcmp(input,"E")!=0) {

if(strcmp(input,"H")==0) { fscanf(fp1,"%s %x %x %s",label,&start,&end,input); address=start; } else if(strcmp(input,"T")==0) { l=length; ptaddr=addr; fscanf(fp1,"%x %x %s",&taddr,&length,input); addr=taddr; if(w==0) { ptaddr=address; w=1; } for(k=0;k<(taddr-(ptaddr+l));k++) { address=address+1; fprintf(fp2,"xx"); count++; if(count==4) { fprintf(fp2," "); i++; if(i==4) { fprintf(fp2,"\n\n%x\t\t",address); i=0; } count=0; } } if(taddr==start) fprintf(fp2,"\n\n%x\t\t",taddr); fprintf(fp2,"%c%c",input[0],input[1]); check(); fprintf(fp2,"%c%c",input[2],input[3]); check(); fprintf(fp2,"%c%c",input[4],input[5]); check(); fscanf(fp1,"%s",input); } else { fprintf(fp2,"%c%c",input[0],input[1]);

check(); fprintf(fp2,"%c%c",input[2],input[3]); check(); fprintf(fp2,"%c%c",input[4],input[5]); check(); fscanf(fp1,"%s",input); } } fprintf(fp2,"\n-------------------------------------------------------\n"); fcloseall(); printf("\n\n The contents of output file:\n\n"); fp2=fopen("OUTPUT.DAT","r"); ch2=fgetc(fp2); while(ch2!=EOF) { printf("%c",ch2); ch2=fgetc(fp2); } fcloseall(); getch(); } void check() { count++; address++; taddr=taddr+1; if(count==4) { fprintf(fp2," "); i++; if(i==4) { fprintf(fp2,"\n\n%x\t\t",taddr); i=0; } count=0; } } INPUT FILE: INPUT.DAT H COPY 001000 00107A T 001000 1E 141033 482039 001036 281030 301015 482061 3C1003 00102A 0C1039 00102D T 00101E 15 0C1036 482061 081033 4C0000 454F46 000003 000000 T 001047 1E 041030 001030 E0205D 30203F D8205D 281030 302057 549039 2C205E 38203F

T 001077 1C 101036 4C0000 000000 001000 041030 E02079 302064 509039 DC2079 2C1036 E 001000

7. IMPLEMENTATION OF RELOCATING LOADER

AIM: To write a "C" program for the implementation of Relocating Loader. ALGORITHM:

1. Enter new starting location to which object code has to be relocated. 2. Read the content of the input file as strings one at a time in an array input. 3. Transfer the string read in array "input" into another array "output" until it is incremented. 4. Move consecutive next 3 strings into array "output". 5. Cover current relocation bit associated with each text record to binary form. 6. Make necessary changes in corresponding words of object code and store the updated object code into array "output". 7. Move object code for which corresponding relocation bit is not set directly to the array "output" from array "input" without any change. 8. Repeat step 2 to 8 until end record is encountered. 9. If the object code is in character from convert into internal hexadecimal representation . 10. Move object code to specified location in memory. 11. write starting LOCCTR value of block of object code and the corresponding internal hexadecimal representative to the output files.

SOURCE CODE:

#include<stdio.h> #include<conio.h>

#include<string.h> #include<stdlib.h> void convert(char h[12]); char bitmask[12]; char bit[12]={0}; void main() {char add[6],length[10],input[10],binary[12],relocbit,ch,pn[5]; int start,inp,len,i,address,opcode,addr,actualadd,tlen; FILE *fp1,*fp2; clrscr(); printf("\n\n Enter the actual starting address : "); scanf("%x",&start); fp1=fopen("RLIN.DAT","r"); fp2=fopen("RLOUT.DAT","w"); fscanf(fp1,"%s",input); fprintf(fp2," ----------------------------\n"); fprintf(fp2," ADDRESS\tCONTENT\n"); fprintf(fp2," ----------------------------\n"); while(strcmp(input,"E")!=0) { if(strcmp(input,"H")==0) { fscanf(fp1,"%s",pn); fscanf(fp1,"%x",add); fscanf(fp1,"%x",length); fscanf(fp1,"%s",input); } if(strcmp(input,"T")==0) { fscanf(fp1,"%x",&address);

fscanf(fp1,"%x",&tlen); fscanf(fp1,"%s",bitmask); address+=start; convert(bitmask); len=strlen(bit); if(len>=11) len=10; for(i=0;i<len;i++) { fscanf(fp1,"%x",&opcode); fscanf(fp1,"%x",&addr); relocbit=bit[i]; if(relocbit=='0') actualadd=addr; else actualadd=addr+start; fprintf(fp2,"\n %x\t\t%x%x\n",address,opcode,actualadd); address+=3; } fscanf(fp1,"%s",input); } } fprintf(fp2," ----------------------------\n"); fcloseall(); printf("\n\n The contents of output file (RLOUT.DAT):\n\n"); fp2=fopen("RLOUT.DAT","r"); ch=fgetc(fp2); while(ch!=EOF) { printf("%c",ch);

ch=fgetc(fp2); } fclose(fp2); getch(); } void convert(char h[12]) { int i,l; strcpy(bit,""); l=strlen(h); for(i=0;i<l;i++) { switch(h[i]) { case '0': strcat(bit,"0"); break; case '1': strcat(bit,"1"); break; case '2': strcat(bit,"10"); break; case '3': strcat(bit,"11"); break; case '4': strcat(bit,"100"); break; case '5':

strcat(bit,"101"); break; case '6': strcat(bit,"110"); break; case '7': strcat(bit,"111"); break; case '8': strcat(bit,"1000"); break; case '9': strcat(bit,"1001"); break; case 'A': strcat(bit,"1010"); break; case 'B': strcat(bit,"1011"); break; case 'C': strcat(bit,"1100"); break; case 'D': strcat(bit,"1101"); break; case 'E': strcat(bit,"1110"); break; case 'F':

strcat(bit,"1111"); break; } } } INPUT FILE: RLIN.DAT H COPY 001000 00107A T 001000 1E FFC 14 0033 48 1039 10 0036 28 0030 30 0015 48 1061 3C 0003 20 002A 1C 0039 30 002D T 002500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003 E 000000

8.

IMPLEMENTATION OF A PASS ONE OF A DIRECT-LINKING LOADER

AIM: To write a "C" program to implement Pass one of a direct-linking loader. ALGORITHM:

1.Get the PROGADDR from the user 2.Initiate CSADDR with value of PROGADDR 3.Read header record from object program 4.Store the length of the control section in CSLTH 5.Search the ESTAB for control section name (i)If found display an error message (ii)Otherwise, insert the control section name with CSADDR into ESTAB 6.Read the next input record 7.If the record type is END goto step 10 8.If the record type is not D goto step 6 9.Search the ESTAB for symbol names given in D record. (i)If found display an error message (ii)Otherwise, insert the symbol into ESTAB with the sum of CSADDR and indicated address and goto step 6. 10.Add CSLTH to CSADDR. 11.If end of file is reached, Exit. Otherwise goto step 3.

SOURCE CODE:

#include<stdio.h> #include<conio.h> #include<string.h> struct estab { char csname[10]; char extsym[10]; int address; int length; }es[20]; void main() { char input[10],name[10],symbol[10],ch; int count=0,progaddr,csaddr,add,len; FILE *fp1,*fp2; clrscr();

fp1=fopen("LINP.DAT","r"); fp2=fopen("LOADMAP.DAT","w"); printf("\n\nEnter the address where the program has to be loaded : "); scanf("%x",&progaddr); csaddr=progaddr; fprintf(fp2,"CS_NAME\tEXT_SYM_NAME\tADDRESS\tLENGTH\n"); fprintf(fp2,"--------------------------------------\n"); fscanf(fp1,"%s",input); while(strcmp(input,"END")!=0) { if(strcmp(input,"H")==0) { fscanf(fp1,"%s",name); strcpy(es[count].csname,name); strcpy(es[count].extsym," "); fscanf(fp1,"%x",&add); es[count].address=add+csaddr; fscanf(fp1,"%x",&len); es[count].length=len;

fprintf(fp2,"%s\t%s\t\t%x\t%x\n\n",es[count].csname,es[count].extsym,es[count]. address,es[count].length); count++; } else if(strcmp(input,"D")==0) { fscanf(fp1,"%s",input); while(strcmp(input,"R")!=0) { strcpy(es[count].csname," ");

strcpy(es[count].extsym,input); fscanf(fp1,"%x",&add); es[count].address=add+csaddr; es[count].length=0;

fprintf(fp2,"%s\t%s\t\t%x\t%x\n\n",es[count].csname,es[count].extsym,es[count]. address,es[count].length); count++; fscanf(fp1,"%s",input); } csaddr=csaddr+len; } else if(strcmp(input,"T")==0) { while(strcmp(input,"E")!=0) fscanf(fp1,"%s",input); } fscanf(fp1,"%s",input); } fprintf(fp2,"--------------------------------------"); fcloseall(); printf("\n The contents of output file:\n\n"); fp2=fopen("LOADMAP.DAT","r"); ch=fgetc(fp2); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp2); } fclose(fp2);

getch(); }

INPUT FILE: LINP.DAT H PROGA 000000 000063 D LISTA 000040 ENDA 000054 R LISTB ENDB LISTC ENDC T 000020 141033 465555 678909 568787 345678 T 000054 000014 789087 776555 876666 456666 M 000054 06 +LISTC E 000020 H PROGB 000000 00007F D LISTB 000060 ENDB 000070 R LISTA ENDA LISTC ENDC T 000036 141033 465555 678909 568787 345678 T 000070 000000 789087 776555 876666 456666 M 000054 06 +ENDA M 000054 06 -LISTA M 000054 06 +LISTC E H PROGC 000000 000051 D LISTC 000030 ENDC 000042 R LISTA ENDA LISTC ENDB T 000018 141033 465555 678909 568787 345678 T 000042 000020 789087 776555 876666 456666 M 000054 06 +ENDA M 000054 06 -LISTA M 000054 06 +PROGC E END

9. AIM:

IMPLEMENTATION OF PASS TWO OF A DIRECT-LINKING LOADER

To write a "C" program to implement Pass two of a direct-linking loader. ALGORITHM: 1.Read the header record from object program. 2.Get the CSADDR for the correct control section from ESTAB. 3.Read next input record. 4.If the record type is END, go to step 8.5.If the record type is T, place the object code in location which is sum of CSADDR and specified address. 6.If the record type is M search the ESTAB for symbol table. (i)If found add or subtract symbol value at location CSADDR + specified Address. (ii)Otherwise display error message. 7.Read next input record and go to step 4. 8.If end of file is reached,Exit.Otherwise go to step 1.

SOURCE CODE:

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct exttable { char csect[20], sname[20]; int padd,plen; }estab[20]; struct objectcode {

unsigned char code[15]; int add; }obcode[500]; void main() { FILE *fp1,*fp2,*fp3; int i,j,x,y,pstart,exeloc,start,textloc,loc,textlen,length,location,st,s; int n=0,num=0,inc=0,count=0,record=0,mloc[30],mlen[30]; signed long int newadd; char operation,lbl[10],input[10],label[50][10],opr[30],ch,*add1,address[10]; clrscr(); fp1=fopen("L2IN.dat","r"); fp2=fopen("L1OUT.dat","r"); fp3=fopen("L2OUT.dat","w"); while(!feof(fp2)) { fscanf(fp2,"%s %s %x %x", estab[num].csect, estab[num].sname, &estab[num].padd, &estab[num].plen); num++; } exeloc=estab[0].padd; loc=exeloc; start=loc; st=start; while(!feof(fp1)) { fscanf(fp1,"%s",input); if(strcmp(input,"H")==0) {

fscanf(fp1,"%s",input); for(i=0;i<num;i++) if(strcmp(input,estab[i].csect)==0) { pstart=estab[i].padd; break; } while(strcmp(input,"T")!=0) fscanf(fp1,"%s",input); } do { if(strcmp(input,"T")==0) { fscanf(fp1,"%x",&textloc); textloc=textloc+pstart; for(i=0;i<(textloc-loc);i++) { strcpy(obcode[inc].code,".."); obcode[inc++].add=start++; } fscanf(fp1,"%x",&textlen); loc=textloc+textlen; } else if(strcmp(input,"M")==0) { fscanf(fp1,"%x",&mloc[record]); mloc[record]=mloc[record]+pstart; fscanf(fp1,"%x",&mlen[record]); fscanf(fp1,"%s",label[record++]);

} else { length=strlen(input); x=0; for(i=0;i<length;i++) { obcode[inc].code[x++]=input[i]; if(x>1) { obcode[inc++].add=start++; x=0; } } } fscanf(fp1,"%s",input); }while(strcmp(input,"E")!=0); if(strcmp(input,"E")==0) fscanf(fp1,"%s",input); } for(n=0;n<record;n++) { operation=label[n][0]; length=strlen(label[n]); for(i=1;i<length;i++) { lbl[i-1]=label[n][i]; } lbl[length-1]='\0'; length=0;

strcpy(address,"\0"); location=mloc[n]-exeloc; loc=location; count=0; while(length<mlen[n]) { strcat(address,obcode[location++].code); count++; length+=2; } for(i=0;i<num;i++) { if(strcmp(lbl,estab[i].csect)==0) break; if(strcmp(lbl,estab[i].sname)==0) break; } switch(operation) { case '+': newadd=strtol(address,&add1,16)+(long int)estab[i].padd; break; case '-': newadd=strtol(address,&add1,16)-(long int)estab[i].padd; break; } ltoa(newadd,address,16); x=0; y=0; while(count>0) {

obcode[loc].code[x++]=address[y++]; if(x>1) { x=0; loc++; count--; } }} count=0; n=0; s=st-16; fprintf(fp3,"%x\t",s); for(i=1;i<=16;i++) { fprintf(fp3,"xx"); if(i==4||i==8||i==12) { fprintf(fp3,"\t"); } } fprintf(fp3,"\n\n%x\t",obcode[0].add); for(i=0;i<inc;i++) { fprintf(fp3,"%s",obcode[i].code); n++; if(n>3) { fprintf(fp3,"\t"); n=0; count++; }

if(count>3) { fprintf(fp3,"\n\n%x\t",obcode[i+1].add); count=0; }} fcloseall(); printf("\n\t***** PASS TWO OF A DIRECT-LINKING LOADER *****\n"); printf("\nThe contents of the output file (L2OUT.DAT):"); printf("\n---------------------------------------------------------------"); printf("\nAddress\t\t\t\tContents"); printf("\n---------------------------------------------------------------\n"); fp3=fopen("L2OUT.dat","r"); ch=fgetc(fp3); while(ch!=EOF) { printf("%c",ch); ch=fgetc(fp3); } fclose(fp3); getch(); } INPUT FILES:

LINK1IN.DAT H PROGA 000000 000063 D LISTA 000040 ENDA 000054 R LISTB ENDB LISTC ENDC T 000020 0A 03201D 77100004 050014 T 000054 0F 100014 000008 004051 000004 100000 M 000024 05 +LISTB

M 000054 06 +LISTC M 000060 06 +LISTB M 000060 06 -LISTA E 000020

H PROGB 000000 00007F D LISTB 000060 ENDB 000070 R LISTA ENDA LISTC ENDC T 000036 0B 03100000 772027 05100000 T 000070 0F 100000 000008 004051 000004 100060 M 000037 05 +LISTA M 00003E 05 +ENDA M 00003E 05 -LISTA M 000070 06 +ENDA M 000070 06 -LISTA M 000070 06 +LISTC M 00007C 06 +PROGB M 00007C 06 -LISTA E 000000

H PROGC 000000 0000051 D LISTC 000030 ENDC 000042 R LISTA ENDA LISTB ENDB T 000018 0C 03100000 77100004 05100000 T 000042 0F 100030 000008 004051 000004 100000 M 000019 05 +LISTA M 00001D 05 +LISTB M 000021 05 +ENDA M 000021 05 -LISTA M 000042 06 +ENDA

M 000042 06 -LISTA M 000042 06 +PROGC M 00004E 06 +LISTB M 00004E 06 -LISTA E 000000 LINK1OUT.DAT PROGA ** 4000 63 ** LISTA 4040 ** ENDA 4054 PROGB ** 4063 7F ** LISTB 40C3 ** ENDB 40D3 PROGC ** 40E2 51 ** LISTC 4112 ** ENDC 4124

10. IMPLEMENTATION OF A SIMPLE TEXT EDITOR WITH FEATURES LIKE INSERTION, DELETION

AIM: To write a "C" program to implement "Text Editor" with features like insertion, deletion.

ALGORITHM: 1.Display options new, open and exit and get choice. 2.If choice is 1 , call Create() function. 3.If choice is 2, call Display() function. 4.If choice is 3, call Append() function. 5.If choice is 4, call Delete() function. 6.If choice is 5, call Display() function. 7.Create() 7.1 Get the file name and open it in write mode.

7.2 Get the text from the user to write it. 8.Display() 8.1 Get the file name from user. 8.2 Check whether the file is present or not. 8.2 If present then display the contents of the file. 9.Append() 9.1 Get the file name from user. 9.2 Check whether the file is present or not. 9.3 If present then append the file by getting the text to add with the existing file. 10. Delete() 10.1 Get the file name from user. 10.2 Check whether the file is present or not. 10.3 If present then delete the existing file.

SOURCE CODE:

#include<stdio.h> #include<conio.h> #include<process.h> int i,j,ec,fg,ec2; char fn[20],e,c; FILE *fp1,*fp2,*fp; void Create(); void Append(); void Delete(); void Display(); void main() { do { clrscr(); printf("\n\t\t***** TEXT EDITOR *****"); printf("\n\n\tMENU:\n\t-----\n "); printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.DELETE\n\t5.EXIT\n") ; printf("\n\tEnter your choice: "); scanf("%d",&ec); switch(ec) { case 1: Create(); break; case 2: Display(); break; case 3: Append(); break; case 4: Delete(); break; case 5: exit(0); }

}while(1); } void Create() { fp1=fopen("temp.txt","w"); printf("\n\tEnter the text and press '.' to save\n\n\t"); while(1) { c=getchar(); fputc(c,fp1); if(c == '.') { fclose(fp1); printf("\n\tEnter then new filename: "); scanf("%s",fn); fp1=fopen("temp.txt","r"); fp2=fopen(fn,"w"); while(!feof(fp1)) { c=getc(fp1); putc(c,fp2); } fclose(fp2); break; }} } void Display() { printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL) { printf("\n\tFile not found!"); goto end1; } while(!feof(fp1)) { c=getc(fp1); printf("%c",c);

} end1: fclose(fp1); printf("\n\n\tPress any key to continue..."); getch(); } void Delete() { printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL) { printf("\n\tFile not found!"); goto end2; } fclose(fp1); if(remove(fn)==0) { printf("\n\n\tFile has been deleted successfully!"); goto end2; } else printf("\n\tError!\n"); end2: printf("\n\n\tPress any key to continue..."); getch(); } void Append() { printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL) { printf("\n\tFile not found!"); goto end3; } while(!feof(fp1)) { c=getc(fp1);

printf("%c",c); } fclose(fp1); printf("\n\tType the text and press 'Ctrl+S' to append.\n"); fp1=fopen(fn,"a"); while(1) { c=getch(); if(c==19) goto end3; if(c==13) { c='\n'; printf("\n\t"); fputc(c,fp1); } else { printf("%c",c); fputc(c,fp1); } } end3: fclose(fp1); getch();

You might also like