Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ CodeBlocks disassembly; Way too much code?

Tags:

c++

x86

assembly

I ran the debugger on CodeBlocks and viewed the disassembly window.

The full source code for the program I debugged is the following:

int main(){}

and the assembly code I saw in the window was this:

00401020    push   %ebp
00401021    mov    %esp,%ebp
00401023    push   %ebx
00401024    sub    $0x34,%esp
00401027    movl   $0x401150,(%esp)
0040102E    call   0x401984 <SetUnhandledExceptionFilter@4>
00401033    sub    $0x4,%esp
00401036    call   0x401330 <__cpu_features_init>
0040103B    call   0x401740 <fpreset>
00401040    lea    -0x10(%ebp),%eax
00401043    movl   $0x0,-0x10(%ebp)
0040104A    mov    %eax,0x10(%esp)
0040104E    mov    0x402000,%eax
00401053    movl   $0x404004,0x4(%esp)
0040105B    movl   $0x404000,(%esp)
00401062    mov    %eax,0xc(%esp)
00401066    lea    -0xc(%ebp),%eax
00401069    mov    %eax,0x8(%esp)
0040106D    call   0x40192c <__getmainargs>
00401072    mov    0x404008,%eax
00401077    test   %eax,%eax
00401079    jne    0x4010c5 <__mingw_CRTStartup+165>
0040107B    call   0x401934 <__p__fmode>
00401080    mov    0x402004,%edx
00401086    mov    %edx,(%eax)
00401088    call   0x4014f0 <_pei386_runtime_relocator>
0040108D    and    $0xfffffff0,%esp
00401090    call   0x401720 <__main>
00401095    call   0x40193c <__p__environ>
0040109A    mov    (%eax),%eax
0040109C    mov    %eax,0x8(%esp)
004010A0    mov    0x404004,%eax
004010A5    mov    %eax,0x4(%esp)
004010A9    mov    0x404000,%eax
004010AE    mov    %eax,(%esp)
004010B1    call   0x401318 <main>
004010B6    mov    %eax,%ebx
004010B8    call   0x401944 <_cexit>
004010BD    mov    %ebx,(%esp)
004010C0    call   0x40198c <ExitProcess@4>
004010C5    mov    0x4050f4,%ebx
004010CB    mov    %eax,0x402004
004010D0    mov    %eax,0x4(%esp)
004010D4    mov    0x10(%ebx),%eax
004010D7    mov    %eax,(%esp)
004010DA    call   0x40194c <_setmode>
004010DF    mov    0x404008,%eax
004010E4    mov    %eax,0x4(%esp)
004010E8    mov    0x30(%ebx),%eax
004010EB    mov    %eax,(%esp)
004010EE    call   0x40194c <_setmode>
004010F3    mov    0x404008,%eax
004010F8    mov    %eax,0x4(%esp)
004010FC    mov    0x50(%ebx),%eax
004010FF    mov    %eax,(%esp)
00401102    call   0x40194c <_setmode>
00401107    jmp    0x40107b <__mingw_CRTStartup+91>
0040110C    lea    0x0(%esi,%eiz,1),%esi

Is it normal to get this much assembly code from so little C++ code?

By normal, I mean is this close to the average amount of assembly code the MinGW compiler generates relative to the amount of C++ source code I provided above?

like image 772
Dziugas Avatar asked Dec 17 '14 02:12

Dziugas


1 Answers

Yes, this is fairly typical startup/shutdown code.

Before your main runs, a few things need to happen:

  1. stdin/stdout/stderr get opened
  2. cin/cout/cerr/clog get opened, referring to stdin/stdout/stderr
  3. Any static objects you define get initialized
  4. command line gets parsed to produce argc/argv
  5. environment gets retrieved (maybe)

Likewise, after your main exits, a few more things have to happen:

  1. Anything set up with atexit gets run
  2. Your static objects get destroyed
  3. cin/cout/cerr/clog get destroyed
  4. all open output streams get flushed and closed
  5. all open input streams get closed

Depending on the platform, there may be a few more things as well, such as setting up some default exception handlers (for either C++ exceptions, some platform-specific exceptions, or both).

Note that most of this is fixed code that gets linked into essentially every program, regardless of what it does or doesn't contain. In theory, they can use some tricks (e.g., "weak externals") to avoid linking in some of this code when it isn't needed, but most of what's above is used so close to universally (and the code to handle it is sufficiently trivial) that it's pretty rare to bother going to any work to eliminate this little bit of code, even when it's not going to be used (like your case, where nothing gets used at all).

Note that what you've shown is startup/shutdown code though. It's linked into your program, traditionally from a file named something like crt0 (along with, perhaps, some additional files).

If you look through your file for the code generated for main itself, you'll probably find that it's a lot shorter--possibly as short and simple as just ret. It may be so tiny that you missed the fact that it's there at all though.

like image 93
Jerry Coffin Avatar answered Oct 12 '22 12:10

Jerry Coffin