Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program to take as input (via stdin) a valid assignment stateme in C a

ID: 3755627 • Letter: W

Question

Write a C program to take as input (via stdin) a valid assignment stateme in C and generate MIPS assembly code to perform the given calculation(s). You can assume that each C variable name is one lowercase letter (e.g., a, b, c, etc.) and of type int. Further, positive int constants are also allowed as part of the given expression. For this assignment, you only need to support the addition (+) and subtraction () operators. Note that you should use isspace O, islower O, isdigit), scanf O, etc. to parse the input. The MIPS code you generate must make use of registers $s0,$s1,.. ,$s7 to correspond to C vari- ables and registers $to,St1,.. .$t9 to correspond to any temporary variables you need. Variables in MIPS should match those in C from left to right, meaning that the final result of the assignment statement must end up in register $s0. You can assume that you will not need more than the specific MIPS registers listed here. Below are a few example runs of your program. In the first example, register $s0 corresponds to C variable f, $s1 corresponds to g, and $s2 corresponds to h. bash$ ./a.out Please enter a valid C assignment statement f g+ h 42; The MIPS pseudocode is: add $to, $s1,$s2 sub $s0,$t0,42 bash$ ./a.out Please enter a valid C assignment statement: The MIPS pseudocode is: sub $t0, $s1,12 add $s0, $t0, $s2 bash$ ./a.out

Explanation / Answer

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

/*Func line takes in a file, and places a line number at the beginning of every
non blank line */

void line(FILE *input, FILE *output) {  
   char a, c;
   int lineNum = 0;
   int startOfLine = 1;
   char ch;
   fprintf(output, "%d ", ++lineNum);

   //copy the contents of the input file into the output file
   do {
    a = fgetc(input);
    fputc(a, output);
    c = getc(input);
    if(a == ' ' && c != ' ') {
        if(lineNum > 9)
            fprintf(output, "%d ", ++lineNum);
        else
            fprintf(output, "%d ", ++lineNum);
    }
    ungetc(c, input);
    } while (a != EOF);
}

/* Func cross takes in a file, and finds each identifier, marks its definition, and every
use and returns that in the output file */

void cross(FILE *input, FILE *output) {  
   FILE *temp = fopen("temp.txt", "a+");
   int lineNum = 0;
   int startOfLine = 1;
   char identifier[20][10];
   char a, c, i;
   int j, k, p;
   int size=0;
   int def[20];
   int use[20][50];
   char tempstr[80];
   fprintf(output, "Identifier Definition Use ");

   fprintf(temp, "%d ", ++lineNum);
   //copy contents of input into a temp file
   do {
    a = fgetc(input);
    fputc(a, temp);
    c = getc(input);

if(a == ' ' && c != ' ') {
        if(lineNum > 9)
            fprintf(temp, "%d ", ++lineNum);
        else
            fprintf(temp, "%d ", ++lineNum);
    }
    ungetc(c, input);
    } while (a != EOF);

    fclose(temp);
    fopen("temp.txt", "r");

   j=0;
   
    //checks to see if current line has an Identifier and if so saves it to an array
    //along with the line number it was defined on
    while(fgets(tempstr, 80, temp)) {
        if(isalpha(tempstr[4]) || tempstr[4] == '_') {
          sscanf(tempstr, "%d %[0-9_A-Z_a-z_]", &def[j], identifier[j]);
          j++;
          size++;
      }
    }
   
    fclose(temp);
    fopen("temp.txt", "r");

    //checks for each identifier, on every line whether or not that identifier is used
    while(fgets(tempstr, 80, temp)) {
        char *tempNum;
        sscanf(tempstr, "%s", tempNum);
        int tempN = atoi(tempNum);
        int n;
        p=0;
        for(n=0; n<size; n++) {
            if(strstr(tempstr, identifier[n]) && tempN > def[n] && tempstr[4] != '#') {
                use[n][p] = tempN;
                p++;
            }
        }
    }

    //writes the identifier, definition, and uses to the file
    for(k=0;k<size;k++) {
        fprintf(output, "%s %d ", identifier[k], def[k]);
        for(p=0; p<50; p++) {
            if(use[k][p] != NULL && use[k][p] < lineNum && use[k][p] > def[k])
                fprintf(output,"%d ", use[k][p]);
        }
        fprintf(output, " ");
    }
}

/*Func both returns the file with numbered lines and a cross reference table at the bottom of the file */

void both(FILE *input, FILE *output, char *outputName) {  
   FILE *lineFile = fopen("line.txt", "a+");
   FILE *crossFile = fopen("cross.txt", "a+");
   char ch;
   line(input, lineFile);
   cross(input, crossFile);
   while( (ch = fgetc(lineFile)) != EOF)
       fputc(ch, output);
   fprintf(output, " Cross Reference Table ");
   while( (ch = fgetc(crossFile)) != EOF)
       fputc(ch, output);
   fclose(output);
}

int main(int argc, char * argv[]) {
   FILE *input, *output;
   output = fopen(argv[4], "a+");
   char outputName[50];
   strcpy(outputName, argv[4]);
   //Error testing
   if(argc > 5)
       exit(1);
   if(strcmp(argv[2], "-l") != 0 && strcmp(argv[2], "-c") != 0 && strcmp(argv[2], "-b") != 0) {
       printf("Incorrect flag syntax... Exiting ");
       exit(1);
   }
   if((input = fopen(argv[3], "r+")) == NULL) {
       printf("Input file could not be opened... Exiting ");
       exit(1);
   }
   else {
       if(strcmp(argv[2], "-l") == 0) {
           line(input, output);
       }
       else if(strcmp(argv[2], "-c") == 0)
           cross(input, output);
       else {
           both(input, output, outputName);
       }
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote