Untitled

 avatar
unknown
plain_text
3 years ago
6.6 kB
3
Indexable
Pass -1 of 2-pass assembler
 
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *f1,*f2,*f3,*f4;
int lc,sa,l,op1,o,len;
char m1[20],la[20],op[20],otp[20];
clrscr();
f1=fopen("input.txt","r");
f3=fopen("symtab.txt","w");
fscanf(f1,"%s %s %d",la,m1,&op1);
if(strcmp(m1,"START")==0)
{
 sa=op1;
 lc=sa;
 printf("\t%s\t%s\t%d\n",la,m1,op1);
 }
 else
 lc=0;
fscanf(f1,"%s %s",la,m1);
while(!feof(f1))
{
 fscanf(f1,"%s",op);
 printf("\n%d\t%s\t%s\t%s\n",lc,la,m1,op);
 if(strcmp(la,"-")!=0)
 {
 fprintf(f3,"\n%d\t%s\n",lc,la);
 }
 f2=fopen("optab.txt","r");
 fscanf(f2,"%s %d",otp,&o);
 while(!feof(f2))
 {
  if(strcmp(m1,otp)==0)
  {
    lc=lc+3;
    break;
  }
  fscanf(f2,"%s %d",otp,&o);
  }
  fclose(f2);
  if(strcmp(m1,"WORD")==0)
 
    {
   lc=lc+3;
   }
   else if(strcmp(m1,"RESW")==0)
   {
    op1=atoi(op);
    lc=lc+(3*op1);
    }
    else if(strcmp(m1,"BYTE")==0)
    {
    if(op[0]=='X')
      lc=lc+1;
      else
      {
      len=strlen(op)-2;
      lc=lc+len;}
    }
    else if(strcmp(m1,"RESB")==0)
    {
     op1=atoi(op);
     lc=lc+op1;
     }
    fscanf(f1,"%s%s",la,m1);
    }
    if(strcmp(m1,"END")==0)
    {
    printf("Program length =\n%d",lc-sa);
    }
    fclose(f1);
    fclose(f3);
    getch();
}




*****************************************





#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFER_SIZE 1024
 
struct input{
    char label[10];
    char opcode[10];
    char oprand[10];
}store[22];

struct optab{
    char op[5];
    char val[5];
}object[20];

int checkOpcode(char* word){
    FILE* file = fopen("optab.txt", "r");
    char *token;
    char buffer[BUFFER_SIZE];
    const char *delimiter_characters = " \n\t}{][)(,;";

    while(fgets(buffer, BUFFER_SIZE, file) != NULL){
        token = strtok(buffer, delimiter_characters); 
        while (token != NULL)
        {
            if (strcmp(buffer, word) == 0){
                return 1;    
            }
            token = strtok(NULL, delimiter_characters);
        }
    }
    fclose(file);
    return 0;
}

int main()
{
    char *last_token;
    char buffer[BUFFER_SIZE];
    int col=0,index=-1,ind=0,indo=4096,counter=4096;
    const char *inpFile = "SIC_input.txt";
    const char *outFile = "SIC_output.txt";
    const char *symFile = "SIC_Symbol.txt";
    const char *objFile = "Object_Code.txt";
    const char *delimiter_characters = " \n\t}{][)(,;";
    FILE *fileptr1 = fopen(inpFile,"r");
    FILE *fileptr2 = fopen(outFile,"w");
    FILE *fileptr3 = fopen(symFile,"w");
    FILE *fileptr4 = fopen(objFile,"r");
 
    // Task 1 - tokenization & Task 2 - Storing in Structure
    printf("PASS - 1\n\n");
    printf("Task 1 & 2 :\n");
    int count=0;
    while(fgets(buffer, BUFFER_SIZE, fileptr1) != NULL)
    {
        index++;
        last_token = strtok(buffer, delimiter_characters); 
        while (last_token != NULL)
        {
            if(col==0) strcpy(store[index].label,last_token);
            else if(col==1) strcpy(store[index].opcode,last_token);
            else strcpy(store[index].oprand,last_token);  
            last_token = strtok(NULL, delimiter_characters);
            col++;
        }
        col=0;
        count++;
    }
    printf("Stored in Structure after Tokenising.\n\n");
 
    // PASS 2
    
    char* op[20] = {"ADD","COMP","J","JEQ","JGT","JLT","JSUB","LDA", "LDB", "LDCH","LDL","LDX","MUL","RD","RSUB","STA","STCH","STL","STX","TD"};
    char* val[20] = {"18","28","3C","30","34","38","48","00","68","50","08","04","20","D8","4C","0C","54","14","10","E0"};
 
    printf("Task 3 & 4 & 5 : Creating SIC_Output.txt and SIC_Symbol.txt file ...\n");
    for(int i=0;i<count;i++)
    {
        if(strcmp(store[i].label,"END") == 0){
            break;
        }
        if(checkOpcode(store[i].label)){
            indo+=3;
        }else if(strcmp(store[i].opcode,"WORD") == 0){
            indo+=3;
        }else if(strcmp(store[i].opcode,"RESW") == 0){
            indo+=atoi(store[i].oprand)*3;
        }else if(strcmp(store[i].opcode,"RESB") == 0){
            indo+=atoi(store[i].oprand);
        }else if(strcmp(store[i].opcode,"BYTE") == 0){
            if(store[i].oprand[0]=='C')
            {
                int temp = strlen(store[i].oprand);
                temp=temp-3;
                indo+=temp;
            }
            else if(store[i].oprand[0]=='X')
            {
                int temp = strlen(store[i].oprand);
                temp=temp-3;
                indo+=(temp/2);
            }
        }
       
        if(i==0)
        { 
            if(strlen(store[i].opcode) != 0){
                fprintf(fileptr2,"%X %s %s %s ",indo,store[i].label,store[i].opcode,store[i].oprand);
                char *obj = "";
                for(int j=0; j<20; j++){
                    if(strcmp(store[i].opcode,object[j].op) == 0){
                        obj = "";
                        strcat(obj,object[j].val);
                        char snum[5];
                        itoa(indo,snum,10);
                        strcat(obj,snum);
                        fprintf(fileptr2,"%s\n",obj);
                        obj = "";
                        break;
                    }
                }
                if(strlen(store[i].label) != 0 && strcmp(store[i].label, store[i-1].label)!=0){
                    fprintf(fileptr3,"%X\t%s\n",indo,store[i].label);
                }
            }

        }
        else if(strlen(store[i].oprand) != 0 && i!=0)
        {
            fprintf(fileptr2,"%X %s %s %s\n",indo,store[i].label,store[i].opcode,store[i].oprand);
            if(strlen(store[i].label) != 0 && strcmp(store[i].label, store[i-1].label)!=0){
                    fprintf(fileptr3,"%X\t%s\n",indo,store[i].label);
                } 
        }
        else
        {
            if(strlen(store[i].opcode) != 0){
                fprintf(fileptr2,"%X %s %s %s\n",indo,store[i].label,store[i].opcode,store[i].oprand);
                if(strlen(store[i].label) != 0 && strcmp(store[i].label, store[i-1].label)!=0){
                    fprintf(fileptr3,"%X\t%s\n",indo,store[i].label);
                }
            }
        }
    }
         
    fprintf(fileptr2,"%d",indo-counter);
    printf("\nGenerated...");
 
    fclose(fileptr1);
    fclose(fileptr2);
    fclose(fileptr3);

    printf("\n\nPASS - 2");
    printf("\nGenerated...");
    
    return 0;
}