You are on page 1of 23

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

h> struct table { char var[10]; int value; }; struct table tb[20]; int i,j,n; void create(); void modify(); int search(char variable[],int n); void insert(); void display(); void main() { int ch,result=0; char v[10]; clrscr(); do { printf("\n1.Create \n2.Insert \n3.Modify \n4.Search \n5.Display \n6.Exit"); printf(Enter your choice: ); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: insert(); break; case 3: modify(); break; case 4: printf("Enter the variable to be searched"); scanf("%s",&v); result=search(v,n); if(result<0) printf("The variable doesnot belong to the table"); else printf("The location of variable is %d,\n the value of %s d",i,tb[i].var,tb[i].value); break; case 5: display(); break; case 6:

exit(0); } } while(ch!=6); getch(); } void create() { printf("Enter the no. of entries"); scanf("%d",&n); printf("\nEnter the variable and value"); for(i=0;i<n;i++) { scanf("%s \n%d",tb[i].var,&tb[i].value); check: if(tb[i].var[0]>='0'&&tb[i].var[0]<='9') { printf("The variable should start with an alphabet \n Enter correct variable name:"); scanf("%s%d",tb[i].var,&tb[i].value); goto check; } check1: for(j=0;j<i;j++) { if(strcmp(tb[i].var,tb[j].var)==0) { printf("The variable already exists \n Enter another variable :\n"); scanf("%s%d",tb[i].var,&tb[i].value); goto check1; } } } printf("The table after creation is:\n"); display(); } void insert() { if(i>=20) printf("cannot insert table is full\n"); else { n++; printf("Enter the value and variable"); scanf("%s%d",tb[i].var,&tb[i].value); check: if(tb[i].var[0]>='0'&&tb[i].var[0]<='9') { printf("The variable should start with an alphabet \n enter the correct variable name"); scanf("%s%d",tb[i].var,&tb[i].value);

goto check; } check1: for(j=0;j<1;j++) { if(strcmp(tb[i].var,tb[j].var)==0) { printf("The variable already exists \n enter the another variable\n"); scanf("%s%d",tb[i].var,&tb[i].value); goto check1; } } printf("The table after insert is:\n"); display(); } } void modify() { char variable[10]; int result=0; printf("enter the variabel to be modified"); scanf("%s",variable); result=search(variable,n); if(result==0) printf("%s doesnot belong to the table",variable); else { printf("the correct value of variable %s is %d\n, tb[result].var,tb[result].value); printf(enter new variable and its value\n") scanf("%s%d",tb[i].var,&tb[i].value); check: if(tb[i].var[0]>='0'&&tb[i].var[0]<='9') { printf("the variable should not start with an alphabet \n enter the correct variable name"); scanf("%s%d",tb[i].var,&tb[i].value); goto check; } } printf("the table after modification is"); display(); } int search(char variable[],int n) { int flag; for(i=0;i<n;i++) if(strcmp(tb[i].var,variable)==0) { flag=1; break;

} if(flag==1) return 1; else return 0; } void display() { printf("variable \t value \n"); for(i=0;i<n;i++) printf("%s\t\t%d\n",tb[i].var,tb[i].value); }

OUTPUT:

1.Create 2.Insert 3.Modify 4.Search 5.Display 6.Exit Enter your choice:1 Enter the no. of entries: 2 Enter the variable and value A 1 S 8 1.Create 2.Insert 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 2 Enter the variable and value C 3 The table after insert is Variable value A 1 S 8 C 3 1.Create 2.Insert 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 3 Enter the variable to be modified S The correct value of variable S is 8 Enter the new variable and its value J 26 The table after modification is

Variable

value

A J C 1.Create 2.Insert 3.Modify 4.Search 5.Display 6.Exit

1 26 3

Enter your choice: 4 Enter the variable to be searched J The value of J is 26 Enter your choice 1.Create 2.Insert 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 5 Variable A J C 1.Create 2.Insert 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 6 value 1 26 3

PROGRAM:

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { char opcode[10],mnemonic[10],operand[10],label[10]; int locctr,start,length; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("input.dat","r"); fp2=fopen("symbol.dat","w"); fp3=fopen("out.dat","w"); fp4=fopen("optab.dat","r"); 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",mnemonic); rewind(fp4); while(strcmp(mnemonic,"END")!=0) { if(strcmp(opcode,mnemonic)==0) { locctr+=3; break; } fscanf(fp4,"%s",mnemonic); } if(strcmp(opcode,"WORD")==0) locctr+=3; else if(strcmp(opcode,"RESW")==0) locctr+=(3*atoi(operand)); else if(strcmp(opcode,"RESB")==0) locctr+=(3*atoi(operand)); else if(strcmp(opcode,"BYTE")==0) ++locctr; fprintf(fp3,"%s\t%s\t%s\t\n",label,opcode,operand);

fscanf(fp1,"%s%s%s",label,opcode,operand); } length=locctr-start; printf("The length of the program is %d",length); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); }

FILE INPUT:\

/* INPUT.DAT */ MAIN BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** START LDA STA STCH STCH WORD RESW BYTE RESB END 5000 NUM1 NUM2 CHAR1 CHAR2 5 1 5 1 BEGIN

/* OPTAB.DAT */ START LDA STA LDCH STCH END * 00 0C 50 54 *

OUTPUT FILES: /* SYMBOL.DAT */ BEGIN5000 NUM1 5012 NUM2 5015 CHAR1 5018 CHAR2 5019 /* OUT.DAT */ MAIN 5000 5003 5006 5009 5012 5015 5018 5019 START BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 5000 LDA STA STCH STCH WORD RESW BYTE RESB NUM1 NUM2 CHAR1 CHAR2 5 1 5 1

OUTPUT: The length of the program is 20

PROGRAM:

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { char symbol[20],opcode[20],mnemonic[20],label[20],code[20],operand[20], character,add[20],objectcode[20]; int locctr,flag,flag1,loc; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("out.dat","r"); fp2=fopen("twoout.dat","w"); fp3=fopen("optab.dat","r"); fp4=fopen("symbol.dat","r"); fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { fprintf(fp2,"\t%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand); } while(strcmp(opcode,"END")!=0) { flag=0; rewind(fp3); fscanf(fp3,"%s%s",mnemonic,code); while(strcmp(mnemonic,"END")!=0) { if((strcmp(opcode,mnemonic)==0)&&(strcmp(code,"*")!=0)) { flag=1; break; } fscanf(fp3,"%s%s",mnemonic,code); } if(flag==1) { flag1=0; rewind(fp4); while(!feof(fp4)) { fscanf(fp4,"%s%d",symbol,&loc); if(strcmp(symbol,operand)==0) { flag1=1; break; } } if(flag1==1)

{ itoa(loc,add,10); strcpy(objectcode,strcat(code,add)); } } else if(strcmp(opcode,"BYTE")==0||strcmp(opcode,"WORD")==0) { if(operand[0]=='C'||operand[0]=='X') { character=operand[2]; itoa(character,add,16); strcpy(objectcode,add); } else { itoa(atoi(operand),add,10); strcpy(objectcode,add); } } else strcpy(objectcode,'\0'); fprintf(fp2,"%d\t%s\t%s\t%s\t%s\n",locctr,label,opcode,operand,objectcode); fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand); } fprintf(fp2,"\t%s\t%s\t%s\n",label,opcode,operand); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); }

INPUT FILES:

/* OUT.DAT */ MAIN 5000 5003 5006 5009 5012 5015 5018 5019 START BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 5000 LDA STA STCH STCH WORD RESW BYTE RESB NUM1 NUM2 CHAR1 CHAR2 5 1 5 1

/* OPTAB.DAT */ START LDA STA LDCH STCH END * 00 0C 50 54 *

/* SYMBOL.DAT */ BEGIN5000 NUM1 5012 NUM2 5015 CHAR1 5018 CHAR2 5019

OUTPUT:

/*TWOOUT.DAT*/ MAIN 5000 5003 5006 5009 5012 5015 5018 5019 5020 START BEGIN ** ** ** NUM1 NUM2 CHAR1 CHAR2 ** 5000 LDA STA STCH STCH WORD RESW BYTE RESB 1 END NUM1 005012 NUM2 0C5015 CHAR1 545018 CHAR2 545019 5 5 1 1 5 5 1 **

PROGRAM:

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #define MAX 20 struct input { char opcode[10],mnemonic[10],operand[10],label[10]; int loc; }table[MAX]; struct symtab { char sym[10]; int f,val,ref; }symtbl[MAX]; void main() { int f1,i=1,j=1,flag,locctr,x; char add[10],code[10],mnemcode[10]; FILE *fp1,*fp2,*fp3; clrscr(); fp1=fopen("input.dat","r"); fp2=fopen("optab.dat","r"); fp3=fopen("spout.dat","w"); fscanf(fp1,"%s%s%s",table[i].label,table[i].opcode,table[i].operand); if(strcmp(table[i].opcode,"START")==0) { locctr=atoi(table[i].operand); i++; fscanf(fp1,"%s%s%s",table[i].label,table[i].opcode,table[i].operand); } else locctr=0; while(strcmp(table[i].opcode,"END")!=0) { if(strcmp(table[i].label,"**")!=0) { for(x=1;x<j;x++) { f1=0; if((strcmp(symtbl[x].sym,table[i].label)==0)&&(symtbl[x].f==1)) { symtbl[x].val=locctr; symtbl[x].f=0; table[symtbl[x].ref].loc=locctr; f1=1; break; } }

if(f1==0) { strcpy(symtbl[j].sym,table[i].label); symtbl[j].val=locctr; symtbl[j].f=0; j++; } } fscanf(fp2,"%s%s",code,mnemcode); while(strcmp(code,"END")!=0) { if(strcmp(table[i].opcode,code)==0) { strcpy(table[i].mnemonic,mnemcode); locctr+=3; for(x=1;x<=j;x++) { flag=0; if(strcmp(table[i].operand,symtbl[x].sym)==0) { flag=1; if(symtbl[x].f==0) table[i].loc=symtbl[x].val; break; } } if(flag!=1) { strcpy(symtbl[j].sym,table[i].operand); symtbl[j].f=1; symtbl[j].ref=i; j++; } } fscanf(fp2,"%s%s",code,mnemcode); } rewind(fp2); if(strcmp(table[i].opcode,"WORD")==0) { locctr+=3; strcpy(table[i].mnemonic,'\0'); table[i].loc=atoi(table[i].operand); } else if(strcmp(table[i].opcode,"RESW")==0) { locctr+=(3*(atoi(table[i].operand))); strcpy(table[i].mnemonic,'\0'); table[i].loc=atoi('\0'); } else if(strcmp(table[i].opcode,"RESB")==0)

{ locctr+=(atoi(table[i].operand)); strcpy(table[i].mnemonic,'\0'); table[i].loc=atoi('\0'); } else if(strcmp(table[i].opcode,"BYTE")==0) { ++locctr; if((table[i].operand[0]=='c')||(table[i].operand[0]=='x')) table[i].loc=(int)table[i].operand[2]; else table[i].loc=locctr; } i++; fscanf(fp1,"%s%s%s",table[i].label,table[i].opcode,table[i].operand); } for(x=1;x<=i;x++) fprintf(fp3,"%s\t%s\t%s\t%s\n",table[x].label,table[x].opcode,table[x].operand,strcat(table[x]. mnemonic,itoa(table[x].loc,add,10))); for(x=1;x<j;x++) printf("%s\t%d\n",symtbl[x].sym,symtbl[x].val); getch(); }

INPUT FILES:

/*INPUT.DAT*/ ** ** ** ** ** ALPHA FIVE CHARZ C1 ** START LDA ADD SUB STA RESW RESW WORD RESW END 2000 FIVE ALPHA CHARZ C1 1 1 1 1

/*OPTAB.DAT*/ ADD 18 ADDR 90 SUB 1C SUBR 94 MUL 20 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 3C JSUB 48 RSUB 4C JEQ 30 JLT 38 JGT 34 START * END *

OUTPUT:

/*SPOUT.DAT*/ ** ** ** ** ** ALPHA FIVE CHARZ C1 ** START LDA FIVE ADD SUB STA RESW RESW WORD RESW END ** 2000 002015 ALPHA 182012 CHARZ 1C2018 C1 0C2021 1 0 1 0 1 1 1 0 0

PROGRAM:

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { int n,i,flag; 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",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; } } if(flag==0) fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);

} fscanf(fp1,"%s%s%s",ilab,iopd,oper); } getch(); }

INPUT FILE:

/*MACROIN.DAT*/ M1 ** ** ** ** M2 ** ** ** ** M3 ** ** ** ** ** ** ** ** ** MACRO ** LDA N1 ADD N2 STA N3 MEND ** MACRO ** LDA N1 SUB N2 STA N4 MEND ** MACRO ** LDA N1 MUL N2 STA N5 MEND ** START 1000 M3 ** M2 ** M1 ** END **

OUTPUT:

/*MACROOUT.DAT*/ ** ** ** ** ** ** ** ** ** ** ** START LDA MUL STA LDA SUB STA LDA ADD STA END 1000 N1 N2 N5 N1 N2 N4 N1 N2 N3 **

You might also like