Is the assembly code of a compiled program different from what was written?

Asked

Viewed 135 times

6

I compiled this Assembly code

global  _main 
    extern  _printf

    section .text
_main:
    push    message
    call    _printf
    add     esp, 4
    ret
message:
    db  'Hello, World!', 10, 0

when I went to give u dump in code Assembly appeared to me this

hello.exe:     file format pei-i386


Disassembly of section .text:

00401000 <___mingw_CRTStartup>:
  401000:   53                      push   %ebx
  401001:   83 ec 38                sub    $0x38,%esp
  401004:   a1 24 30 40 00          mov    0x403024,%eax
  401009:   85 c0                   test   %eax,%eax
  40100b:   74 1c                   je     401029 <___mingw_CRTStartup+0x29>
  40100d:   c7 44 24 08 00 00 00    movl   $0x0,0x8(%esp)
  401014:   00 
  401015:   c7 44 24 04 02 00 00    movl   $0x2,0x4(%esp)
  40101c:   00 
  40101d:   c7 04 24 00 00 00 00    movl   $0x0,(%esp)
  401024:   ff d0                   call   *%eax
  401026:   83 ec 0c                sub    $0xc,%esp
  401029:   c7 04 24 10 11 40 00    movl   $0x401110,(%esp)
  401030:   e8 7b 0b 00 00          call   401bb0 <_SetUnhandledExceptionFilter@4>
  401035:   83 ec 04                sub    $0x4,%esp
  401038:   e8 13 04 00 00          call   401450 <___cpu_features_init>
  40103d:   e8 ee 04 00 00          call   401530 <__fpreset>
  401042:   8d 44 24 2c             lea    0x2c(%esp),%eax
  401046:   89 44 24 10             mov    %eax,0x10(%esp)
  40104a:   a1 00 20 40 00          mov    0x402000,%eax
  40104f:   c7 44 24 04 00 50 40    movl   $0x405000,0x4(%esp)
  401056:   00 
  401057:   c7 04 24 04 50 40 00    movl   $0x405004,(%esp)
  40105e:   c7 44 24 2c 00 00 00    movl   $0x0,0x2c(%esp)
  401065:   00 
  401066:   89 44 24 0c             mov    %eax,0xc(%esp)
  40106a:   8d 44 24 28             lea    0x28(%esp),%eax
  40106e:   89 44 24 08             mov    %eax,0x8(%esp)
  401072:   e8 d9 0a 00 00          call   401b50 <___getmainargs>
  401077:   a1 18 50 40 00          mov    0x405018,%eax
  40107c:   85 c0                   test   %eax,%eax
  40107e:   74 42                   je     4010c2 <___mingw_CRTStartup+0xc2>
  401080:   8b 1d fc 60 40 00       mov    0x4060fc,%ebx
  401086:   a3 04 20 40 00          mov    %eax,0x402004
  40108b:   89 44 24 04             mov    %eax,0x4(%esp)
  40108f:   8b 43 10                mov    0x10(%ebx),%eax
  401092:   89 04 24                mov    %eax,(%esp)
  401095:   e8 be 0a 00 00          call   401b58 <__setmode>
  40109a:   a1 18 50 40 00          mov    0x405018,%eax
  40109f:   89 44 24 04             mov    %eax,0x4(%esp)
  4010a3:   8b 43 30                mov    0x30(%ebx),%eax
  4010a6:   89 04 24                mov    %eax,(%esp)
  4010a9:   e8 aa 0a 00 00          call   401b58 <__setmode>
  4010ae:   a1 18 50 40 00          mov    0x405018,%eax
  4010b3:   89 44 24 04             mov    %eax,0x4(%esp)
  4010b7:   8b 43 50                mov    0x50(%ebx),%eax
  4010ba:   89 04 24                mov    %eax,(%esp)
  4010bd:   e8 96 0a 00 00          call   401b58 <__setmode>
  4010c2:   e8 99 0a 00 00          call   401b60 <___p__fmode>
  4010c7:   8b 15 04 20 40 00       mov    0x402004,%edx
  4010cd:   89 10                   mov    %edx,(%eax)
  4010cf:   e8 dc 05 00 00          call   4016b0 <__pei386_runtime_relocator>
  4010d4:   83 e4 f0                and    $0xfffffff0,%esp
  4010d7:   e8 34 08 00 00          call   401910 <___main>
  4010dc:   e8 87 0a 00 00          call   401b68 <___p__environ>
  4010e1:   8b 00                   mov    (%eax),%eax
  4010e3:   89 44 24 08             mov    %eax,0x8(%esp)
  4010e7:   a1 00 50 40 00          mov    0x405000,%eax
  4010ec:   89 44 24 04             mov    %eax,0x4(%esp)
  4010f0:   a1 04 50 40 00          mov    0x405004,%eax
  4010f5:   89 04 24                mov    %eax,(%esp)
  4010f8:   e8 53 02 00 00          call   401350 <_main>
  4010fd:   89 c3                   mov    %eax,%ebx
  4010ff:   e8 6c 0a 00 00          call   401b70 <__cexit>
  401104:   89 1c 24                mov    %ebx,(%esp)
  401107:   e8 ac 0a 00 00          call   401bb8 <_ExitProcess@4>
  40110c:   8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi

00401110 <__gnu_exception_handler@4>:
  401110:   53                      push   %ebx
  401111:   83 ec 28                sub    $0x28,%esp
  401114:   8b 44 24 30             mov    0x30(%esp),%eax
  401118:   8b 00                   mov    (%eax),%eax
  40111a:   8b 00                   mov    (%eax),%eax
  40111c:   3d 91 00 00 c0          cmp    $0xc0000091,%eax
  401121:   77 3d                   ja     401160 <__gnu_exception_handler@4+0x50>
  401123:   3d 8d 00 00 c0          cmp    $0xc000008d,%eax
  401128:   72 4d                   jb     401177 <__gnu_exception_handler@4+0x67>
  40112a:   bb 01 00 00 00          mov    $0x1,%ebx
  40112f:   c7 44 24 04 00 00 00    movl   $0x0,0x4(%esp)
  401136:   00 
  401137:   c7 04 24 08 00 00 00    movl   $0x8,(%esp)
  40113e:   e8 35 0a 00 00          call   401b78 <_signal>
  401143:   83 f8 01                cmp    $0x1,%eax
  401146:   0f 84 f4 00 00 00       je     401240 <__gnu_exception_handler@4+0x130>
  40114c:   85 c0                   test   %eax,%eax
  40114e:   0f 85 a0 00 00 00       jne    4011f4 <__gnu_exception_handler@4+0xe4>
  401154:   31 c0                   xor    %eax,%eax
  401156:   83 c4 28                add    $0x28,%esp
  401159:   5b                      pop    %ebx
  40115a:   c2 04 00                ret    $0x4
  40115d:   8d 76 00                lea    0x0(%esi),%esi
  401160:   3d 94 00 00 c0          cmp    $0xc0000094,%eax
  401165:   74 4b                   je     4011b2 <__gnu_exception_handler@4+0xa2>
  401167:   3d 96 00 00 c0          cmp    $0xc0000096,%eax
  40116c:   74 17                   je     401185 <__gnu_exception_handler@4+0x75>
  40116e:   3d 93 00 00 c0          cmp    $0xc0000093,%eax
  401173:   75 df                   jne    401154 <__gnu_exception_handler@4+0x44>
  401175:   eb b3                   jmp    40112a <__gnu_exception_handler@4+0x1a>
  401177:   3d 05 00 00 c0          cmp    $0xc0000005,%eax
  40117c:   74 42                   je     4011c0 <__gnu_exception_handler@4+0xb0>
  40117e:   3d 1d 00 00 c0          cmp    $0xc000001d,%eax
  401183:   75 cf                   jne    401154 <__gnu_exception_handler@4+0x44>
  401185:   c7 44 24 04 00 00 00    movl   $0x0,0x4(%esp)
  40118c:   00 
  40118d:   c7 04 24 04 00 00 00    movl   $0x4,(%esp)
  401194:   e8 df 09 00 00          call   401b78 <_signal>
  401199:   83 f8 01                cmp    $0x1,%eax
  40119c:   74 69                   je     401207 <__gnu_exception_handler@4+0xf7>
  40119e:   85 c0                   test   %eax,%eax
  4011a0:   74 b2                   je     401154 <__gnu_exception_handler@4+0x44>
  4011a2:   c7 04 24 04 00 00 00    movl   $0x4,(%esp)
  4011a9:   ff d0                   call   *%eax
  4011ab:   b8 ff ff ff ff          mov    $0xffffffff,%eax
  4011b0:   eb a4                   jmp    401156 <__gnu_exception_handler@4+0x46>
  4011b2:   31 db                   xor    %ebx,%ebx
  4011b4:   e9 76 ff ff ff          jmp    40112f <__gnu_exception_handler@4+0x1f>
  4011b9:   8d b4 26 00 00 00 00    lea    0x0(%esi,%eiz,1),%esi
  4011c0:   c7 44 24 04 00 00 00    movl   $0x0,0x4(%esp)
  4011c7:   00 
  4011c8:   c7 04 24 0b 00 00 00    movl   $0xb,(%esp)
  4011cf:   e8 a4 09 00 00          call   401b78 <_signal>
  4011d4:   83 f8 01                cmp    $0x1,%eax
  4011d7:   74 4a                   je     401223 <__gnu_exception_handler@4+0x113>
  4011d9:   85 c0                   test   %eax,%eax
  4011db:   0f 84 73 ff ff ff       je     401154 <__gnu_exception_handler@4+0x44>
  4011e1:   c7 04 24 0b 00 00 00    movl   $0xb,(%esp)
  4011e8:   ff d0                   call   *%eax
  4011ea:   b8 ff ff ff ff          mov    $0xffffffff,%eax
  4011ef:   e9 62 ff ff ff          jmp    401156 <__gnu_exception_handler@4+0x46>
  4011f4:   c7 04 24 08 00 00 00    movl   $0x8,(%esp)
  4011fb:   ff d0                   call   *%eax
  4011fd:   b8 ff ff ff ff          mov    $0xffffffff,%eax
  401202:   e9 4f ff ff ff          jmp    401156 <__gnu_exception_handler@4+0x46>
  401207:   c7 44 24 04 01 00 00    movl   $0x1,0x4(%esp)
  40120e:   00 
  40120f:   c7 04 24 04 00 00 00    movl   $0x4,(%esp)
  401216:   e8 5d 09 00 00          call   401b78 <_signal>
  40121b:   83 c8 ff                or     $0xffffffff,%eax
  40121e:   e9 33 ff ff ff          jmp    401156 <__gnu_exception_handler@4+0x46>
  401223:   c7 44 24 04 01 00 00    movl   $0x1,0x4(%esp)
  40122a:   00 
  40122b:   c7 04 24 0b 00 00 00    movl   $0xb,(%esp)
  401232:   e8 41 09 00 00          call   401b78 <_signal>
  401237:   83 c8 ff                or     $0xffffffff,%eax
  40123a:   e9 17 ff ff ff          jmp    401156 <__gnu_exception_handler@4+0x46>
  40123f:   90                      nop
  401240:   c7 44 24 04 01 00 00    movl   $0x1,0x4(%esp)
  401247:   00 
  401248:   c7 04 24 08 00 00 00    movl   $0x8,(%esp)
  40124f:   e8 24 09 00 00          call   401b78 <_signal>
  401254:   85 db                   test   %ebx,%ebx
  401256:   b8 ff ff ff ff          mov    $0xffffffff,%eax
  40125b:   0f 84 f5 fe ff ff       je     401156 <__gnu_exception_handler@4+0x46>
  401261:   89 44 24 1c             mov    %eax,0x1c(%esp)
  401265:   e8 c6 02 00 00          call   401530 <__fpreset>
  40126a:   8b 44 24 1c             mov    0x1c(%esp),%eax
  40126e:   e9 e3 fe ff ff          jmp    401156 <__gnu_exception_handler@4+0x46>
  401273:   8d b6 00 00 00 00       lea    0x0(%esi),%esi
  401279:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

00401280 <_mainCRTStartup>:
  401280:   83 ec 1c                sub    $0x1c,%esp
  401283:   c7 04 24 01 00 00 00    movl   $0x1,(%esp)
  40128a:   ff 15 f4 60 40 00       call   *0x4060f4
  401290:   e8 6b fd ff ff          call   401000 <___mingw_CRTStartup>
  401295:   8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
  401299:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

004012a0 <_WinMainCRTStartup>:
  4012a0:   83 ec 1c                sub    $0x1c,%esp
  4012a3:   c7 04 24 02 00 00 00    movl   $0x2,(%esp)
  4012aa:   ff 15 f4 60 40 00       call   *0x4060f4
  4012b0:   e8 4b fd ff ff          call   401000 <___mingw_CRTStartup>
  4012b5:   8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
  4012b9:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

004012c0 <_atexit>:
  4012c0:   a1 0c 61 40 00          mov    0x40610c,%eax
  4012c5:   ff e0                   jmp    *%eax
  4012c7:   89 f6                   mov    %esi,%esi
  4012c9:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

004012d0 <__onexit>:
  4012d0:   a1 00 61 40 00          mov    0x406100,%eax
  4012d5:   ff e0                   jmp    *%eax
  4012d7:   90                      nop
  4012d8:   90                      nop
  4012d9:   90                      nop
  4012da:   90                      nop
  4012db:   90                      nop
  4012dc:   90                      nop
  4012dd:   90                      nop
  4012de:   90                      nop
  4012df:   90                      nop

004012e0 <___gcc_register_frame>:
  4012e0:   55                      push   %ebp
  4012e1:   89 e5                   mov    %esp,%ebp
  4012e3:   83 ec 18                sub    $0x18,%esp
  4012e6:   a1 0c 20 40 00          mov    0x40200c,%eax
  4012eb:   85 c0                   test   %eax,%eax
  4012ed:   74 3a                   je     401329 <___gcc_register_frame+0x49>
  4012ef:   c7 04 24 00 30 40 00    movl   $0x403000,(%esp)
  4012f6:   e8 c5 08 00 00          call   401bc0 <_GetModuleHandleA@4>
  4012fb:   83 ec 04                sub    $0x4,%esp
  4012fe:   85 c0                   test   %eax,%eax
  401300:   ba 00 00 00 00          mov    $0x0,%edx
  401305:   74 15                   je     40131c <___gcc_register_frame+0x3c>
  401307:   c7 44 24 04 0e 30 40    movl   $0x40300e,0x4(%esp)
  40130e:   00 
  40130f:   89 04 24                mov    %eax,(%esp)
  401312:   e8 b1 08 00 00          call   401bc8 <_GetProcAddress@8>
  401317:   83 ec 08                sub    $0x8,%esp
  40131a:   89 c2                   mov    %eax,%edx
  40131c:   85 d2                   test   %edx,%edx
  40131e:   74 09                   je     401329 <___gcc_register_frame+0x49>
  401320:   c7 04 24 0c 20 40 00    movl   $0x40200c,(%esp)
  401327:   ff d2                   call   *%edx
  401329:   c7 04 24 40 13 40 00    movl   $0x401340,(%esp)
  401330:   e8 8b ff ff ff          call   4012c0 <_atexit>
  401335:   c9                      leave  
  401336:   c3                      ret    
  401337:   89 f6                   mov    %esi,%esi
  401339:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

00401340 <___gcc_deregister_frame>:
  401340:   55                      push   %ebp
  401341:   89 e5                   mov    %esp,%ebp
  401343:   5d                      pop    %ebp
  401344:   c3                      ret    
  401345:   90                      nop
  401346:   90                      nop
  401347:   90                      nop
  401348:   90                      nop
  401349:   90                      nop
  40134a:   90                      nop
  40134b:   90                      nop
  40134c:   90                      nop
  40134d:   90                      nop
  40134e:   90                      nop
  40134f:   90                      nop

00401350 <_main>:
  401350:   c3                      ret    
  401351:   66 90                   xchg   %ax,%ax
  401353:   66 90                   xchg   %ax,%ax
  401355:   66 90                   xchg   %ax,%ax
  401357:   66 90                   xchg   %ax,%ax
  401359:   66 90                   xchg   %ax,%ax
  40135b:   66 90                   xchg   %ax,%ax
  40135d:   66 90                   xchg   %ax,%ax
  40135f:   90                      nop

00401360 <___dyn_tls_dtor@12>:
  401360:   83 ec 1c                sub    $0x1c,%esp
  401363:   8b 44 24 24             mov    0x24(%esp),%eax
  401367:   85 c0                   test   %eax,%eax
  401369:   74 15                   je     401380 <___dyn_tls_dtor@12+0x20>
  40136b:   83 f8 03                cmp    $0x3,%eax
  40136e:   74 10                   je     401380 <___dyn_tls_dtor@12+0x20>
  401370:   b8 01 00 00 00          mov    $0x1,%eax
  401375:   83 c4 1c                add    $0x1c,%esp
  401378:   c2 0c 00                ret    $0xc
  40137b:   90                      nop
  40137c:   8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
  401380:   8b 54 24 28             mov    0x28(%esp),%edx
  401384:   89 44 24 04             mov    %eax,0x4(%esp)
  401388:   8b 44 24 20             mov    0x20(%esp),%eax
  40138c:   89 54 24 08             mov    %edx,0x8(%esp)
  401390:   89 04 24                mov    %eax,(%esp)
  401393:   e8 18 07 00 00          call   401ab0 <___mingw_TLScallback>
  401398:   b8 01 00 00 00          mov    $0x1,%eax
  40139d:   83 c4 1c                add    $0x1c,%esp
  4013a0:   c2 0c 00                ret    $0xc
  4013a3:   8d b6 00 00 00 00       lea    0x0(%esi),%esi
  4013a9:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

004013b0 <___dyn_tls_init@12>:
  4013b0:   56                      push   %esi
  4013b1:   53                      push   %ebx
  4013b2:   83 ec 14                sub    $0x14,%esp
  4013b5:   83 3d 28 50 40 00 02    cmpl   $0x2,0x405028
  4013bc:   8b 44 24 24             mov    0x24(%esp),%eax
  4013c0:   74 0a                   je     4013cc <___dyn_tls_init@12+0x1c>
  4013c2:   c7 05 28 50 40 00 02    movl   $0x2,0x405028
  4013c9:   00 00 00 
  4013cc:   83 f8 02                cmp    $0x2,%eax
  4013cf:   74 12                   je     4013e3 <___dyn_tls_init@12+0x33>
  4013d1:   83 f8 01                cmp    $0x1,%eax
  4013d4:   74 42                   je     401418 <___dyn_tls_init@12+0x68>
  4013d6:   83 c4 14                add    $0x14,%esp
  4013d9:   b8 01 00 00 00          mov    $0x1,%eax
  4013de:   5b                      pop    %ebx
  4013df:   5e                      pop    %esi
  4013e0:   c2 0c 00                ret    $0xc
  4013e3:   be 14 70 40 00          mov    $0x407014,%esi
  4013e8:   81 ee 14 70 40 00       sub    $0x407014,%esi
  4013ee:   c1 fe 02                sar    $0x2,%esi
  4013f1:   85 f6                   test   %esi,%esi
  4013f3:   7e e1                   jle    4013d6 <___dyn_tls_init@12+0x26>
  4013f5:   31 db                   xor    %ebx,%ebx
  4013f7:   8b 04 9d 14 70 40 00    mov    0x407014(,%ebx,4),%eax
  4013fe:   85 c0                   test   %eax,%eax
  401400:   74 02                   je     401404 <___dyn_tls_init@12+0x54>
  401402:   ff d0                   call   *%eax
  401404:   83 c3 01                add    $0x1,%ebx
  401407:   39 f3                   cmp    %esi,%ebx
  401409:   75 ec                   jne    4013f7 <___dyn_tls_init@12+0x47>
  40140b:   83 c4 14                add    $0x14,%esp
  40140e:   b8 01 00 00 00          mov    $0x1,%eax
  401413:   5b                      pop    %ebx
  401414:   5e                      pop    %esi
  401415:   c2 0c 00                ret    $0xc
  401418:   8b 44 24 28             mov    0x28(%esp),%eax
  40141c:   c7 44 24 04 01 00 00    movl   $0x1,0x4(%esp)
  401423:   00 
  401424:   89 44 24 08             mov    %eax,0x8(%esp)
  401428:   8b 44 24 20             mov    0x20(%esp),%eax
  40142c:   89 04 24                mov    %eax,(%esp)
  40142f:   e8 7c 06 00 00          call   401ab0 <___mingw_TLScallback>
  401434:   eb a0                   jmp    4013d6 <___dyn_tls_init@12+0x26>
  401436:   8d 76 00                lea    0x0(%esi),%esi
  401439:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

...

the code is completely different, because the code changes after compiled?

  • 2

    In fact you have not compiled the Assembly code, you "rode". It seems that some assemblers can make small optimizations, see on wikipedia article.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

6

The source code of your Assembly is cute, the binary code has no comments, no formatting, no details.

Your code does not include everything needed to run it. You took a binary that included other codes needed to run. It has settings to reduce this or even delete, but when eliminating everything will produce a binary, but not an executable.

It is possible that in the assembly process the code you wrote will be rewritten, especially if you are using some form of macro Assembler behaves like a compiler even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.