URDINESH: Assembler in C
Showing posts with label Assembler in C. Show all posts
Showing posts with label Assembler in C. Show all posts

Wednesday, May 7, 2014

IMPLEMENTATION OF PASS OF A DIRECT LINKING LOADER

IMPLEMENTATION OF PASS OF A DIRECT LINKING LOADER

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct estab
{
   char csect[10],sym_name[10];
   long int add;
   int length;
}table[MAX];
void main()
{
   FILE *fp1,*fp2;
   char in[10];
   long int i,count=0,start,len,loc;
   clrscr();
   printf("\nImplementation Of Pass1 Of A Direct Linking Loader:");
   printf("\n---------------------------------------------------\n\n");
   fp1=fopen("link1in.txt","r");
   fp2=fopen("link1out.txt","w");
   printf("Enter the Location where the program has to be loaded: ");
   scanf("%lx",&start);
   fprintf(fp2,"CSECT\tSYM_NAME\tADDRESS\tLENGTH\n");
   rewind(fp1);
   while(!feof(fp1))
   {
     fscanf(fp1,"%s",in);
     if(strcmp(in,"H")==0)
     {
       fscanf(fp1,"%s",in);
       strcpy(table[count].csect,in);
       strcpy(table[count].sym_name,"\0");
       fscanf(fp1,"%s",in);
       table[count].add=atoi(in)+start;
       fscanf(fp1,"%s",in);
       len=atoi(in);
       table[count++].length=atoi(in);
       fscanf(fp1,"%s",in);
     }
     if(strcmp(in,"D")==0)
     {
       fscanf(fp1,"%s%lx",in,&loc);
       while(strcmp(in,"R")!=0)
       {
             strcpy(table[count].csect,"\0");
             strcpy(table[count].sym_name,in);
             table[count].add=loc+start;
             table[count++].length=0;
             fscanf(fp1,"%s%lx",in,&loc);
       }
       while(strcmp(in,"T")!=0)
             fscanf(fp1,"%s",in);
     }
     if(strcmp(in,"T")==0)
     while(strcmp(in,"E")!=0)
     fscanf(fp1,"%s",in);
     fscanf(fp1,"%s",in);
     start=start+len;
   }
   for(i=0;i<count;i++)
   fprintf(fp2,"%s\t%s\t%lx\t%d\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length);
   printf("\nProgram Completed Successfully...\n");
   getch();
}







OUTPUT:


Implementation Of Pass1 Of A Direct Linking Loader:
-------------------------------------------------------------------

Enter the Location where the program has to be loaded: 4000

Program Completed Successfully...





INPUT FILE:

LINK1IN.TXT

H     PROGA    000000   000070
D     LISTA      000040   ENDA     000054
R     LISTB      ENDB    LISTC     ENDC
T     000020   10    03201D    77100004    150014
T     000054   16    100014     15100006    00002F    100014    FFFFC0
M    000024   05    +LISTB
M    000054   06    +LISTC
M    000058   06    +ENDC
M    000064   06    +LISTB
E     000000

H     PROGB     000000    000088
D     LISTB       000060    ENDB     000070
R     LISTA       ENDA     LISTC     ENDC
T     000036   11    03100000    772027       05100000
T     000070   18    100000        05100006   05100020    05100030   100000
M    000037   05    +LISTA
M    000044   05    +ENDA
M    000070   06    +ENDA
M    000074   06    +ENDC
M    000078   06    +ENDC
M    000082   06    +ENDA
E     000000

H    PROGC     000000     000057
D    LISTC       000030     ENDC     000042
R    LISTA       ENDA      LISTB     ENDB
T    000018    12    03100000    77100004    05100000
T    000042    15    100030        100008        100011     100000    100000
M   000019    05    +LISTA
M   000023    05    +LISTB
M   000027    05    +ENDA
M   000048    06    +LISTA
M   000051    06    +ENDA
M   000054    06    +LISTB
E    000000


OUTPUT FILE:


LINK1OUT.TXT


CSECT               SYM_NAME          ADDRESS       LENGTH
PROGA                                                 4000                  70
                                LISTA                    4040                  0
                                ENDA                    4054                  0
PROGB                                                 4046                 88
                                LISTB                    40a6                  0
                                ENDB                    40b6                  0
PROGC                                                 409e                  57
                                LISTC                    40ce                   0

                                ENDC                    40e0                  0

IMPLEMENTATION OF A RELOCATION LOADER

IMPLEMENTATION OF A RELOCATION LOADER

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
   int locctr; char add[10];
}obcode[300];
void main()
{
   char in[100][16],out[100][16],bin[20],addr[20],stloc[10];
   int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0;
   FILE *fp1,*fp2;
   clrscr();
   printf("\nImplementation Of A Relocation Loader:");
   printf("\n--------------------------------------\n\n");
   fp1=fopen("loadin.txt","r");
   fp2=fopen("loadout1.txt","w");
   printf("Enter the Location where the program has to be loaded : ");
   scanf("%s",stloc);
   start=atoi(stloc);
   location=start;
   tloc=start;
   fscanf(fp1,"%s",in[i]);
   while(strcmp(in[i],"T")!=0)
   {
     strcpy(out[i],in[i]); i++;
     fscanf(fp1,"%s",in[i]);
     strcpy(out[i],in[i]);
   }
   itoa(start,out[2],10);
   while(strcmp(in[i],"E")!=0)
   {
     strcpy(out[i],in[i]);
     if(strcmp(in[i],"T")==0)
     {
       for(j=0;j<3;j++)
       {
             i++;
             fscanf(fp1,"%s",in[i]);
             strcpy(out[i],in[i]);
       }
       bitmask=atoi(out[i]);
       itoa(bitmask,bin,2);
       strcpy(out[i],NULL);
       textloc=atoi(out[i-2]);
       textloc=textloc+start;
       itoa(textloc,out[i-2],10);
       for(n=0;n<(textloc-(tloc+tlen));n++)
       {
             strcpy(obcode[inc].add,"XX");
             obcode[inc++].locctr=location++;
       }
       tlen=atoi(out[i-1]);
       tloc=textloc; k=0;
     }
     else
     {
       if(bin[k]=='1')
       {
             num=0; len=strlen(out[i]);
             strcpy(addr,NULL);
             for(j=2;j<len;j++)
             {
               addr[num]=out[i][j];
               out[i][j]='\0';
               num++;
             }
             loc=atoi(addr); loc=loc+start;
             itoa(loc,addr,10);
             strcat(out[i],addr);
       }
       k++;
       len=strlen(out[i]); num=0;
       for(n=0;n<len;n++)
       {
             obcode[inc].add[num++]=out[i][n];
             if(num>1)
             {
               obcode[inc++].locctr=location++;
               num=0;
             }
       }
     }
     i++;
     fscanf(fp1,"%s",in[i]);
   }
   strcpy(out[i],in[i]); i++;
   fscanf(fp1,"%s",in[i]);
   loc=atoi(in[i]); loc=loc+start;
   strcpy(out[i],itoa(loc,addr,10));
   count=0; i=0; n=0;
   fprintf(fp2,"%d\t",obcode[n].locctr);
   for(n=0;n<inc;n++)
   {
     fprintf(fp2,"%s",obcode[n].add);
     i++;
     if(i>3)
     {
       fprintf(fp2,"\t");
       i=0; count++;
     }
     if(count>3)
     {
       fprintf(fp2,"\n%d\t",obcode[n+1].locctr);
       count=0;
     }
   }
   printf("\nProgram Completed Successfully...\n");
   getch();
}























OUTPUT:


Implementation Of A Relocation Loader:
----------------------------------------------------

Enter the Location where the program has to be loaded : 6000

Program Completed Successfully...



INPUT FILE:


LOADIN.TXT

H    PROG1    000000    000018
T    000000   15    000009    180012    0C0015   000005    000010
T    000023   18    000034    200029    130043    0C0054   000006    00015
E    000000



OUTPUT FILE:


LOADOUT1.TXT

6000    1860120C              00150000      05006010      XXXXXXXX        
6016    XXXXXXXX        20602913      00430C00    54000006    

6032    006015

IMPLEMENTATION OF AN ABSOLUTE LOADER

IMPLEMENTATION OF AN ABSOLUTE LOADER

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
   int locctr; char byte[5];
};
struct object_code code[200];
void main()
{
   FILE *fp1,*fp2;
   char input[15];
   int i,len,n=0,count=0,inc=0,textloc,tlen,tloc=0,num=0,loc;
   clrscr();
   printf("\nImplementation Of An Absolute Loader:");
   printf("\n-------------------------------------\n\n");
   fp1=fopen("loadin.txt","r");
   fp2=fopen("loadout.txt","w");
   rewind(fp1);  rewind(fp2);
   fscanf(fp1,"%s",input);
   if(strcmp(input,"H")==0)
   {
     for(i=0;i<4;i++)
     {
       if(i==1)
             fscanf(fp1,"%x",&loc);
       else
             fscanf(fp1,"%s",input);
     }
   }
   tloc=loc;
   while(strcmp(input,"E")!=0)
   {
     if(strcmp(input,"T")==0)
     {
       fscanf(fp1,"%x",&textloc);
       for(i=0;i<(textloc-(tloc+tlen));i++)
       {
             strcpy(code[inc].byte,"XX");
             code[inc++].locctr=loc++;
       }
       fscanf(fp1,"%x",&tlen);
       tloc=textloc;
     }
     else
     {
       len=strlen(input);
       for(i=0;i<len;i++)
       {
             code[inc].byte[num++]=input[i];
             if(num>1)
             {
               code[inc].locctr=loc;
               loc++;  inc++;  num=0;
             }
       }
     }
     fscanf(fp1,"%s",input);
   }
   n=0; i=0; count=0;
   fprintf(fp2,"%x\t",code[i].locctr);
   for(i=0;i<inc;i++)
   {
     fprintf(fp2,"%s",code[i].byte);
     n++;
     if(n>3)
     {
       fprintf(fp2,"\t");
       n=0; count++;
     }
     if(count>3)
     {
       fprintf(fp2,"\n%x\t",code[i+1].locctr);
       count=0;
     }
   }
   printf("\nProgram Completed Successfully...\n");
   getch();
}








OUTPUT:


Implementation Of An Absolute Loader:
--------------------------------------------------

Program Completed Successfully...


INPUT FILE:


LOADIN.TXT

H      PROG1     001000    000018
T      001000   15   001009   181012   0C1015   000005    000010
T      001023   18   001034   201029   131043    0C0054   000006    000015
E      001000


OUTPUT FILE:


LOADOUT.TXT

1000    00100918               10120C10            15000005              000010XX           
1010    XXXXXXXX        XXXXXXXX    XXXXXXXX       XX001034          
1020    20102913               10430C00            54000006              000015


IMPLEMENTATION OF A SINGLE PASS ASSEMBLER

IMPLEMENTATION OF A SINGLE PASS ASSEMBLER
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
struct input
{
   char label[10],opcode[10],operand[10],mnemonic[5];
   int loc;
};
struct input table[MAX];
struct symtab
{
   char sym[10];
   int f,val,ref;
};
struct symtab symtb1[MAX];
void main()
{
            int f1,i=1,j=1,flag,locctr,x;
            char add[10],code[10],mnemcode[5];
            FILE *fp1,*fp2,*fp3;
            clrscr();
            printf("\nImplementation Of A Single Pass Assembler:");
            printf("\n------------------------------------------\n\n");
            fp1=fopen("input.txt","r");
            fp2=fopen("optab.txt","r");
            fp3=fopen("spout.txt","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(symtb1[x].sym,table[i].label)==0) && (symtb1[x].f==1))
                  {
                        symtb1[x].val=locctr;
                        symtb1[x].f=0;
                        table[symtb1[x].ref].loc=locctr;
                        f1=1;  break;
                  }
                }
                if(f1==0)
                {
                  strcpy(symtb1[j].sym,table[i].label);
                  symtb1[j].val=locctr;
                  symtb1[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,symtb1[x].sym)==0)
                        {
                          flag=1;
                          if(symtb1[x].f==0)
                          table[i].loc=symtb1[x].val;
                          break;
                        }
                  }
                  if(flag!=1)
                  {
                        strcpy(symtb1[j].sym,table[i].operand);
                        symtb1[j].f=1;
                        symtb1[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",symtb1[x].sym,symtb1[x].val);
            printf("\nProgram Completed Successfully...\n");
            getch();
}











OUTPUT:


Implementation Of A Single Pass Assembler:
---------------------------------------------------------

VAR1     1009
VAR2     1012
ANS       1015

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:


SPOUT.TXT


**               START         1000            0
**               LDA             VAR1         001009
**               MUL            VAR2         201012
**               STA              ANS            0C1015
VAR1        WORD         11                11
VAR2        WORD         12                12

ANS           RESW          1                  0

Followers