all students informed that they r provided all program`s of all subject`s in engg.

Wednesday, June 25, 2008

program for 2 pass assembler


/*Pass Assembler*/
#include
#include
#include
#include
#include
#include
void ShowPass2();
void ShowSymbol();
void ShowPass1();
void readword(void);
void ismot();
void issymbol();
void symbolpresent(char c[]);
void findtype(int );
void display();
void show(int);
void pass2();
struct mot
{
char mne[7];
int type; //type 1.IMPERATIVE 2.DECLARATIVE 3.DIRECTIVE
char op[5];
int len;
}m;
struct mot mtable[10]= {
{"LOAD",1,"08",1},
{"STORE",1,"05",1},
{"ADD",1,"12",1},
{"PRINT",1,"07",1},
{"DS",2,"DL#1",0},
{"DC",2,"DL#2",0},
{"START",3,"-",0},
{"EQU",3,"-",0},
{"ORIGIN",3,"-",0},
{"END",3,"-",0}
};
struct pass1
{
int LC;
char code1[10];
char code2[10];
}pa[25];
struct pa2
{
int LC;
char code1[10];
char code2[10];
}p2[25];
struct symtab
{
char symbol[15];
int address,length,OtherInfo;
}stab[20];
int index=0; //give last index of symtab
static int passindex=0;
int lc;
char word[30];
FILE *fs,*ft;
char ch;
void ShowSymbol()
{
int i;
printf("\n ***********SYMBOL TABLE***********");
gotoxy(2,3);
printf("symbol");
gotoxy(22,3);
printf("Address\n*********************************");
for(i=1;i<=index-1;i++)
{
gotoxy(4,4+i);
printf("%s",stab[i].symbol);
gotoxy(24,4+i);
printf("%d",stab[i].address);
}
printf("\n ****************************************");
}
void ShowMOT()
{
int i;
printf("\n**************MNEMOPNICS OPCODE TABLE***************");
gotoxy(2,3);
printf("MNEMONIC");
gotoxy(22,3);
printf("Type");
gotoxy(30,3);
printf("opcode");
gotoxy(40,3);
printf("Length");
printf("\n ******************************************************");
for(i=0;i<10;i++)
{
gotoxy(4,6+i);
printf("%s",mtable[i].mne);
gotoxy(24,6+i);
printf("%d",mtable[i].type);
gotoxy(33,6+i);
printf("%s",mtable[i].op);
gotoxy(43,6+i);
printf("%d",mtable[i].len);
}
printf("\n ******************************************************");
}
void ShowPass1()
{
int i;
clrscr();
printf("\n ******************PASS 1********************");
gotoxy(2,3);
printf("LC");
gotoxy(17,3);
printf("Opcode");
gotoxy(32,3);
printf("Operand");
printf("\n ***********************************");
for(i=0;i{
gotoxy(4,5+i);
printf("%d",pa[i].LC);
gotoxy(19,5+i);
printf("%s",pa[i].code1);
gotoxy(34,5+i);
printf("%s",pa[i].code2);
}
printf("\n ***********************************");
}
void ShowPass2()
{
int i;
clrscr();
printf("\n ***************PASS 2********************");
gotoxy(2,3);
printf("LC");
gotoxy(17,3);
printf("Opcode");
gotoxy(32,3);
printf("Operand");
printf("\n ***********************************");
for(i=0;i{
gotoxy(4,4+i);
printf("%d",p2[i].LC);
gotoxy(19,4+i);
printf("%s",p2[i].code1);
gotoxy(34,4+i);
printf("%s",p2[i].code2);
}
printf("\n ***********************************");
}
void main()
{
char fname[15];
int i;
clrscr();
printf("\n Enter the name of the file==>\t");
gets(fname);
if((fs=fopen(fname,"r"))==NULL)
{
printf("\nUnable TO Read This File.");
getch();
exit(1);
}
else
{
if((ft=fopen("output.asm","w"))==NULL)
{
printf("Unable TO Open File.");
getch();
fclose(fs);
exit(1);
}
do
{
printf(" ");
readword();
ismot();
}while(ch!=EOF);
pass2();
show(passindex);
display();
getch();
fclose(fs);
fclose(ft);
}
}
void readword(void)
{
int i;
i=0;
while((ch=fgetc(fs))!=' ')
{
if(ch==EOF ch=='\t' ch=='\n')
break;
if(ch==EOF && ch=='\t' && ch=='\n')
word[i++]=ch;
}
word[i]='\0';
}
void ismot()
{
int i,t,flag=0;
for(i=0;i<11;i++)
{
if((strcmpi(mtable[i].mne,word))==0)
{
flag=1;
switch(i)
{
case 0: //load
strcpy(pa[passindex].code1,mtable[i].op);
readword();
issymbol();
strcpy(pa[passindex].code2,word);
pa[++passindex].LC=pa[passindex-1].LC+mtable[i].len;
break;
case 1: //store
strcpy(pa[passindex].code1,mtable[i].op);
readword();
issymbol();
strcpy(pa[passindex].code2,word);
pa[++passindex].LC=pa[passindex-1].LC+mtable[i].len;
break;
case 2: //add
strcpy(pa[passindex].code1,mtable[i].op);
readword();
issymbol();
strcpy(pa[passindex].code2,word);
pa[++passindex].LC=pa[passindex-1].LC+mtable[i].len;
break;
case 3: //print
strcpy(pa[passindex].code1,mtable[i].op);
readword();
issymbol();
strcpy(pa[passindex].code2,word);
pa[++passindex].LC=pa[passindex++].LC=pa[passindex-1].LC+mtable[i].len;
lc=pa[passindex-1].LC;
break;
case 4: //DS
readword();
lc=atoi(word);
t=pa[passindex].LC+lc;
strcpy(pa[passindex].code1,mtable[i].op);
strcpy(pa[passindex].code2,strcat(word,"i"));
pa[++passindex].LC=t;
break;
case 5: //dc
readword();
strcpy(pa[passindex].code1,mtable[i].op);
strcpy(pa[passindex].code2,strcat(word,"i"));
lc=pa[passindex].LC;
pa[passindex++].LC=lc;
break;
case 6: //start
readword();
lc=atoi(word);
pa[passindex].LC=lc;
break;
case 7: //equ
readword();
issymbol();
readword();
readword();
for(i=0;i<=index;i++)
{
flag=0;
if(strcmpi(stab[i].symbol,"D")==0)
{
stab[i].address=lc+1;
break;
}
}
break;
case 8: //origin
readword();
issymbol();
readword();
readword();
pa[passindex].LC=lc+atoi(word);
break;
case 9: //end
break;
default:
break;
}
}
}
// getch();
if(flag==0)
issymbol();
}
void issymbol()
{
int i=0,flag;
//readword();
for(i=0;i<=index;i++)
{
flag=0;
if(strcmpi(stab[i].symbol,word)==0)
{
lc=stab[i].address;
flag=1;
break;
}
}
if(flag==0)
{
index++;
strcpy(stab[index].symbol,word);
stab[index].address=pa[passindex].LC;
}
}
void findtype(int n)
{
switch(n)
{
case 1: printf("\Imperative");
break;
case 2: printf("\Declarative");
break;
case 3: printf("\Directive");
break;
}
printf("%s",word);
}
void show(int n)
{
int i;
printf("\npassindex=%d",n);
for(i=0;i{
printf("\n%d\t",pa[i].LC);
printf("%s\t",pa[i].code1);
printf("%s\t",pa[i].code2);
}
getch();
}
void pass2()
{
int i;
char ch[10];
for(i=0;i{
p2[i].LC=pa[i].LC;
strcpy(p2[i].code1,pa[i].code1);
strcpy(p2[i].code2,pa[i].code2);
strcpy(ch,p2[i].code2);
symbolpresent(ch);
itoa(lc,p2[i].code2,10);
}
printf("\n PASS2");
for(i=0;i{
printf("\n%d\t",p2[i].LC);
printf("%s\t",p2[i].code1);
printf("%s\t",p2[i].code2);
}
}
void symbolpresent(char c[])
{
int i=0;
for(i=0;i<=index;i++)
{
if(strcmpi(stab[i].symbol,c)==0)
{
lc=stab[i].address;
break;
}
}
}
void display()
{
clrscr();
ShowMOT();
getch();
clrscr();
ShowSymbol();
getch();
ShowPass1();
getch();
ShowPass2();
getch();
}
ASSEM.C
START 100
A DS 3
L1 LOAD B
ADD C
STORE D
D EQU A + 1
L2 PRINT D
ORIGIN A + 2
C DC 5
ORIGIN L2 + 1
B DC 19
OUTPUT
**************MNEMONICS OPCODE TABLE***************
Mnemonic Type Opcode Length
*********************************************************
LOAD 1 08 1
STORE 1 05 1
ADD 1 12 1
PRINT 1 07 1
DS 2 DL#1 0
DC 2 DL#2 0
START 3 -- 0
EQU 3 -- 0
ORIGIN 3 -- 0
END 3 -- 0
***************SYMBOL TABLE***************
Symbol Address
*********************************************
A 100
L1 103
B 103
C 104
D 101
*********************************************
********************PASS 1********************
LC OPCODE OPERAND
**********************************************
100 DL#1 3i
103 08 B
104 12 C
105 05 D
106 07 D
102 00 5i
107 00 19i
**********************************************
********************PASS 2********************
LC OPCODE OPERAND
**********************************************
103 08 103
104 12 104
105 05 101
106 07 101
**********************************************

No comments:

Powered By Blogger

my phylosophy

My photo
virtual city, MH, India
۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ hi, gies i am fan of computer so, to share my knowledge. i made this blog with some reference: 1.www.mumbaihacker.com 2.www.redhacker.xm.com 3.www.onlinehackers.xm.com 4.www.rupeshhacktrix.blogspot.com ************************************ ® registered . ۩ orkut V.I.P account ۩ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ ♥ ۩۞۩ wєlcσмє тσ⋙ αcα∂ємy σƒ нαcking ⋘ ҳ̸Ҳ̸ҳ●๋• experience the difference ҳ̸Ҳ̸ҳ●๋•