Saturday, April 26, 2014

C/C++ CODE FOR ASSEMBLER PASS-2

 ASSEMBLER PASS-2

Actually, In a Two Pass Assembler most of the job is done in the first pass itself.The remaining work do be done includes:

  1. Replace symbols with their address by referring Symbol Table.
  2. Replace literals with their address by referring Literal Table.
  3. Generate the Machine Code. 

The following code illustrates this:

#include<stdio.h>

#include<conio.h>

#include<string.h>



struct ST

{

char name[10];

int len;

int val;

int addr;

};


struct LT

{

char name[10];

int addr;

};


struct ST s[10];



struct LT l[10]=

{

{"='7'",212},

{"='5'",404},

{"='2'",544},

{"='1'",548}

};


void main()

{

FILE *fptr;

int i,j,k,x,flag=0,num=0;

float scnt;

char a[10],b[10],c[10],d[10],ch;

clrscr();

fptr=fopen("apaas2.txt","r");

strcpy(s[5].name,"sum");

s[5].val=8;

s[5].len=4;

s[5].addr=420;

strcpy(s[7].name,"area");

s[7].val=0;

s[7].len=120;

s[7].addr=424;

ch=fgetc(fptr);

while(ch!=EOF)

{

i=0;

flag=0;

num=0;

while(ch!=' ' && ch!='\n')

{

a[i]=ch;

i++;

ch=fgetc(fptr);

}

a[i]='\0';

for(j=0;a[j]!='\0';j++)

{

num=num+(j*a[j]);

}

scnt=num%10;

x=strcmp(s[scnt].name,a);

if(x==0)

{

a[0]='\0';

itoa(s[scnt].addr,a,10);

printf(" %s",a);

flag=1;

}

for(j=0;j<4;j++)

{

x=strcmp(a,l[j].name);

if(x==0)

{

a[0]='\0';

itoa(l[j].addr,a,10);

printf(" %s",a);

flag=1;

}

}


if(flag!=1)

{

printf("%s",a);

}

if(ch==' ')

printf(" ");

if(ch=='\n')

printf("\n");


ch=fgetc(fptr);

}

fclose(fptr);

getch();

}


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

You can use this as input in the file you are opening in your program:

200) 42 3 sum
204) 41 1 ='7'
208) 42 1  3
212)='7'
216) 41 3 ='5'
400) 42 1  2
404)='5'
408) 42 1 ='2'
412) 41 3 ='1'
416) 41 1  3
420)sum
424)xyz
544)='2'
548)='1'
552)


No comments:

Post a Comment

Thanks for your valuable comment