URDINESH: C Programming samples with out put
Showing posts with label C Programming samples with out put. Show all posts
Showing posts with label C Programming samples with out put. Show all posts

Wednesday, May 7, 2014

PASS ONE OF A TWO PASS ASSEMBLER

PASS ONE OF A TWO PASS ASSEMBLER
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char opcode[10],mnemonic[3],operand[10],label[10],code[10];
            int locctr,start,length;
            FILE *fp1,*fp2,*fp3,*fp4;
            clrscr();
            printf("\nPass One Of A Two Pass Assembler:");
            printf("\n----------------------------------\n\n");
            fp1=fopen("input.txt","r");
            fp2=fopen("symtb1.txt","w");
            fp3=fopen("out.txt","w");
            fp4=fopen("optab.txt","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%s",code,mnemonic);
              while(strcmp(code,"END")!=0)
              {
                if(strcmp(opcode,code)==0)
                {
                  locctr+=3;  break;
                }
                fscanf(fp4,"%s%s",code,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+=(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\t%s\n",locctr,label,opcode,operand);
            length=locctr-start;
            printf("\nThe Length Of The Program Is...%d",length);
            fclose(fp1);
            fclose(fp2);
            fclose(fp3);
            fclose(fp4);
            printf("\nProgram Completed Successfully...\n");
            getch();
}




OUTPUT:

Pass One Of A Two Pass Assembler:
--------------------------------------------
The Length Of The Program Is...18
Program Completed Successfully...

INPUT FILES:

INPUT.TXT

**            START           1000
**            LDA               VAR1
**            MUL              VAR2
**            STA                ANS
VAR1     WORD           11
VAR2     WORD           12
ANS        RESW            1
**            END               **

OPTAB.TXT

START            *
ADD              18
DIV                24
LDA               00
LDCH            50
MUL              20
STA                0C
STCH             54
SUB               1C
END               *


OUTPUT FILES:

SYMTB1.TXT

VAR1      1009
VAR2      1012
ANS         1015


OUT.TXT

                **           START         1000
1000        **           LDA             VAR1
1003        **           MUL            VAR2
1006       **           STA              ANS
1009        VAR1    WORD         11
1012        VAR2    WORD         12
1015        ANS       RESW          1

1018        **           END             **

TOKEN SEPARATION Program In C

TOKEN SEPARATION
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
FILE *fp;
char kw[25][10]={"include","int","void","float","char","if","else","while","switch"};
char optr[15]={'+','-','*','/','%','='};
char sym[20]={'{','}','[',']','.',',','"','<','>',':',';'};
char dt[5][8]={"int","float","char"};
char fname[10],c[10],x,y; int i;
void main()
{
            int ch,op;
            void display(); void operators();
            void constants(); void specialsymbols(); clrscr();
            printf("\nToken Separation:\n");
            printf("-----------------\n\n");
printf("Menus:\n\n1.Display Program\n2.Display Keywords\n3.Display Variables\n4.Display Operators\n5.Display Constants\n6.Display Special Symbols\n7.Quit\n\n");
            label:
            printf("Enter your choice: ");
            scanf("%d",&ch);
            switch(ch)
            {
              case 1:
                display(); break;
              case 2:
                printf("\nEnter the file name with extension : ");
                scanf("%s",fname);
                fp=fopen(fname,"r");
                if(fp==NULL)  printf("ERROR...");
                else
                {
                  do
                  {
                   fscanf(fp,"%s",&c);
                   for(i=0;i<9;i++)
                   {
                        if(strcmp(c,kw[i])==0)   printf("%s  ",c);
                   }
                  }while(!feof(fp));
                }
                fclose(fp);
                break;
              case 3:
                printf("\nEnter the file name with extension : ");
                scanf("%s",fname);
                fp=fopen(fname,"r");
                if(fp==NULL)  printf("ERROR...");
                else
                {
                  do
                  {
                        fscanf(fp,"%s",&c);
                        for(i=0;i<3;i++)
                        {
                          if(strcmp(c,dt[i])==0)
                          {
                            while((x=fgetc(fp))!=';')
                              printf("%c",x);
                            break;
                          }
                        }
                  }while(!feof(fp));
                }
                fclose(fp);
                break;
              case 4:
                operators(); break;
              case 5:
                constants(); break;
              case 6:
                specialsymbols(); break;
              case 7:
                exit(0); break;
              default:
                printf("Invalid Option...Try Again...\n"); break;
            }
            printf("\nDo you want to continue (1/0): ");
            scanf("%d",&op);
            if(op==1)
              goto label;
            else
              exit(0);
            getch();
}
void display()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              fscanf(fp,"%c",&y);
              while(!feof(fp))
              {
                printf("%c",y); fscanf(fp,"%c",&y);
              }
            }
            fclose(fp);
}
void operators()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              while(fread(&y,sizeof(char),1,fp))
              {
                for(i=0;i<6;i++)
                  if(y==optr[i])   printf("%c,",y);
              }
            }
            fclose(fp);
}
void constants()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              while(fread(&y,sizeof(char),1,fp))
              {
                if(isdigit(y))  printf("%c,",y);
              }
            }
            fclose(fp);
}
void specialsymbols()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              while(fread(&y,sizeof(char),1,fp))
              {
                for(i=0;i<11;i++)
                  if(y==sym[i])  printf("%c,",y);
              }
            }
            fclose(fp);
}



OUTPUT:


Token Separation:
-----------------------
Menus:
1.Display Program
2.Display Keywords
3.Display Variables
4.Display Operators
5.Display Constants
6.Display Special Symbols
7.Quit

Enter your choice: 1
Enter the file name with extension : add.c
//Addition
#include<stdio.h>
#include<conio.h>
void main()
{
        int c; float x,y; char s[10],a;
        clrscr();
        c=123+456;
        printf("Added value is...%d",c);
        getch();
}
Do you want to continue (1/0): 1

Enter your choice: 2
Enter the file name with extension : add.c
void  int  float  char
Do you want to continue (1/0): 1

Enter your choice: 3
Enter the file name with extension : add.c
 c x,y s[10],a
Do you want to continue (1/0): 1

Enter your choice: 4
Enter the file name with extension : add.c
/,/,=,+,%,
Do you want to continue (1/0): 1

Enter your choice: 5
Enter the file name with extension : add.c
1,0,1,2,3,4,5,6,
Do you want to continue (1/0): 1

Enter your choice: 6
Enter the file name with extension : add.c
<,.,>,<,.,>,{,;,,,;,[,],,,;,;,;,",.,.,.,",,,;,;,},
Do you want to continue (1/0): 1

Enter your choice: 7

Followers