Guide: Online C Compiling
An interesting tool to observe how C code translates into assembly language is Compiler Explorer.
- Go to Compiler Explorer.
- Load the “sum over array” program from the examples (accessible using the load button, shaped like a floppy disk).
- Make sure
x86-64 gcc 4.8.2is selected underCompiler:. - Use the option
-m32(inCompiler options) to display code in 32-bit assembly language (as opposed to 64-bit by default). - If you see the message
<Compilation failed>, add the option-std=c99. - Initially, the code might be quite cumbersome. To make it more human-readable, add the option
-O2to the compilation options (Compiler options). - You may notice the presence of symbols like
.L3:and.L4:. These represent fixed points in the program, labels, quite similar to what is found in C. - Go through the compilers corresponding to the following architectures one by one: ARM, ARM64, AVR, PowerPC.
Note: for ARM, ARM64, and AVR, you will need to remove the previously set -m32 flag. You can observe how the generated code differs from one architecture to another. - Also, try the following compilers:
clangandicc. As you can see, even though it’s the same C code and the same architecture, the generated code differs. This happens because each compiler can have a different optimization and code generation strategy.
NOTE: clang is an open-source C/C++ compiler. It is often used in IDEs due to its very suggestive compilation error messages.
NOTE:
iccis the C/C++ compiler from Intel.
Write the following code sequence in the Code editor area:
int simple_fn(void)
{
int a = 1;
a++;
return a;
}
Observe the assembly code when the compilation options (Compiler options) are -m32, and when the compilation options are -m32 -O2. Notice the effect of optimization options on the generated assembly code.