Lab 2

Hi everyone,

In this lab, I will answer some questions from the lab2. First, the lab asks me to write a basic C program which prints a message on the screen, Hello World!-style — something like this:

#include <stdio.h>

int main() {
    printf("Hello World!\n");
}

This program just simply print out Hello World! to the screen, as any beginner tutorial on the internet on how to write code.

Then I compile the program using the GCC compiler that include each of these compiler options. After typing objdump, I can see the code location and output string that are set in the <main()>.

-g               # enable debugging information
-O0              # do not optimize (that's a capital letter and then the digit zero)
-fno-builtin     # do not use builtin function optimizations

 gcc lab2.c -g -O0 -fno-builtin -o lab2 

Option 1: (Add -static)

401bb5:       55                      push   %rbp
  401bb6:       48 89 e5                mov    %rsp,%rbp
  401bb9:       bf 10 00 48 00          mov    $0x480010,%edi
  401bbe:       b8 00 00 00 00          mov    $0x0,%eax
  401bc3:       e8 f8 72 00 00          callq  408ec0 <_IO_printf>
  401bc8:       b8 00 00 00 00          mov    $0x0,%eax
  401bcd:       5d                      pop    %rbp
  401bce:       c3                      retq   
  401bcf:       90                      nop

When compiling with -static option, the file size is bigger a than the original compilation without the option because it has stdio.h header file.

Option 2: (Remove -fno-builtin)

 401126:       55                      push   %rbp
  401127:       48 89 e5                mov    %rsp,%rbp
  40112a:       bf 10 20 40 00          mov    $0x402010,%edi
  40112f:       e8 fc fe ff ff          callq  401030 <puts@plt>
  401134:       b8 00 00 00 00          mov    $0x0,%eax
  401139:       5d                      pop    %rbp
  40113a:       c3                      retq   
  40113b:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)

When compiling without -fno-builtin, the file size returns smaller size, the function call changes from <printf@plt> to <puts@plt>.

Option 3: (Remove -g)

When compiling without -g option, the file size becomes even smaller than the previous files and there are no mire debugger outputs. By disabling the debugging information option, the disassembly output does not include the contents of section .debug_str.

Option 4: (Add additional arguments to printf())

401126:       55                      push   %rbp
  401127:       48 89 e5                mov    %rsp,%rbp
  40112a:       48 83 ec 08             sub    $0x8,%rsp
  40112e:       6a 0a                   pushq  $0xa
  401130:       6a 09                   pushq  $0x9
  401132:       6a 08                   pushq  $0x8
  401134:       6a 07                   pushq  $0x7
  401136:       6a 06                   pushq  $0x6
  401138:       41 b9 05 00 00 00       mov    $0x5,%r9d
  40113e:       41 b8 04 00 00 00       mov    $0x4,%r8d
  401144:       b9 03 00 00 00          mov    $0x3,%ecx
  401149:       ba 02 00 00 00          mov    $0x2,%edx
  40114e:       be 01 00 00 00          mov    $0x1,%esi
  401153:       bf 10 20 40 00          mov    $0x402010,%edi
  401158:       b8 00 00 00 00          mov    $0x0,%eax
  40115d:       e8 ce fe ff ff          callq  401030 <printf@plt>
  401162:       48 83 c4 30             add    $0x30,%rsp
  401166:       b8 00 00 00 00          mov    $0x0,%eax
  40116b:       c9                      leaveq 
  40116c:       c3                      retq   
  40116d:       0f 1f 00                nopl   (%rax)

Compilation with additional arguments in the printf did not create any changes. It remained the same as there was not any change in the compilation options.

Option 5: (Move printf() to separate function call)

40113c:       55                      push   %rbp
  40113d:       48 89 e5                mov    %rsp,%rbp
  401140:       b8 00 00 00 00          mov    $0x0,%eax
  401145:       e8 dc ff ff ff          callq  401126 <output>
  40114a:       b8 00 00 00 00          mov    $0x0,%eax
  40114f:       5d                      pop    %rbp
  401150:       c3                      retq   
  401151:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  401158:       00 00 00 
  40115b:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)

In the regular compilation the printf() statement and its content is displayed in the main() code. While during the changed file compilation, the main() function only displays the call to the output() function without displaying any of the content of the output() function.

Option 6: (Remove -O0 and add -O3)

401126:       55                      push   %rbp
  401127:       48 89 e5                mov    %rsp,%rbp
  40112a:       bf 10 20 40 00          mov    $0x402010,%edi
  40112f:       b8 00 00 00 00          mov    $0x0,%eax
  401134:       e8 f7 fe ff ff          callq  401030 <printf@plt>
  401139:       b8 00 00 00 00          mov    $0x0,%eax
  40113e:       5d                      pop    %rbp
  40113f:       c3                      retq   

Replace the -Oo option with the O3 caused the main() function to compile much earlier in the code, increase the optimization and performance.

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 )

Google photo

You are commenting using your Google 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