Mark A. Holmes

COSC 5700.01, Spring 2023

Lab 4

May 3, 2023

 

 

 

C program to generate machine Code generation from quadruples for simple arithmetic expression

 

This is your last Lab (for extra cfedit). You are to write  a C code that generates the code from quadruples (or triples if you want) for the arithmetic expression below

The arithmetic expression is a = b/c + d *e – f * g + h/i

As a rubric, I have included the code that I just wrote to implement the code generation for the expression A = - B *(C + D).

(a)    You will need to write down your intermediate code for the given expression and put it in a text file (which I called input.txt).

(b)   The output should also be printed in a file (which I called output.txt. You can just click on the output.txt after compiling and running the code.)

(c)    Include any code that you believe is needed to complete the task

(d)   Please submit your code, input file, as well as your output file

Here is my input.txt file that contains the intermediate code:

T1 0 - B

T2 C + D

T3 T1 * T2

A T3 = ?

(I used 0 – B to obtain -B in the first line but this could be implemented differently using a unary operator to obtain -B)

Below is the code:

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <iostream>

#include <fstream>

char op[2], arg1[5],arg2[5], result[5];

void 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,"\nLD RO, %s",arg1);

        fprintf(fp2,"\nADD RO, RO, %s", arg2);

        fprintf(fp2,"\nLD %s,RO", result);

          }

      if(strcmp(op,"*")==0)

      {

        fprintf(fp2,"\nLD RO, %s", arg1);

        fprintf(fp2,"\nMULT RO,RO, %s",arg2);

        fprintf(fp2,"\nLD %s,RO",result);

        }

      if(strcmp(op,"-")==0)

      {

        fprintf(fp2,"\nLD RO, %s", arg1);

        fprintf(fp2,"\nSUB RO, RO, %s", arg2);

        fprintf(fp2,"\nLD %s, RO", result);

      }

 

      if(strcmp(op,"=")==0)

      {

        fprintf(fp2,"\nLD RO,%s",arg1);

        fprintf(fp2,"\nST %s,RO",result);

      }

      }

  fclose(fp1);

  fclose(fp2);

  }

 

 This LAB is due on 05/10/23 at 11:59 PM. Please I will not accept late submissions. ALL submissions should be on Canvas and not via email.

 

 

My code. (MAH)

 

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <cstring>

#include <fstream>

 

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,"\nLD RO, %s",arg1);

fprintf(fp2,"\nADD RO, RO, %s", arg2);

fprintf(fp2,"\nLD %s,RO", result);

 

}

 

if(strcmp(op,"*")==0)

 

{

 

fprintf(fp2,"\nLD RO, %s", arg1);

fprintf(fp2,"\nMULT RO,RO, %s",arg2);

fprintf(fp2,"\nLD %s,RO",result);

 

 

}

 

 

if(strcmp(op,"-")==0)

 

 

{

 

 

 

fprintf(fp2,"\nLD RO, %s", arg1);

fprintf(fp2,"\nSUB RO, RO, %s", arg2);

fprintf(fp2,"\nLD %s, RO", result);

 

 

 

}

 

 

if(strcmp(op,"/")==0)

if(strcmp(op,"=")==0)

 

 

 

{

 

 

fprintf(fp2,"\nLD RO,%s",arg1);

fprintf(fp2,"\nST %s,RO",result);

 

 

 

}

}

 

 

 

fclose(fp1);

fclose(fp2);

 

 

 

}

 

 

Go Back