System Software Lab 5 | Read Now
System Software VTU Lab
Program 5:- Design, develop and implement a C/Java program to generate the machine code using Triples for the statement A = -B * (C +D) whose intermediate code in three- address form:
T1 = -B
T2 = C + D
T3 = T1 + T2
A = T3
Program Code [lab5.c]
#include<stdio.h> #include<stdlib.h> #include<ctype.h> char op[2],arg1[5],arg2[5],result[5]; int main() { FILE *fp1,*fp2; fp1=fopen("input.txt","r"); fp2=fopen("output.txt","w"); while(!feof(fp1)) { fscanf(fp1,"%s%s%s%s",result,arg1,op,arg2); if(strcmp(op,"+")==0) { fprintf(fp2,"\nMOV R0,%s",arg1); fprintf(fp2,"\nADD R0,%s",arg2); fprintf(fp2,"\nMOV %s,R0",result); } if(strcmp(op,"*")==0) { fprintf(fp2,"\nMOV R0,%s",arg1); fprintf(fp2,"\nMUL R0,%s",arg2); fprintf(fp2,"\nMOV %s,R0",result); } if(strcmp(op,"-")==0) { fprintf(fp2,"\nMOV R0,%s",arg1); fprintf(fp2,"\nSUB R0,%s",arg2); fprintf(fp2,"\nMOV %s,R0",result); } if(strcmp(op,"/")==0) { fprintf(fp2,"\nMOV R0,%s",arg1); fprintf(fp2,"\nDIV R0,%s",arg2); fprintf(fp2,"\nMOV %s,R0",result); } if(strcmp(op,"=")==0) { fprintf(fp2,"\nMOV R0,%s",arg1); fprintf(fp2,"\nMOV %s,R0",result); } } fclose(fp1); fclose(fp2); }
input.txt
T1 – B = ?
T2 C + D
T3 T1 * T2
A T3 = ?