PASS ONE OF A TWO PASS ASSEMBLER | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

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             **

No comments:

Post a Comment

Thanks for your valuable comments

Followers