Assignment 3 : Assembler
Software Description – An Assembler is a software processor that takes as input an assembly language program and translates it into machine code if it is error free otherwise provides a list of errors.
We will design an assembler for the hypothetical simple Instruction computer and its assembly language. Apart from imperative statements an assembly language contains Assembler directives and declaration statements.
The assembler should be able to handle programs containing assembler directives ‘START’ and ‘END’, declaration statements ‘DS’ and ‘DC’ and the imperative statements.
The Assembler will have two passes. In the first pass it will generate intermediate code and create symbol table. In the second pass intermediate code will be converted to machine code.
Mnemonic table :-Table that stores valid mnemonics and also the index matches the opcode or imperative statements
*mne[15]={"STOP","ADD",………"DC","START","END"}
Register table :-Table stores register names and index indicates the register number
char *reg[4]={"AREG", "BREG","CREG","DREG"};
Condition Code table:- Table stores condition names and index indicates the condition code
char *cc[6]={"LT","LE","EQ","GT","GE", "ANY"};
Symbol table :-Each entry in symbol table contains the name, address and flags indicating whether the symbol is used and defined
struct symtab { char symbol[20]; int add; int used; int defined; }sym[50];
Intermediate code table :- Each table entry stores address, opcode, register operand number, character which can be ‘S’ or ‘C’ indicating Symbol or constant and the value
struct ictab{ int address; int opcode; int regop; char optype; int value; }ic[50];
Error table:- Each entry contains line number and the error number indicating type of error struct errtab{ int lineno; int errno; }err[50];
Error message table :-This table is used for giving different error messages
char *errmsg[6]={“used but not defined”, “invalid opcode”,”wrong statement format”,..};
Slot 1
i) Answer the following questions after carefully reading the description and program structure. a) What data structures are used by the first pass of assembler?
Answer:
1. Symbol Table
2. Intermediate codes
b) How mnemonic table will be implemented in C?
Answer:
Mnemonic table :-Table that stores valid mnemonics and also the index matches the opcode or imperative statements
*mne[15]={"STOP","ADD",………"DC","START","END"}
c) Give the declaration for Symbol table.
Answer:
Symbol table :-Each entry in symbol table contains the name, address and flags indicating whether the symbol is used and defined
struct symtab { char symbol[20]; int add; int used; int defined; }sym[50];
ii) Implement a Two pass Assembler for hypothetical simple Instruction Computer and its simple assembly language that includes Assembler directives ”START” and “END”, the declarative statements “DS” and “DC” and imperative statements with mnemonics “STOP” to “ “PRINT”
a) Implement necessary tables statically and write functions for checking, displaying adding to tables. b) Store test program given below in a file and write dummy Passone that only prints the source program lines with line nos
START 300
BEGIN READ NUM
LOOP MOVEM AREG NUM
PRINT NUM
MULT AREG NUM
COMP AREG HUNDRED
BC LT LOOP
STOP
NUM DS 2
HUNDRED DC ‘100’
END BEGIN
Slot 2
i) Implement Separatetokens
ii) Verify using test program the separation of tokens for each line
iii) Display the error table
**********************************************************
Program :-
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
char symb[20];
int decl,used;
struct node *next;
}NODE;
NODE *first, *last;
void add_symb1(char *s)
{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
p->decl = 1;
p->used = 0;
p->next = NULL;
if(first==NULL)
first = p;
else
last->next = p;
last = p;
}
void add_symb2(char *s)
{ NODE *p;
p = (NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
p->decl = 0;
p->used = 1;
p->next = NULL;
if(first==NULL)
first=p;
else
last->next = p;
last=p;
}
NODE * search(char *s)
{
NODE *p;
p = first;
while(p!=NULL)
{
if(strcmp(p->symb,s)==0)
return p;
p=p->next;
}
return NULL;
}
char optab[][6]={"STOP","ADD","SUB","MULT",
"MOVER","MOVEM","COMP","BC",
"DIV","READ","PRINT"};
int search_op(char *s)
{
int i;
for(i=0;i<11;i++)
if(strcmp(optab[i],s)==0)
return i;
return -1;
}
int main()
{
NODE *p;
FILE *fp;
char buff[80],t1[20],t2[20],t3[20],t4[20],fname[40];
int n,i;
printf("Enter source file name:");
scanf("%s",fname);
fp = fopen(fname,"r");
if(fp==NULL)
{ printf("File %s not found.\n",fname);
exit(1);
}
while(fgets(buff,80,fp)!=NULL)
{
n = sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);
switch(n)
{
case 2:
if(strcmp(t1,"START")==0)
break;
i = search_op(t1);
if(i==9 || i==10) // READ or PRINT
{
p=search(t2);
if(p==NULL)
add_symb2(t2);
else
p->used=1;
break;
}
p = search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;
break;
case 3:
i = search_op(t1);
if(i>=1 && i<=8) // ADD to DIV
{
p = search(t3);
if(p==NULL)
add_symb2(t3);
else
p->used=1;
break;
}
if(strcmp(t2,"DS")==0 || strcmp(t2,"DC")==0)
{
p=search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;
break;
}
p=search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;
p=search(t3);
if(p==NULL)
add_symb2(t3);
else
p->used = 1;
break;
case 4:
p = search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;
p = search(t4);
if(p==NULL)
add_symb2(t4);
else
p->used =1;
} // switch
} // while
fclose(fp);
p = first;
while(p!=NULL)
{ if(p->decl!=0 && p->used==0)
printf("Symb %s declared but not used.\n",p->symb);
if(p->used==1 && p->decl==0)
printf("Symb %s used but not declared.\n",p->symb);
if(p->decl>1)
printf("Symb %s redeclared.\n",p->symb);
p=p->next;
}
return 0;
}
****************************************************
inp.asm
START 300
BEGIN READ NUM
LOOP MOVEM AREG NUM
PRINT NUM
MULT AREG NUM
COMP AREG HUNDRED
BC LT LOOP
STOP
NUM DS 2
HUNDRED DC ‘100’
END BEGIN
/***********************OUTPUT***********************
Enter source file name:inp.asm
Symb BEGIN declared but not used.
Symb END declared but not used.
**********************************************************************
Slot 3
i) Implement process tokens
ii) Display the contents of symbol table, error table and IC table
iii) Implement Pass2
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char symb[20];
int addr,val,len;
struct node *next;
}NODE;
NODE *first,*last;
void add_symb(char *s, int a, int v, int l)
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
strcpy(p->symb, s);
p->addr = a;
p->val = v;
p->len = l;
p->next = NULL;
if(first==NULL)
first=p;
else
last->next=p;
last=p;
}
void display_symbtab()
{
NODE *p;
printf("symb\taddr\tval\tlen\n");
p=first;
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\n",
p->symb,p->addr,p->val,p->len);
p=p->next;
}
}
char optab[][6]={"STOP","ADD","SUB","MULT",
"MOVER","MOVEM","COMP","BC",
"DIV","READ","PRINT"};
int search_op(char *s)
{ int i;
for(i=0;i<11;i++)
{
if(strcmp(optab[i],s)==0)
return i;
}
return -1;
}
char regtab[][5]={"AREG","BREG","CREG","DREG"};
int search_reg(char *s)
{
int i;
for(i=0;i<4;i++)
{
if(strcmp(regtab[i],s)==0)
return i;
}
return -1;
}
char adtab[][7]={"START","END",
"ORIGIN","EQU","LTORG"};
int search_ad(char *s)
{ int i;
for(i=0;i<5;i++)
{ if(strcmp(adtab[i],s)==0)
return i;
}
return -1;
}
int main()
{
NODE *p;
FILE *fp,*fp1;
char fname[40],buff[80],t1[20],t2[20],t3[20],t4[20];
int n,i,j,pc;
printf("Enter source file name:");
scanf("%s",fname);
fp = fopen(fname,"r");
if(fp==NULL)
{ printf("File %s not found.\n",fname);
exit(1);
}
fp1 = fopen("temp.i","w");
while(fgets(buff,80,fp)!=NULL)
{
n = sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);
switch(n)
{
case 1:
i = search_op(t1);
if(i==0) // STOP
{
fprintf(fp1,"%03d) (IS, %02d)\n",pc, i);
break;
}
i = search_ad(t1);
fprintf(fp1,"%03d) (AD, %02d)\n",pc,i+1);
break;
case 2:
i = search_ad(t1);
if(i==0) // START
{
fprintf(fp1,"%03d) (AD, %02d) (C, %s)\n", pc, i+1, t2);
pc = atoi(t2)-1;
break;
}
i = search_op(t1);
if(i==9 || i==10) // READ or PRINT
{ fprintf(fp1,"%03d) (IS, %02d) %s\n",pc,i,t2);
break;
}
add_symb(t1, pc, 0, 0);
fprintf(fp1,"%03d) (IS, 00)\n",pc);
break;
case 3:
if(strcmp(t2,"DS")==0)
{
add_symb(t1,pc,0,atoi(t3));
fprintf(fp1,"%03d) (DL, 02) (C, %s)\n",pc,t3);
pc+=atoi(t3)-1;
break;
}
if(strcmp(t2,"DC")==0)
{
add_symb(t1,pc,atoi(t3+1),1);
fprintf(fp1,"%03d) (DL, 01) (C, %d)\n",pc,atoi(t3+1));
break;
}
i = search_op(t1);
if(i>=1 && i<=8) // ADD to DIV
{
t2[strlen(t2)-1]='\0';
j = search_reg(t2);
fprintf(fp1,"%03d) (IS, %02d) (%d) %s\n",pc,i,j+1,t3);
break;
}
i = search_op(t2);
add_symb(t1,pc,0,0);
fprintf(fp1,"%03d) (IS, %02d) %s\n",pc,i,t3);
break;
case 4:
i=search_op(t2);
t3[strlen(t3)-1]='\0';
j=search_reg(t3);
add_symb(t1,pc,0,0);
fprintf(fp1,"%03d) (IS, %02d) (%d) %s\n",pc,i,j+1,t4);
}
pc++;
}
fclose(fp);
fclose(fp1);
display_symbtab();
printf("Intermediate Code:\n");
fp1 = fopen("temp.i","r");
while(fgets(buff,80,fp1)!=NULL)
printf("%s",buff);
fclose(fp1);
return 0;
}
*******************************************
inp.asm
START 300
BEGIN READ NUM
LOOP MOVEM AREG NUM
PRINT NUM
MULT AREG NUM
COMP AREG HUNDRED
BC LT LOOP
STOP
NUM DS 2
HUNDRED DC ‘100’
END BEGIN
********************************************************
OUTPUT:-
Enter source file name:inp.asm
symb addr val len
BEGIN 300 0 0
LOOP 301 0 0
NUM 307 0 2
HUNDRED 309 0 1
END 310 0 0
Intermediate Code:
000) (AD, 01) (C, 300)
300) (IS, 09) NUM
301) (IS, 05) (0) NUM
302) (IS, 10) NUM
303) (IS, 03) (0) NUM
304) (IS, 06) (0) HUNDRED
305) (IS, 07) (0) LOOP
306) (IS, 00)
307) (DL, 02) (C, 2)
309) (DL, 01) (C, 0)
310) (IS, 00)
***********************************************************
No comments:
Post a Comment