0% found this document useful (0 votes)
30 views

Practical # 08: Object

The document describes a program that implements the backend of a compiler to produce three address statements. It reads in a number of quadruples representing intermediate code and converts each one into equivalent assembly code statements. The program declares variables, gets the number of statements, reads the quadruples into an array, and then uses if/else statements and functions to parse each quadruple and output the corresponding assembly code statement.

Uploaded by

Rabia Jiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Practical # 08: Object

The document describes a program that implements the backend of a compiler to produce three address statements. It reads in a number of quadruples representing intermediate code and converts each one into equivalent assembly code statements. The program declares variables, gets the number of statements, reads the quadruples into an array, and then uses if/else statements and functions to parse each quadruple and output the corresponding assembly code statement.

Uploaded by

Rabia Jiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical # 08

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);

printf("\n MOV R1,%c%c",a[i][4],a[i][5]);


return;
}
int calc1(int i,char d)
{
printf("\n MOV %c%c,R0",a[i][1],a[i][2]);
printf("\n MOV %c,R1",a[i][3]);
choose(d);
printf("\n MOV R1,%c%c",a[i][4],a[i][5]);
return;
}
int calc2(int i,char d)
{
printf("\n MOV %c%c,R0",a[i][1],a[i][2]);
printf("\n MOV %c%c,R1",a[i][3],a[i][4]);
choose(d);
printf("\n MOV R1,%c%c",a[i][5],a[i][6]);
return;
}
int calc3(int i,char d)
{
printf("\n MOV %c,R0",a[i][1]);
printf("\n MOV %c,R1",a[i][2]);
choose(d);
printf("\n MOV R1,%c%c",a[i][3],a[i][4]);
return;
}

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:

You might also like