Professional Documents
Culture Documents
CS2308
NAME :
REGNO :
Internal Examiner
External Examiner
INDEX
S.NO 1 2 3 4 5 6 7 8 9 10 DATE NAME OF EXPERIMENT PAGE SIGN
11 12 13 14
Ex.No: 01
SYMBOL TABLE
AIM: To implement a Symbol table with functions to create, insert, modify, search and display in C language. ALGORITHM: 1. 2. 3. 4. 5. 6. 7. Start the program Define the structure of the symbol table Enter the choice for performing the operations in the symbol table If choice is 1, search symbol table for the symbol to be inserted. If the symbol is already present display Duplicate Symbol, else insert symbol and corresponding address in the symbol table If choice is 2, symbols present in the symbols table are displayed If choice is 3, symbol to be deleted is searched in the symbol table, if found deletes else displays Not Found. If choice is 5, the symbol to be modified is searched in the symbol table. The label or address or both can be modified.
#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; struct symbtab *next; }; struct symbtab *first,*last; void main() { int op; int y;
char la[10]; clrscr(); do { printf("\nSYMBOL TABLE IMPLEMENTATION\n"); printf("1.INSERT\n"); printf("2.DISPLAY\n"); printf("3.DELETE\n"); printf("4.SEARCH\n"); printf("5.MODIFY\n"); printf("6.END\n"); printf("\nEnter your option: "); scanf("%d",&op); switch(op) { case 1: insert(); display(); break; case 2: display(); break;
case 3: del(); display(); break; case 4: printf("Enter the label to be searched: "); scanf("%s",la); y=search(la); if(y==1) printf("The label is already in the symbol table\n"); else printf("The label is not found in the symbol tablel\n"); break; case 5: modify(); display(); break; case 6: break; } }while(op<6); getch(); } void insert() { int n; char l[10]; printf("Enter the label: "); scanf("%s",l); n=search(l); if(n==1) printf("The label is already in the symbol table. Duplicate cant be inserted\n"); else { struct symbtab *p; p=malloc(sizeof(struct symbtab)); strcpy(p->label,l); printf("Enter the address: "); scanf("%d",&p->addr); p->next=null; if(size==0) { first=p; last=p; } else { last->next=p; last=p; } size++;
} } void display() { int i; struct symbtab *p; p=first; printf("LABEL\tADDRESS\n"); for(i=0;i<size;i++) { printf("%s\t%d\n",p->label,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("What do you want to modify?\n"); printf("1.Only the label\n"); printf("2.Only the address of a particular label\n"); printf("3.Both the label and address\n"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the old label\n"); scanf("%s",l); printf("Enter the new label: "); scanf("%s",nl); s=search(l); if(s==0) printf("\nNo such label");
else { for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) { strcpy(p->label,nl); } p=p->next; } } break; case 2: printf("Enter the label whose address is to be modified: "); scanf("%s",l); printf("Enter the new address: "); scanf("%d",&add); s=search(l); if(s==0) printf("\nNo such label"); else { for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) { p->addr=add; } p=p->next; } } break; case 3: printf("Enter the old label: "); scanf("%s",l); printf("Enter the new label: "); scanf("%s",nl); printf("Enter the new address: "); scanf("%d",&add); s=search(l); if(s==0) printf("\nNo such label"); else { for(i=0;i<size;i++) { if(strcmp(p->label,l)==0) { strcpy(p->label,nl); p->addr=add; }
p=p->next; } } break; } } void del() { int a; char l[10]; struct symbtab *p,*q; p=first; printf("Enter the label to be deleted: "); scanf("%s",l); a=search(l); if(a==0) printf("Label 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--; } }
OUTPUT: SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 1 Enter the label: FIRST Enter the address: 1002 LABEL ADDRESS FIRST 1002 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 1 Enter the label: SECOND Enter the address: 1080 LABEL ADDRESS FIRST 1002 SECOND 1080 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 2 LABEL ADDRESS FIRST 1002 SECOND 1080
1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 3 Enter the label to be deleted: THIRD Label not found LABEL ADDRESS FIRST 1002 SECOND 1080 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 3 Enter the label to be deleted: SECOND LABEL ADDRESS FIRST 1002 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 4 Enter the label to be searched: SECOND The label is not found in the symbol table SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 4 Enter the label to be searched: FIRST The label is already in the symbol table
SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 5 What do you want to modify? 1.Only the label 2.Only the address of a particular label 3.Both the label and address Enter your choice: 1 Enter the old label FIRST Enter the new label: RECORD LABEL ADDRESS RECORD 1002 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 5 What do you want to modify? 1.Only the label 2.Only the address of a particular label 3.Both the label and address Enter your choice: 2 Enter the label whose address is to be modified: RECORD Enter the new address: 2002 LABEL ADDRESS RECORD 2002 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 5 What do you want to modify? 1.Only the label 2.Only the address of a particular label
3.Both the label and address Enter your choice: 3 Enter the old label: RECORD Enter the new label: FIRST Enter the new address: 8002 LABEL ADDRESS FIRST 8002 SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option:6
RESULT: Thus a Symbol table with functions to create, insert, modify, search and display is implemented in C language.
Ex.No: 02
PROGRAM: /* C Program to implement PASS ONE OF TWO PASS ASSEMBLER #include<stdio.h> #include<conio.h> #include<string.h> void main() { char opcode[10],mnemonic[10],operand[10],label[10],code[10]; int locctr,start,length; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("input.dat","r+"); fp2=fopen("symbol.dat","w+"); fp3=fopen("output.dat","w+"); fp4=fopen("optab.dat","r+"); fprintf(fp3,"\t \t pass1 assembler \n"); fscanf(fp1,"%s %s %s",label,opcode,operand); if(strcmp(opcode,"START")==0) { start=atoi(operand); locctr=start; fprintf(fp3,"\t %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 \t",locctr); if(strcmp(label,"**")!=0) fprintf(fp2,"%s \t %d \n",label,locctr); fscanf(fp4,"%s ",code); while(strcmp(code,"END")!=0) { if(strcmp(opcode,code)==0) { locctr+=3; break; } fscanf(fp4,"%s",code); } 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,"%s \t %s \t %s \n",label,opcode,operand); fscanf(fp1,"%s %s %s",label,opcode,operand); } fprintf(fp3,"%d \t %s \t %s \n",locctr,opcode,operand); length=locctr-start; printf("The length of the pgm is %d",length); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); }
INPUT.DAT: MAIN BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** START LDA ADD LDCH STCH WORD RESW BYTE RESB END 2000 NUM1 NUM2 CHAR1 CHAR2 5 1 C'A' 1 BEGIN
OPTAB.DAT: ADD ADDR SUB SUBR MUL MULR DIV DIVR LDA LDB LDX LDCH STA STB STX STCH TIX J JSUB RSUB JEQ JLT JGT START END 18 90 1C 94 20 98 24 9C 00 68 04 50 0C 78 10 54 2C 3C 48 4C 30 38 34 * *
SYMTAB.DAT: BEGIN NUM1 NUM2 CHAR1 CHAR2 2000 2012 2015 2018 2019
OUT.DAT: MAIN 2000 2003 2006 2009 2012 2015 2018 2019 2020 START BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** 2000 LDA ADD LDCH STCH WORD RESW BYTE RESB END NUM1 NUM2 CHAR1 CHAR2 5 1 C'A' 1 BEGIN
Ex.No: 03
4.
PROGRAM: /* C Program to implement PASS TWO OF TWO PASS ASSEMBLER #include<stdio.h> #include<conio.h> #include<string.h> void main() { char opcode[10],mnemonic[10],operand[10],label[10],code[10]; int locctr,start,length; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("input.dat","r+"); fp2=fopen("symbol.dat","w+"); fp3=fopen("output.dat","w+"); fp4=fopen("optab.dat","r+"); fprintf(fp3,"\t \t pass1 assembler \n"); fscanf(fp1,"%s %s %s",label,opcode,operand); if(strcmp(opcode,"START")==0) { start=atoi(operand); locctr=start; fprintf(fp3,"\t %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 \t",locctr); if(strcmp(label,"**")!=0) fprintf(fp2,"%s \t %d \n",label,locctr); fscanf(fp4,"%s ",code); while(strcmp(code,"END")!=0) { if(strcmp(opcode,code)==0) { locctr+=3; break; } fscanf(fp4,"%s",code); } 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,"%s \t %s \t %s \n",label,opcode,operand); fscanf(fp1,"%s %s %s",label,opcode,operand); } fprintf(fp3,"%d \t %s \t %s \n",locctr,opcode,operand); length=locctr-start; printf("The length of the pgm is %d",length); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); }
INPUT.DAT: MAIN BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** OPTAB: ADD ADDR SUB SUBR MUL MULR DIV DIVR LDA LDB LDX LDCH STA STB STX STCH TIX J JSUB RSUB JEQ JLT JGT START END 18 90 1C 94 20 98 24 9C 00 68 04 50 0C 78 10 54 2C 3C 48 4C 30 38 34 * * START LDA ADD LDCH STCH WORD RESW BYTE RESB END 2000 NUM1 NUM2 CHAR1 CHAR2 5 1 C'A' 1 BEGIN
SYMTAB.DAT: BEGIN NUM1 NUM2 CHAR1 CHAR2 2000 2012 2015 2018 2019
OUT.DAT MAIN 2000 2003 2006 2009 2012 2015 2018 2019 2020 START BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** 2000 LDA ADD LDCH STCH WORD RESW BYTE RESB END NUM1 NUM2 CHAR1 CHAR2 5 1 C'A' 1 BEGIN
TWOOUT.DAT: 2000 2003 2006 2009 2012 2015 2018 2019 2020 MAIN BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** START LDA ADD LDCH STCH WORD RESW BYTE RESB END 2000 NUM1 NUM2 CHAR1 CHAR2 5 1 C'A' 1 BEGIN 002012 182015 502018 542019 000005 41
Ex.No: 04
PROGRAM: /* SINGLE PASS ASSEMBLER */ #include<stdio.h> #include<string.h> #define q 11//no. of mnemonics in the array A void main() { int lc,ad,address,err=0; int s,num,l,i=0,j,n=0,line=1,f=0,f1=0,t=0,ni=0,m=0,t1; FILE *fp1,*fp2,*fp3,*fp4; char lab[10],op[10],val[10],code[10]; char a[20] [15]={"STA","STL","LDA","LDB","J","JEQ","J","SUB","COMP","STCH","ADD","SUB"}; char b[20][15]={"14","32","03","69","34","30","48","28","24","16","0C"}; char sym[15][10]; int symadd[15]; clrscr(); fp1=fopen("INPUT.DAT","r"); fp2=fopen("OBJFILE.DAT","w"); fp3=fopen("ERROR.DAT","w"); fp4=fopen("SYMTAB.DAT","w"); while((!feof(fp1))) { fscanf(fp1,"%s\t%s\t%s",lab,op,val); t++; m++; if(strcmp(op,".")==0) m=0; else if(strcmp(op,"END")==0) break; } t=t-1; m--; fclose(fp1); fp1=fopen("INPUT.DAT","r"); fscanf(fp1,"%s\t%s\t%x",lab,op,&lc); fprintf(fp3,"-------------------------------------\n"); fprintf(fp3,"LINE NO.\t|ERROR FOUND\n"); fprintf(fp3,"-------------------------------------"); fprintf(fp4,"SYMBOL\tADDRESS"); s=lc; fprintf(fp2,"H^%s^00%x^%x\n",lab,lc,t*3); fprintf(fp2,"T^00%x^",lc); if(m>10) fprintf(fp2,"1E"); else
fprintf(fp2,"%x",m*3); while((op,".")!=0&&(!feof(fp1))) { fscanf(fp1,"%s\t%s\t%s",lab,op,val); line++; if(strcmp(lab,"$")!=0) { for(i=0;i<n;i++) { if(strcmp(lab,sym[i])==0) { f=1; break; } f=0; } if(f==0) { strcpy(sym[n],lab); symadd[n]=lc; fprintf(fp4,"\n%s\t%x",lab,lc); n++; } if(f==1){ fprintf(fp3,"%d\t\t|SYMBOL ALREADY DEFINED\n",line);err++;} } num=atoi(val); if(strcmp(op,"RESW")==0) lc=lc+(num*3); else if(strcmp(op,"RESB")==0) lc=lc+num; else if(strcmp(op,"BYTE")==0) { num=strlen(val)-3; lc=lc+num; for(i=2,j=0;i<strlen(val)-1;i++) { code[j]=val[i]; j++; } code[j]='\0'; fprintf(fp2,"^%s",code); ni++; } else lc=lc+3; if(strcmp(op,".")==0) break; } while(strcmp(op,"END")!=0&&(!feof(fp1))) {
fscanf(fp1,"%s\t%s\t%s",lab,op,val); line++; if(strcmp(op,"END")==0) break; if((strcmp(lab,"$")!=0)&&((strcmp(op,"RESW")!=0||strcmp(op,"RESB")!=0|| strcmp(op,"WORD")!=0||strcmp(op,"BYTE")==0))) { for(i=0;i<n;i++) { if(strcmp(lab,sym[i])==0) { f=1; break; } f=0; } if(f==0) { strcpy(sym[n],lab); symadd[n]=lc; fprintf(fp4,"\n%s\t%x",lab,lc); n++; } else{ fprintf(fp3,"\n%d\t\t|SYMBOL ALREADY DEFINED");err++;} } else if(strcmp(op,"RESW")==0||strcmp(op,"RESB")==0|| strcmp(op,"WORD")==0||strcmp(op,"BYTE")==0) fprintf(fp3,"\n%d\t\t|Declaration not allowed here",line); if(strcmp(op,"RESW")!=0&&strcmp(op,"RESB")!=0&&strcmp(op,"WORD")! =0&&strcmp(op,"BYTE")!=0) { for(i=0;i<q;i++) { if(strcmp(op,a[i])==0) { strcpy(code,b[i]); f1=0; break; } f1=1; } if(f1==1){ fprintf(fp3,"\n%d\t\t|WRONG OPCODE",line);err++;} for(i=0;i<n;i++) { if(strcmp(val,sym[i])==0) { address=symadd[i]; f=0; break;
} f=1; } if(f){ fprintf(fp3,"\n%d\t\t|UNDEFINED SYMBOL",line);err++;} } if(ni<10) { fprintf(fp2,"^%s%x",code,address); ni++; } else { fprintf(fp2,"T^00%x^",lc); if(m>10) { fprintf(fp2,"1E"); m=m-10; } else { fprintf(fp2,"%x",m*3); fprintf(fp2,"^%s%x",code,address); ni=0; } } lc=lc+3; } fprintf(fp2,"\nE^00%x",s); fprintf(fp3,"No of errors=%d\n--------------------------------------",err); printf("Output file:OBJCODE.DAT\nErrors are described in ERROR.DAT\nSymbol table is in the file:SYMTAB.DAT"); getch(); fcloseall(); }
OUTPUT:
INPUT.DAT COPY START 1000 RETADR RESW 1 BUFFER RESB 4 EOF BYTE C`EOF` $ . $ FIRST STA RETADR $ STL BUFFER $ J FIRST $ END START OUTPUT SCREEN: Output file:OBJCODE.DAT Errors are described in ERROR.DAT Symbol table is in the file:SYMTAB.DAT OBJCODE.DAT H^COPY^001000^18 T^001000^9^EOF^141000^321003^34100d E^001000 SYMTAB.DAT SYMBOL RETADR BUFFER EOF FIRST ERROR.DAT ------------------------------------LINE NO. |ERROR FOUND -------------------------------------No of errors=0 -------------------------------------ADDRESS 1000 1003 1007 100d
Ex.No: 05
PROGRAM:
/* TWO PASS MACRO PROCESSOR */
#include<stdio.h> #include<conio.h> #include<string.h> FILE *f; char var[25][10]; char s[25],str[50],v[25],tmp[25]; int j,c,i=0,l,t=0; struct deftab { char sym[25]; char xas[25]; }dt[50]; void variable() { fscanf(f,"%s",str); strcpy(dt[t++].xas,str); j=1;l=0; do { c=0; do { if(str[j]!='&') v[c++]=str[j]; j++; }while(str[j]!=','&&str[j]!='\0'); v[c]='\0'; strcpy(var[i],v); i++; }while(str[i++]!='\0'); } void main() { int cnt=0,m,n,mn; char res[25]; FILE *f1,*f2; f=fopen("msource.txt","r"); f1=fopen("deftab.txt","w+"); f2=fopen("namtab.txt","w+"); clrscr(); do { fscanf(f,"%s",str); while(strcmp(str,"MACRO")) { if(!strcmp(str,"END")) goto b; strcpy(s,str); fscanf(f,"%s",str); }
fprintf(f2,"%s\t%d",s,t); strcpy(dt[t].sym,s); variable(); fscanf(f,"%s",str); do { strcpy(dt[t].sym,str); fscanf(f,"%s",str); cnt=0; c=0; for(l=0;l<strlen(str);l++) { if(str[l]=='&') { cnt++; m=l; }} if(cnt) { for(l=m+1;l<strlen(str);l++) { tmp[c++]=str[l]; tmp[c]='\0'; for(n=0;n<i;n++) if(!strcmp(tmp,var[n])) { mn=n; goto a; } } a: c=0; for(n=0;n<m;n++) res[c++]=str[n]; res[c++]='?'; res[c++]=mn+49; for(n=l+1;n<strlen(str);n++) res[c++]=str[n]; res[c]='\0'; strcpy(dt[t++].xas,res); } else { strcpy(dt[t++].xas,str); } fscanf(f,"%s",str); }while(strcmp(str,"MEND")); fprintf(f2,"\t%d\n",t); strcpy(dt[t].sym,"MEND"); strcpy(dt[t++].xas,""); } while(!feof(f)); b: for(n=0;n<t;n++) fprintf(f1,"%s\t\t%s\n",dt[n].sym,dt[n].xas);
INPUT FILE:
MSOURCE.TXT: COPY START 0 RDBUFF MACRO &INDEV,&BUFADR,&RECLTH CLEAR X CLEAR A CLEAR S LDT #4096 TD =X'&INDEV' JEQ *-3 RD =X'&INDEV' COMPR A,S JEQ *+11 STCH &BUFADR,X TIXR T JLT *-19 STX &RECLTH MEND WRBUFF MACRO &OUTDEV,&BUFADR,&RECLTH CLEAR X LDT &RECLTH LDCH &BUFADR,X TD =X'&OUTDEV' JEQ *-3 WD =X'&OUTDEV' TIXR T JLT *-14 MEND FIRST STL RETADR CLOOP RDBUFF f1,BUFFER,LENGTH LDA LENGTH COMP #0 JEQ ENDFIL WRBUFF 05,BUFFER,LENGTH J CLOOP ENDFIL WRBUFF 05,EOF,THREE J @RETADR EOF BYTE C'EOF' THREEWORD 3 RETADR RESW 1 LENGTH RESW 1 BUFFER RESB 4096 END FIRST
OUPUT FILE:
DEFTAB.TXT: RDBUFF CLEAR CLEAR CLEAR LDT TD JEQ RD COMPR JEQ STCH TIXR JLT STX MEND WRBUFF CLEAR LDT LDCH TD JEQ WD TIXR JLT MEND &INDEV,&BUFADR,&RECLTH X A S #4096 =X'?1' *-3 =X'?1' A,S *+11 ?1 T *-19 ?1 &OUTDEV,&BUFADR,&RECLTH X ?1 ?1 =X'?I' *-3 =X'?I' T *-14
Ex.No: 06
6.
PROGRAM: /* C Program to implement SINGLE PASS MACRO PROCESSOR #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { int n,flag,i; char ilab[20],iopd[20],oper[20],NAMTAB[20][20]; FILE *fp1,*fp2,*DEFTAB; clrscr(); fp1=fopen("macroin.dat","r"); fp2=fopen("macroout.dat","w"); n=0; rewind(fp1); fscanf(fp1,"%s%s%s",ilab,iopd,oper); while(!feof(fp1)) { if(strcmp(iopd,"MACRO")==0) { strcpy(NAMTAB[n],ilab); DEFTAB=fopen(NAMTAB[n],"w"); fscanf(fp1,"%s%s%s",ilab,iopd,oper); while(strcmp(iopd,"MEND")!=0) { fprintf(DEFTAB,"%s\t%s\t%s\n",ilab,iopd,oper); fscanf(fp1,"%s%s%s",ilab,iopd,oper); } fclose(DEFTAB); n++; } else { flag=0; for(i=0;i<n;i++) { if(strcmp(iopd,NAMTAB[i])==0) { flag=1; DEFTAB=fopen(NAMTAB[i],"r"); fscanf(DEFTAB,"%s%s%s\n",ilab,iopd,oper); while(!feof(DEFTAB)) { fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper); fscanf(DEFTAB,"%s%s%s",ilab,iopd,oper); } break; */
OUTPUT: MACROIN.DAT M1 ** ** ** ** M2 ** ** ** ** M3 ** ** ** ** ** ** ** ** ** MACRO LDA ADD STA MEND MACRO LDA SUB STA MEND MACRO LDA MUL STA MEND START M3 M2 M1 END ** N1 N2 N3 ** ** N1 N2 N4 ** ** N1 N2 N5 ** 1000 ** ** ** **
MACROOUT.DAT ** ** ** ** ** ** ** ** ** ** ** START LDA MUL STA LDA SUB STA LDA ADD STA END 1000 N1 N2 N5 N1 N2 N4 N1 N2 N3 **
Ex.No: 07
ABSOLUTE LOADER
AIM: To implement an Absolute loader in C language. ALGORITHM: 1. Read the Header record 2. Verify program name and length 3. Read first Text record from the input file 4. Process the following steps until an End record is reached 5.1 If object code is in character form, convert it to internal hexadecimal representation 5.2 Move object codes to specified locations in memory 5.3 Write the starting location counter value of a block of object code and the corresponding internal representation to the output file 5.4 Read next Text record from the input file 5. Go to the address specified in End record 6. Close all the files and exit
PROGRAM: /* C Program to implement ABSOLUTE LOADER #include<stdio.h> #include<conio.h> #include<string.h> void main() { char input[10]; int start,length,address; FILE *fp1,*fp2; clrscr(); fp1=fopen("input.dat","r"); fp2=fopen("output.dat","w"); fscanf(fp1,"%s",input); while(strcmp(input,"E")!=0) { if(strcmp(input,"H")==0) { fscanf(fp1,"%d",&start); fscanf(fp1,"%d",&length); fscanf(fp1,"%s",input); } else if(strcmp(input,"T")==0) { fscanf(fp1,"%d",&address); fscanf(fp1,"%s",input); fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]); fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]); fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]); address+=3; fscanf(fp1,"%s",input); } else { fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]); fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]); fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]); address+=3; fscanf(fp1,"%s",input); } } fclose(fp1); fclose(fp2); printf("FINISHED"); getch(); } */
OUTPUT: INPUT.DAT: H 1000 232 T 1000 142033 483039 102036 T 2000 298300 230000 282030 302015 E OUTPUT.DAT: 1000 14 1001 20 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
Ex.No: 08
RELOCATING LOADER
AIM: To implement a Relocating loader in C language. ALGORITHM: 1. Enter the new starting location to which the object code has to be relocated 2. Read the content of the input file as strings one at a time 3. Transfer the strings from input file to output file, until T is encountered 4. Move the consecutive next three strings from input to output 5. Convert the current string, which is the relocation bit associated with each text record to binary form 6. Make the necessary changes in the corresponding words of object code by adding the new starting address with the address part of the object code for which the corresponding relocation bit is set and store the updated object code in the output 7. Move the object code for which the corresponding relocation bit is not set from output to input without change 8. Repeat steps from 2 to 7 until end record is encountered 9. If object code is in character form, convert it to internal hexadecimal representation 10. Move object codes to specified locations in memory 11. Write the starting location counter value of a block of object code and the corresponding internal hexadecimal representations to the output file
PROGRAM: /* C Program to implement RELOCATING LOADER #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { char add[6],length[10],input[10],bitmask[12],binary[12],relocbit; int start,len,inp,i,address,opcode,addr,actualadd; FILE *fp1,*fp2; clrscr(); printf("Enter the actual starting address: "); scanf("%d",&start); fp1=fopen("input.dat","r"); fp2=fopen("output.dat","w"); fscanf(fp1,"%s",input); while(strcmp(input,"E")!=0) { if(strcmp(input,"H")==0) { fscanf(fp1,"%s",add); fscanf(fp1,"%s",length); fscanf(fp1,"%s",input); } if(strcmp(input,"T")==0) { fscanf(fp1,"%d",&address); fscanf(fp1,"%s",bitmask); address+=start; len=strlen(bitmask); for(i=0;i<len;i++) { fscanf(fp1,"%d",&opcode); fscanf(fp1,"%d",&addr); relocbit=bitmask[i]; if(relocbit=='0') actualadd=addr; else actualadd=addr+start; fprintf(fp2,"%d\t%d%d\n",address,opcode,actualadd); address+=3; } fscanf(fp1,"%s",input); } } fclose(fp1); fclose(fp2); printf("FINISHED"); getch(); */
OUTPUT: INPUT.DAT: H 1000 200 T 1000 11001 14 1033 48 1039 90 1776 92 1765 57 1765 T 2011 11110 23 1838 43 1979 89 1060 66 1849 99 1477 E 1000 Enter the actual starting address: 7000 OUTPUT.DAT: 7000 147033 7003 487039 7006 901776 7009 921765 7012 577765 8011 237838 8014 437979 8017 897060 8020 667849 8023 991477
Ex.No: 09
PROGRAM: /* C Program to implement PASS ONE OF DIRECT LINKING LOADER #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct estab { char csect[10]; char sym_name[10]; int add,length; }table[10]; void main() { char input[10]; int i,count=0,start,length,loc; FILE *fp1,*fp2; clrscr(); fp1=fopen("linkin.dat","r"); fp2=fopen("linkout.dat","w"); printf("\nEnter the location where the program has to be located: "); scanf("%x",&start); fprintf(fp2,"CSect\tSym_Name\tAddress\t\tLength\n\n"); rewind(fp1); while(!feof(fp1)) { fscanf(fp1,"%s",input); if(strcmp(input,"H")==0) { fscanf(fp1,"%s",input); strcpy(table[count].csect,input); strcpy(table[count].sym_name,"**"); fscanf(fp1,"%s",input); table[count].add=atoi(input)+start; fscanf(fp1,"%x",&length); table[count++].length=length; fscanf(fp1,"%s",input); } if(strcmp(input,"D")==0) { fscanf(fp1,"%s%x",input,&loc); while(strcmp(input,"R")!=0) { strcpy(table[count].csect,"**"); strcpy(table[count].sym_name,input); table[count].add=loc+start; table[count++].length=0; fscanf(fp1,"%s%x",input,&loc); */
} while(strcmp(input,"T")!=0) fscanf(fp1,"%s",input); } if(strcmp(input,"T")==0) while(strcmp(input,"E")!=0) fscanf(fp1,"%s",input); fscanf(fp1,"%s",input); start=start+length; } for(i=0;i<count;i++) fprintf(fp2,"%s\t%s\t\t%x\t\t%x\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length); fcloseall(); getch(); }
OUTPUT: Enter the location where the program has to be located: 5075 LINKIN.DAT H D R T T M M M M E H D R T T M M M M M M E H D R T T M M M M M M E PROGA LISTA LISTB 000020 000054 000024 000054 000058 000064 000000 PROGB LISTB LISTA 000036 000070 000037 000044 000070 000074 000078 000082 000000 PROGC LISTC LISTA 000018 000042 000019 000023 000027 000048 000051 000054 000000 000000 000040 ENDB 10 16 05 06 06 06 000070 ENDA LISTC 032010 100014 +LISTB +LISTC +ENDC +ENDC 000054 ENDC 77100004 15100006
15001 00002F
100014
000088 ENDB LISTC 03100000 100000 +LISTA +ENDA +ENDA +ENDC +ENDC +ENDA 000057 ENDC LISTB 03100000 100030 +LISTA +LISTB +ENDA +LISTA +ENDA +LISTB
0510030 0510020
0510030
05100000 100011
100000
LINKOUT.DAT CSect PROGA ** ** PROGB ** ** PROGC ** ** Sym_Name ** LISTA ENDA ** LISTB ENDB ** LISTC ENDC Address 5075 50b5 50c9 50e5 5145 5155 516d 519d 51af Length 70 0 0 88 0 0 57 0 0
Ex.No: 10
PROGRAM: /* C Program to implement PASS TWO OF DIRECT LINKING LOADER #include<stdio.h> #include<conio.h> #include<string.h> struct input { char type[10]; char sname[20]; char add[20]; char len[20]; char type1[10]; char sname1[10]; char add1[10]; char len1[20]; }in; struct estab { char address[10]; char name[10]; }est; struct output { char code[20]; char op1; char op2; }out; int x,y,i,len,j,a,l; void main() { char stadd[10],ladd[10],nadd[10],tarr[10],madd[10]; FILE *f1,*f2,*f3; f1=fopen("inputa.dat","r"); f2=fopen("inputb.dat","r"); f3=fopen("output.dat","w"); while(1) { m: fscanf(f1,"%s%s%s%s",in.type,in.sname,in.add,in.len); if(strcmp(in.type,"END")==0) break; if(strcmp(in.type,"H")==0) */
{ while(!feof(f2)) { fscanf(f2,"%s%s",est.name,est.address); if(strcmp(in.sname,est.name)==0) { strcpy(stadd,est.address); break; } } rewind(f2); } if(strcmp(in.type,"T")==0) { x=atoi(in.sname)+atoi(stadd); itoa(x,ladd,10); strcpy(out.code,ladd); fscanf(f1,"%s%s%s%s",in.type1,in.sname1,in.add1,in.len1); y=atoi(in.sname1)+atoi(stadd); itoa(y,madd,10); while(!feof(f2)) { fscanf(f2,"%s%s",est.name,est.address); if(strcmp(est.name,in.len1)==0) strcpy(nadd,est.address); } rewind(f2); for(i=0;i<strlen(in.len);i=i+2) { if(strcmp(out.code,madd)==0) { l=atoi(in.add1); for(j=0;j<l;i++,j++) tarr[j]=in.len[i]; a=atoi(tarr)+atoi(nadd); itoa(a,tarr,10); i=(i-l); for(j=0;j<l;i++,j++) in.len[i]=tarr[j]; i=(i-l); } out.op1=in.len[i]; out.op2=in.len[i+1]; fprintf(f3,"%s\t%c%c\n",out.code,out.op1,out.op2); x=x+1; itoa(x,out.code,10); } } } getch();
INPUT: 1. FILE NAME: inputa.dat H D R T M E END PROGB LISTB LISTA 000033 000050 000075 000030 000039 09 06 000047 000041 LISTA -
2. FILE NAME: inputb.dat PROGA LISTA PROGB LISTB 004000 004040 004050 004070
Ex.No: 11
PROGRAM: /* SIMPLE TEXT EDITOR #include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> void newf(); void view(); void saveas(); void modify(); void main() { int op; clrscr(); do { printf("\nMENU\n"); printf("1.New\n"); printf("2.Open\n"); printf("3.Save As\n"); printf("4.Modify\n"); printf("5.Exit\n"); printf("Enter u'r choice: "); scanf("%d",&op); switch(op) { case 1: newf(); break; case 2: view(); break; case 3: saveas(); break; case 4: modify(); break; case 5: exit(0); default: printf("\nWrong choice!!!"); break; } }while(op!=5); } void newf() */
{ FILE *f; char fname[20],c; printf("\nEnter the Filename: "); scanf("%s",fname); printf("\nType the content and CTRL+Z to terminate:\n"); f=fopen(fname,"w"); rewind(f); while((c=getchar())!=EOF) { putc(c,f); } fclose(f); } void view() { FILE *f; char fname[20],c; printf("\nEnter the name of the file:"); scanf("%s",&fname); if(searchpath(fname)) { f=fopen(fname,"r"); while((c=getc(f))!=EOF) { printf("%c",c); } } else printf("\nThe file %s does not exist",fname); fclose(f); } void saveas() { FILE *f1,*f2; char c,sou[20],des[20]; printf("\nEnter the Source file name: "); scanf("%s",sou); if(searchpath(sou)) { printf("Enter the Destination file name: "); scanf("%s",des); f1=fopen(sou,"r"); f2=fopen(des,"w"); while((c=getc(f1))!=EOF) { putc(c,f2); } fclose(f1); fclose(f2); }
else printf("\nFile does not exist"); getch(); } void modify() { int ch1; FILE *f1; char c,*word,*sent,fname[20]; printf("Enter the filename to be modified: "); scanf("%s",fname); if(searchpath(fname)) { printf("\n1.Character"); printf("\n2.Word"); printf("\n3.Sentence"); printf("\nEnter U'r choice: "); scanf("%d",&ch1); if(ch1==1) { f1=fopen(fname,"a+"); fseek(f1, 0L, SEEK_END); printf("Enter the character and CTRL+Z to exit:\n "); while((c=getchar())!=EOF) { putc(c,f1); } } else if(ch1==2) { printf("Enter the word: "); scanf("%s",word); f1=fopen(fname,"a+"); fseek(f1, 0L, SEEK_END); fputs(word,f1); } else { printf("Enter the sentence and CTRL+Z to exit: "); f1=fopen(fname,"a+"); fseek(f1, 0L, SEEK_END); while((c=getchar())!=EOF) { putc(c,f1); } } } else printf("\nFilename does not exist"); fcloseall(); }
OUTPUT: MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice: 1 Enter the Filename: clang.txt Type the content and CTRL+Z to terminate: C is a procedure oriented language It includes 1. Arrays 2. String 3. Pointer etc ^Z MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice: 2 Enter the name of the file:vamp.txt The file vamp.txt does not exist MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice: 2 Enter the name of the file:clang.txt C is a procedure oriented language It includes 1. Arrays 2. String 3. Pointer etc MENU 1.New 2.Open
3.Save As 4.Modify 5.Exit Enter u'r choice: 3 Enter the Source file name: clang.txt Enter the Destination file name: cprogram.txt MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice: 4 Enter the filename to be modified: clang.txt 1.Character 2.Word 3.Sentence Enter U'r choice: 1 Enter the character and CTRL+Z to exit: S ^Z MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice: 4 Enter the filename to be modified: clang.txt 1.Character 2.Word 3.Sentence Enter U'r choice: 2 Enter the word: Template MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice: 4 Enter the filename to be modified: clang.txt 1.Character 2.Word
3.Sentence Enter U'r choice: 3 Enter the sentence and CTRL+Z to exit: IIIIIIIIIIIIII NNNNN KKKKKKKKK ^Z MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice: 2 Enter the name of the file:clang.txt C is a procedure oriented language It includes 1. Arrays 2. String 3. Pointer etc S Template IIIIIIIIIIIIII NNNNN KKKKKKKKK MENU 1.New 2.Open 3.Save As 4.Modify 5.Exit Enter u'r choice:5
RESULT: Thus a simple text editor with features like insertion/deletion of a character, word and sentence is implemented in C language.
Ex.No: 12
PROGRAM:
/*
*/
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #define MAX 11 char l[10]; struct symtab { int add; char label[10]; }sy[11]; void search(); void display(); void main() { int a[MAX],num,key,i,ch; char ans; int create(int); void lprob(int [],int,int); clrscr(); for(i=0;i<MAX;i++) a[i]=0; do { printf("\nEnter your choice:\n1.create a symbol table\n2.search\n3.exit"); scanf("%d",&ch); switch(ch) { case 1: do { printf("\nEnter the address:"); scanf("%d",&num); key=create(num); printf("\nEnter the lable:"); scanf("%s",l); lprob(a,key,num); printf("\nContinue(y/n)?"); ans=getche(); }while(ans=='y'); display(); break; case 2: search(); break; case 3: exit(0); break; }}while(ch<=2); getch(); } int create(int num)
{ int key=num%11; return key; } void lprob(int a[MAX],int key,int num) { int flag,i,count=0; flag=0; if(a[key]==0) { a[key]=num; sy[key].add=num; strcpy(sy[key].label,l); } else { i=0; while(i<MAX) {if(a[i]!=0) count++; i++; } if(count==MAX) { printf("\nHash Table is full"); display(); getch(); exit(1); } for(i=key+1;i<MAX;i++) if(a[i]==0) { a[i]=num; flag=1; sy[key].add=num; strcpy(sy[key].label,l); break; } for(i=0;i<key&&flag==0;i++) if(a[i]==0) { a[i]=num; flag=1; sy[key].add=num; strcpy(sy[key].label,l); break; }}} void display() { FILE *fp; int i; fp=fopen("symbal.txt","w"); printf("\nHash values\tAddress\tlable"); for(i=0;i<MAX;i++) { printf("\n%d\t%d\t%s",i,sy[i].add,sy[i].label);
fprintf(fp,"\n%d\t%d\t%s",i,sy[i].add,sy[i].label); } fclose(fp); } void search() { FILE *fp1; char la[10]; int set=0,s; int i,j; printf("\nEnter the lable:"); scanf("%s",la); fp1=fopen("symbal.txt","r"); for(i=0;i<MAX;i++) { fscanf(fp1,"%d%d",&j,&sy[i].add); if(sy[i].add!=0) fscanf(fp1,"%s",sy[i].label); } for(i=0;i<MAX;i++) { if(sy[i].add!=0) { if(strcmp(sy[i].label,la)==0) { set=1; s=sy[i].add; }}} if(set==1) printf("\nThe lable--%s--is present in symbol table at address:%d\n",la,s); else printf("\nThe lable is not present in symbol table"); }
OUTPUT: Enter your choice: 1.create a symbol table 2.search 3.exit1 Enter the address:1000 Enter the lable:ADD Continue(y/n)?y Enter the address:1500 Enter the lable:SUB Continue(y/n)?n Hash values Address lable 0 0 1 0 2 0 3 0 4 1500 SUB 5 0 6 0 7 0 8 0 9 0 10 1000 ADD Enter your choice: 1.create a symbol table 2.search 3.exit2 Enter the lable:ADD The lable--ADD--is present in symbol table at address:1000 Enter your choice: 1.create a symbol table 2.search 3.exit3
Ex.No: 13
LEXICAL ANALYSIS USING ARRAY AIM: To implement Lexical analysis using array in c ALOGRITHM: 1.Start the program 2.Declare character arrays to store the contents of the expressions 3.Accept the expressions from the user store it in an array 4.The expression is split up into token and it is being analyzed 5.Each token from the first to the last is checked and is being classified as a digit punctuation,special character,arithmetic operator,keywords and alphabets. 6.output is displayed for the given expression 7.stop the program.
PROGRAM: /*
#include<stdio.h> #include<conio.h> #include<ctype.h> void main() { int i,t=0,l=0; char a[100]; char e[100],op[]={'/','*','-','+'}; clrscr(); printf("\nEnter the expression;"); scanf("%s",&e); printf("\nDigit"); for(i=0;e[i]!='\0';i++) { if(isdigit(e[i])) { printf("\t%c",e[i]); } } printf("\nPunctuation"); for(i=0;e[i]!='\0';i++) { if(ispunct(e[i])) { if(e[i]!='+') printf("\t%c",e[i]); } } printf("\nArithematic operation"); for(i=0;e[i]!='\0';i++) { int j; for(j=0;op[j]!='\0';j++)
*/
{ if(op[j]==e[i]) { printf("\t%c",e[i]); } } } printf("\nKeywords"); for(i=0;e[i]!='\0';i++) { if(e[i]=='i'&&e[i+1]=='f') { l=l+1; printf("\tif"); } else if(e[i]=='v'&&e[i+1]=='o'&&e[i+2]=='i'&&e[i+3]=='d') { l=l+1; printf("\t void"); } else if(e[i]=='f'&&e[i+1]=='0'&&e[i+2]=='r') { l=l+1; printf("\t for");\ } else if(e[i]=='i'&&e[i+1]=='n'&&e[i+2]=='t') { l=l+1; printf("\t int"); } else if(l==0) { if(isalpha(e[i])) { a[t]=e[i]; t++;
OPUTPUT: Enter the expression:for<i=0;i<10;i++); Digit 0 1 < for f o r i i i_ 0 = + ; + < ; > ; Punctuation Arithematic operation Keywords Alphabet
RESULT:
Ex.No: 14 CODE GENERATOR AIM: To write a program to the generate machine code for the given intermediate code in c ALOGRITHM: 1.Start the program 2.Accept the input string from the user 3.Intermediate code is given as input and machine code is generated as output 4.The code along with the symbol table information is used to determine the runtime address of the data objects. 5.Using switch case perform the appropriate operation 6.Registers are initialized to store the variables 7.Display the machine code that is generated for the given intermediate code. 8.stop the program.
PROGRAM: /* #include<stdio.h> #include<conio.h> #include<ctype.h> int z=-1,i1,i,j=0,q1=-1,g; int r=0,r1,r2; int num1[]={1,2,3,4,5,6,7,8,8,}; char op1,op2[]={'/','*','+','-'}; char expr[25],num[]={'0','1','2','3','4','5','6','7','8','9'},temp[25]; char ex[10][5]; int ab[20]; void modify(int x,int y) { ex[z][0]=num[z]; ex[z][1]='='; ex[z][2]=temp[x]; ex[z][3]=op1; ex[z][4]=temp[y]; temp[x]=num[z]; for(i1=y-1;temp[i1]!=NULL;i1++) temp[i1]=temp[i1+3]; printf("\n"); for(i=0;ex[z][i]!=NULL;i++) { if(isdigit(ex[z][i])) printf("t%c",ex[z][i]); else printf("%c",ex[z][i]); } } CODE GENERATOR */
void main() { clrscr(); for(i=0;i<25;i++) { expr[i]=NULL; temp[i]=NULL; ab[i]=NULL; } printf("\n Enter the expression:"); scanf("%s",&expr); for(i=0;expr[i]!=NULL;i++) temp[i]=expr[i]; printf("\nTHREE ADDRESS CODE GENERATION\n"); do { E: op1=op2[j]; for(i=2;temp[i]!=NULL;i++) if(temp[i]==op1) { z++; modify(i-1,i+1); goto E; } j++; } while(j<4); printf("\n%c=t%c",expr[0],num[z]); j=0; printf("\n\n CODE GENERATION"); for(i=0;i<=z;i++) {
printf("\n"); if(!isdigit(ex[i][2])) { q1++; printf("\nMOV %c,R %d",ex[i][2],num1[q1]); ab[i]=num1[q1]; } else { g=(int)ex[i][2]-48; ab[i]=ab[g]; } if(!isdigit(ex[i][4])) { q1++; printf("\nMOV %c,R %d",ex[i][2],num1[q1]); r1=num1[q1]; } else { g=(int)ex[i][4]-48; r1=ab[g]; switch(ex[i][3]) { case'*': printf("\nMUL"); break; case'/': printf("\nDIV"); break; case'+': printf("\nADD"); break; case'-':
OUTPUT:
Enter the expression:a=b+c*d/e THREE ADDRESS CODE GENERATION t0=d/e t1=c*t0 t2=b+t1 a=t2 CODE GENERATION MOV d ,R1 MOV e ,R2 DIV R2 ,R1 MOV c MOV B ,R3 ,R4
RESULT: