Generation of C assembly instructions

Source: Internet
Author: User

The main program is as follows:

The key to generating assembly commands based on four elements is the generation of labels.

Struct node6
{
Int L; // determines whether a flag is required
Int num; // the flag.
Char op [10]; // Operator
Char arg1 [10]; // operand 1
Char arg2 [10]; // operand 2
};

The format is as follows:

Output Format: row number, Quaternary (operation, operand, operand, result ).

(1): (=, 3,-, I ).
(2): (j <, I, 7,4 ).
(3): (J,-,-, 11 ).
(4): (j <, I, 3, 6 ).
(5): (J,-,-, 8 ).
(6): (=, 5,-, I ).
(7): (J,-,-, 10 ).
(8): (+, I, 1, T1 ).
(9): (=, T1,-, I ).
(10): (J,-,-, 2 ).

Int ccompiler: com_num (int x)
{
For (INT I = 0; I <quad_ I; I ++)
If (com_next_num [I]. num = X)
Return com_next_num [I]. value;
Return-1;
}
Void ccompiler: equal (INT num)
{
Com_ I _next ++;
If (strcmp (quad [num]. arg1 ,"-")! = 0)
{
Strcpy (com_next [com_ I _next]. Op, "mov ");
Strcpy (com_next [com_ I _next]. arg1, quad [num]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [num]. arg1 );
}
Else
{
Strcpy (com_next [com_ I _next]. Op, "mov ");
Strcpy (com_next [com_ I _next]. arg1, quad [num]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [num]. arg2 );
}
Com_next [com_ I _next]. num = num;
}
Void ccompiler: add (int I)
{
Com_ I _next ++;
Strcpy (com_next [com_ I _next]. Op, "mov ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg1 );
Com_next [com_ I _next]. num = I;
Com_ I _next ++;
Strcpy (com_next [com_ I _next]. Op, "add ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
// Com_next [com_ I _next]. L =-1;
}
Void ccompiler: Sub (int I)
{
Com_ I _next ++;
Strcpy (com_next [com_ I _next]. Op, "mov ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg1 );
Com_next [com_ I _next]. num = I;
Com_ I _next ++;
// Com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "sub ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
}
Void ccompiler: MUL (int I)
{
Com_ I _next ++;
Com_next [com_ I _next]. num = I;
Strcpy (com_next [com_ I _next]. Op, "mov ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg1 );
Com_ I _next ++;
// Com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "Mul ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
}
Void ccompiler: div (int I)
{
Com_ I _next ++;
Com_next [com_ I _next]. num = I;
Strcpy (com_next [com_ I _next]. Op, "mov ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg1 );
Com_ I _next ++;
// Com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "Div ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. Result );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
}
Void ccompiler: jnz (int I)
{

Int number1;
Int xx;
Com_ I _next ++;

Com_next [com_ I _next]. num = I;

Strcpy (com_next [com_ I _next]. Op, "CMP ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. arg1 );
Strcpy (com_next [com_ I _next]. arg2, "0 ");

Com_ I _next ++;

Strcpy (com_next [com_ I _next]. Op, "jnz ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
If (com_next [com_ I _next]. l! = 1) com_next [com_ I _next]. L =-1;

Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';
// Com_next [number1]. L = 1;
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;

}
Void ccompiler: JMP (int I)
{
Int number1;
Int xx;
Com_ I _next ++;
If (com_next [com_ I _next]. l! = 1)
{
Com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "JMP ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;
}
Else
{
Strcpy (com_next [com_ I _next]. Op, "JMP ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
Com_next [com_ I _next]. arg1 [0] = 'l ';
Com_next [com_ I _next]. arg1 [1] = '/0 ';
Strcat (com_next [com_ I _next]. arg1, quad [I]. Result );
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = I;

}
 
}
Void ccompiler: JB (int I)
{
Int number1;
Com_ I _next ++;
Com_next [com_ I _next]. num = I;

Strcpy (com_next [com_ I _next]. Op, "CMP ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. arg1 );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
Com_ I _next ++;
// Com_next [com_ I _next]. L =-1;
If (com_next [com_ I _next]. l! = 1) com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "JB ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';
// Com_next [number1]. L = 1;
Int xx;
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;
}
Void ccompiler: jbe (int I)
{
Int number1;
Com_ I _next ++;
Com_next [com_ I _next]. num = I;
Strcpy (com_next [com_ I _next]. Op, "CMP ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. arg1 );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
Com_ I _next ++;
// Com_next [com_ I _next]. L =-1;
If (com_next [com_ I _next]. l! = 1) com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "jbe ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
 
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';
// Com_next [number1]. L = 1;
Int xx;
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;
}

Void ccompiler: JA (int I)
{
Int number1;
Com_ I _next ++;
Com_next [com_ I _next]. num = I;
Strcpy (com_next [com_ I _next]. Op, "CMP ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. arg1 );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
Com_ I _next ++;
Com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "Ja ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
 
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';

Int xx;
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;
}
Void ccompiler: Jae (int I)
{
Int number1;
Com_ I _next ++;
Com_next [com_ I _next]. num = I;
Strcpy (com_next [com_ I _next]. Op, "CMP ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. arg1 );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
Com_ I _next ++;
If (com_next [com_ I _next]. l! = 1) com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "Jae ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
 
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';

Int xx;
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;
}
Void ccompiler: je (int I)
{
Int number1;
Com_ I _next ++;
Com_next [com_ I _next]. num = I;
Strcpy (com_next [com_ I _next]. Op, "CMP ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. arg1 );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
Com_ I _next ++;
Com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "Je ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
 
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';

Int xx;
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;
}
Void ccompiler: JNE (int I)
{
Int number1;
Com_ I _next ++;
Com_next [com_ I _next]. num = I;
Strcpy (com_next [com_ I _next]. Op, "CMP ");
Strcpy (com_next [com_ I _next]. arg1, quad [I]. arg1 );
Strcpy (com_next [com_ I _next]. arg2, quad [I]. arg2 );
Com_ I _next ++;
Com_next [com_ I _next]. L =-1;
Strcpy (com_next [com_ I _next]. Op, "JNE ");
Strcpy (com_next [com_ I _next]. arg2 ,"");
 
Number1 = quad [I]. Result [0]-'0 ';
If (quad [I]. Result [1] <= '9') & (quad [I]. Result [1]> = '0 '))
Number1 = number1 * 10 + quad [I]. Result [1]-'0 ';
 
Int xx;
Xx = com_num (number1 );
Com_next [xx]. L = 1;
Com_next [com_ I _next]. num = number1;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.