SPO600 Lab 3

Lab 3 exposed us to some assembly code. We were instructed to create a simple hello world program in C and investigate the assembly code to see how the compiler generates the instructions for the machine. Each group of 4/5 people were tasked with turning certain parameters on/off and making additional tweaks to the program and then see how those changes affected the actual machine code. Here are my observations on some of the options that we were tasked with:

  1. Add the compiler option -static while compiling
    • The static option is for used for linking by the compiler during compilation. It tells the compiler to make a static link to your libraries or use a dynamic link. Static link means copying all the code from your library into the executable and dynamic link is creating a dynamic link to the actual library that’s somewhere in the system. Now, since static link actually copies over all the code from your library into your executable, the size of the executable increases. As for the function calls and sections, they didn’t change as the static option just affected the library linkage
  2. Add the compiler option -fno-builtin while compiling
    • This option is what me and my team were assigned to. The -fno-builtin option is related to optimization. It tells the compiler to not use any built-in optimizations making it handy when you’d rather handle the optimization by yourself which you think is better than what the compiler can do. From our observations, the size of the assembly file didn’t change. The function call, however, was changed. Instead of calling the puts function (in file without the option), it called the “printf()” C library function to handle printing the string to the console/
  3. Add the compiler option -g while compiling
    • This option is related for debugging purposes. Having this option turned on while compiling tells the compiler to put in debugging information and support in the executable. Adding that extra information in increases the size of the executable by quite a lot (about 2000 bytes for this example). Majority of this information was added into the “.text” section which.
  4. Adding 10 arguments into the printf() function
    • The team was tasked with adding 10 arguments into the printf() function and notice the change in behaviour of the assembly code. The size, surprisingly, stayed the same but in the actual assembly code, additional “pushq” and “mov” calls were being made to the stack. Which means that no more registers were available so the data started being pushed into main memory.
  5. Moving the printf() into a function called output()
    • The team was tasked with moving their hello world printf string into a function called output(). This change added a separate section for the output function (right above the main) which then called the printf() function resulting in an increase of size for the assembler code.
  6. Removing -O0 and adding -O3
    • The -O0 flag means no optimizations (defautl) and -O3 means maximum optimization (speed). The compiler supports many optimization options and so adding in -O3 means adding most of them instead of manually having to type them in by ourselves. Adding these flags didn’t affect the size however did  change some of the contents. The compiler added a “xor” directive to the main section pointing to the EAX register and an “add” directive. The EAX or AX register handles most arithmetic instructions. Adding that flag in makes the program run faster, maybe for our case since our program is very simple, but also adds some arithmetic that wasn’t there initially for the computer to handle.

Overall, I learned quite a lot about how the compiler does it’s magic. Before this, I knew very little about assembly language and what the compiler actually does vs what we think it does. The compiler is very sophisticated and sometimes, really refactors your code by itself for best results without you having to do it yourself. I also learned that if you ever have any optimizations to do, don’t try to out-think the compiler. Because the compiler could actually be doing the same exact optimizations that you do, for you.

Leave a Reply

Your email address will not be published. Required fields are marked *