Practical # 08: Object
Practical # 08: Object
Object:
Write a program in C to implement backend of the compiler that produce three address
statement.
Algorithm:
1. Start the program
2. Declare the required variables
3. Get maximum number of three address statement read from keyboard or user
4. Read the intermediate code in the form of quadruple three address
code from the keyboard or user and store it in to array.
4. Reads the arrays element up to delimiter and convert it into target code
i.e assembly code using for loop, isalpha() and if else statement
5. Three address statement is of the form c= a+b
e-g
three address statement
+at1t2
*t1t2t3 then an equivalent assembly code statement is
Code:
#include<stdio.h>
#include<string.h>
char a[10][10];
void main()
{
int i,j,n;
printf("\n enter the numbe of line");
scanf("%d",&n);
printf("\n enter the intermediate code:");
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n;i++)
printf("\n%s",a[i]);
for(i=0;i<n;i++)
{
if(a[i][0]=='+'||a[i][0]=='*'||a[i][0]=='/'||a[i][0]=='-')
{
if((isdigit(a[i][3]))&&(isdigit(a[i][5])))
calc(i,a[i][0]);
else if((isdigit(a[i][2]))&&(isdigit(a[i][5])))
calc1(i,a[i][0]);
else if((isdigit(a[i][2]))&&(isdigit(a[i][4])))
calc2(i,a[i][0]);
else if(isdigit(a[i][4]))
calc3(i,a[i][0]);
}
}
getch();
}
int calc(int i,char d)
{
printf("\n MOV %c,R0",a[i][1]);
printf("\n MOV %c%c,R1",a[i][2],a[i][3]);
choose(d);
choose(char d)
{
if(d=='+')
printf("\n ADD R0,R1");
else if(d=='-')
printf("\n SUB R0,R1");
else if(d=='*')
printf("\n MUL R0,R1");
else if(d=='/')
printf("\n DIV R0,R1");
return 0;
}
Output: