Implement the back end of the compiler in C

 Questions

Implement the back end of the compiler which takes the three address code and produces the 8086 assembly language instructions that can be assembled and run using a 8086 assembler. The target assembly instructions can be simple move, add, sub, jump. Also simple addressing modes are used.

Program

#include<stdio.h>
#include<stdio.h>
//#include<conio.h>
#include<string.h>
void main()
{
 char icode[10][30],str[20],opr[10];
int i=0;
//clrscr();
printf("\n Enter the set of intermediate code (terminated by exit):\n");
do
{
 scanf("%s",icode[i]);
} while(strcmp(icode[i++],"exit")!=0);
printf("\n target code generation");
printf("\n************************");
i=0;
do
{
 strcpy(str,icode[i]);
 switch(str[3])
 {
 case '+':
 strcpy(opr,"ADD");
 break;
 case '-':
 strcpy(opr,"SUB");
 break;
 case '*':
 strcpy(opr,"MUL");
 break;
 case '/':
 strcpy(opr,"DIV");
 break;
 }
 printf("\n\tMov %c,R%d",str[2],i);

 printf("\n\t%s%c,R%d",opr,str[4],i);
 printf("\n\tMov R%d,%c",i,str[0]);
 }while(strcmp(icode[++i],"exit")!=0);
//getch();
}{codeBox}

Output

Explanation

This code appears to be a program that generates target code from a set of intermediate code. Here is an explanation of the code:

  1. The program begins by including several header files, including stdio.h and string.h. These header files provide functions for input/output operations and string manipulation, respectively.
  2. The main function is defined. This is the entry point of the program and is where the program's execution begins.
  3. A character array called icode is declared, which will store the intermediate code. Another character array called str is also declared, which will be used to store a copy of a single intermediate code line. Another character array called opr is also declared, which will be used to store the corresponding target code operation (e.g., ADD, SUB, MUL, or DIV).
  4. The program prompts the user to enter a set of intermediate code, terminated by the word "exit". The intermediate code is read using the scanf function and stored in the icode array. This process is repeated until the word "exit" is encountered.
  5. The program then prints a message indicating that it is generating the target code and a separator line.
  6. The program enters a loop that iterates over each intermediate code line stored in the icode array. For each iteration:
  • A copy of the intermediate code line is made and stored in the str array.
  • The program uses a switch statement to determine the operation indicated by the fourth character of the str array (which corresponds to the operation in the intermediate code). Based on the operation, the corresponding target code operation is copied into the opr array.
  • The program prints a message indicating that the target code operation "Mov" should be performed, followed by the second character of the str array and the register Ri, where i is the current iteration number.
  • The program prints a message indicating that the target code operation stored in the opr array should be performed, followed by the fourth character of the str array and the register Ri.
  • The program prints a message indicating that the target code operation "Mov" should be performed, followed by the register Ri and the first character of the str array.
7.The loop continues until the word "exit" is encountered in the icode array.
8.The main function ends, and the program terminates.

Conclusion

I hope this helps! Let me know if you have any questions about the code.

Previous Post Next Post
Premium By Raushan Design With Shroff Templates