SPO – Lab#3

Compilation Flags and Assembler Code

This lab is focused on the effects of different compiler options on the compilation of code and how the binary generated on compilation, changes with different flags.

The code used here is the world famous, Hello World! program

#include <stdio.h>
int main() {
        printf("Hello World!\n");


As suspected compiling the above program doesn’t do much and we get “Hello World!” on the screen.

Next we’ll compile with the following compilation flags

  1. “-g”  –  This flag generates the debugging information for the program and stores it in the compiled executable of the program.
  2. “O0”  –  This compiler option tells the compiler to not apply any optimization and keep everything on default.
  3. “-fno-builtin”  –  This option tells the compiler to not apply any kind of bulit in function optimizations on its own.

On compilation, we get and ELF file. We can view and analyze the code inside this file by using the “objdump” program.

The “-g” compiler flag increased the file size of the binary by 20% because of the debugging information added to the binary.

Using other compiler options:

1) -static

The binary generated with this flag made it 700% larger. This flag makes sure that the program runs in the exact same way every time.

2) removing -fno-builtin

The difference in the size of the file was almost negligible and the only difference in the instructions was the addition of an instruction telling the system to not do anything in terms of loading data into the cache.

3) Adding arguments to the printf function

If we add more than 6 arguments to the function, the first 6 arguments are stored in 32-bit registers and the following arguments are pushed to the stack to be pulled out later when needed.

4) Putting printf() in a separate function

Putting the printf function in  a separate function doesn’t have much effect. The disassembly changes to include another section <output> and this section will have all the printf statements that the main function had before.

5) Removing O0 and adding O3

This will set the optimization level in the compiler to the highest. O3 optimized programs aren’t always stable because of some experimental features of the compiler.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s