int, return, main, void, if, else, while, continue, break
Identifiers compliant with the C89 standard ([A-Za-z_][0-9A-Za-z_]*)
Decimal integers, such as 1, 223, 10
, etc
=
Unary Operator
- ! ~
Binary Operator
+ - * / % < <= > >= == != & | ^ && ||
; { } ( )
Variable Declaration
int a, b=111, c=1+3;
Assignment Statement
a = (d+b&1)/(e!=3^b/c&d);
a = b+c;
Return Statement
return a+b;
return func(a);
Function Call
println_int(a+b);
Conditional Statement
if ( condition ) { ... }
if ( condition ) { ... } else { ... }
Loop Statement
while ( condition ) { ... }
Loop Control Statement
continue;
break;
Without Arg(s)
int func(){...}
void func() {...}
With Arg(s)
int func(int a, int b){...}
void func(int a, int b){...}
In addition to custom functions, it is also necessary to support calls to preset functions
println_int(int a)
Has the same output as printf("%d\n", a)
in C
The following command can generate assembly code using gcc
gcc -S -masm=intel -fverbose-asm test.c
To compile with cmake, execute the following command from the command line:
mkdir build
cd build
cmake ..
cmake –build .
The compilation product is called build/CompilerlabX
.
If you want to execute the assembly code yourself and debug it
X86:
Execute in Linux end point after inserting the code yourself
gcc -m32 -no-pie <input assembly file> -o <output executable file> ./<output executable file>
The output can be observed.
Note: On some machines, you may need to add i386 architecture packages to perform the above operations correctly. The reference command is as follows
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386 libstdc++6:i386 gcc-multilib