The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Monday, November 26, 2012

Generate Three Address Code For Selected Statements


CODING :-

#include<stdio.h>
#include<string.h>

main()
{      
char str[10];
int i,j;
char op;
  printf(“\nEnter the expression:”);
scanf("%s",str);
op=str[0];
i=0;

while((op!='0')||(op!='+')||(op!='-')||(op!=’*’)||(op!=’/’))
{
op=str[++i];
}

switch(op)
{
case '+':printf("MOV R2,%c\n",str[i-1]);
printf("MOV R3,%c\n",str[i+1]);
printf("ADD R1,R2,R3\n");
printf("STORE R1,%c\n",str[i-3]);
break;
case '-':printf("MOV R2,%c\n",str[i-1]);
printf("MOV R3,%c\n",str[i+1]);
printf("SUB R1,R2,R3\n");
printf("STORE R1,%c\n",str[i-3]);
break;
                case '*':printf("MOV R2,%c\n",str[i-1]);
                         printf("MOV R3,%c\n",str[i+1]);
                         printf("MUL R1,R2,R3\n");
                         printf("STORE R1,%c\n",str[i-3]);
                         break;
                case '/':printf("MOV R2,%c\n",str[i-1]);
                         printf("MOV R3,%c\n",str[i+1]);
                         printf("DIV R1,R2,R3\n");
                         printf("STORE R1,%c\n",str[i-3]);
                         break;
default:printf("ERROR\n");
                break;
}
}

Output :-

Enter the expression: x=a+b

MOV R2,a
MOV R3,b
ADD R1,R2,R3
STORE R1,x


No comments:

Post a Comment