Skip to content

Instantly share code, notes, and snippets.

@blacksmithop
Created November 12, 2020 06:17
Show Gist options
  • Save blacksmithop/d5ddf817bb337377988d715a67776386 to your computer and use it in GitHub Desktop.
Save blacksmithop/d5ddf817bb337377988d715a67776386 to your computer and use it in GitHub Desktop.
Intermediate Code -> Target Code
#include <stdio.h>
#include <string.h>
void main(){
char icode[10][30], str[30], opr[30];
int i=0;
printf("Enter Intermediate Code (terminated by exit):\n");
do{
scanf("%s", icode[i]);
}while(strcmp(icode[i++], "exit") !=0);
printf("Target Code Generator\n");
i = 0;
do{
strcpy(str, icode[i]);
switch (str[3]) { //this is where symbols are
case '+':
strcpy(opr, "ADD");
break;
case '-':
strcpy(opr, "SUB");
break;
case '*':
strcpy(opr, "MUL");
break;
case '/':
strcpy(opr, "DIV");
break;
}
printf("\nMOV %c,R%d",str[2],i);
printf("\n%s %c,R%d",opr,str[4],i);
printf("\nMOV R%d,%c",i,str[0]);
}while(strcmp(icode[++i], "exit") !=0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment