zoukankan      html  css  js  c++  java
  • C++ 汇编代码查看

    gcc 编译为汇编代码

    原始C++代码如下:

    #include<atomic>                                                                
    using namespace std;
                                                                                    
    atomic<int> a;
    int j;
                                                                                    
    int main()
    {
      int n;
                                                                                    
      a.store(1,memory_order_acquire);
      n = j;
      return 0;
    }

    编译成汇编代码:

    root@ubuntu:/data1# g++ -S -g -std=c++11 test10.cpp  -o test10.s
    root@ubuntu:/data1# cat  test10.s
    root@ubuntu:/data1# cat  test10.s | grep main -A 20
            .global main
            .type   main, %function
    main:
    .LFB331:
            .file 3 "test10.cpp"
            .loc 3 8 0
            .cfi_startproc
            stp     x29, x30, [sp, -32]!
            .cfi_def_cfa_offset 32
            .cfi_offset 29, -32
            .cfi_offset 30, -24
            add     x29, sp, 0
            .cfi_def_cfa_register 29
            mov     w0, 1
            str     w0, [x29, 20]
            mov     w0, 2
            str     w0, [x29, 24]
    .LBB5:
    .LBB6:
            .loc 1 369 0
            mov     w1, 65535
            ldr     w0, [x29, 24]
            bl      _ZStanSt12memory_orderSt23__memory_order_modifier
    --
            .size   main, .-main
            .align  2
            .type   _Z41__static_initialization_and_destruction_0ii, %function
    _Z41__static_initialization_and_destruction_0ii:
    .LFB351:
            .loc 3 14 0
            .cfi_startproc
            stp     x29, x30, [sp, -32]!
            .cfi_def_cfa_offset 32
            .cfi_offset 29, -32
            .cfi_offset 30, -24
            add     x29, sp, 0
            .cfi_def_cfa_register 29
            str     w0, [x29, 28]
            str     w1, [x29, 24]
            .loc 3 14 0
            ldr     w0, [x29, 28]
            cmp     w0, 1
            bne     .L9
            .loc 3 14 0 is_stmt 0 discriminator 1
            ldr     w1, [x29, 24]
    --
            .string "main"
    .LASF92:
            .string "~atomic"
    .LASF90:
            .string "atomic"
    .LASF98:
            .string "__gnu_cxx"
            .ident  "GCC: (Ubuntu/Linaro 5.5.0-12ubuntu1) 5.5.0 20171010"
            .section        .note.GNU-stack,"",@progbits

    使用 as 展示汇编代码

    另一种更好的做法是使用as
    编译成汇编代码:

    root@ubuntu:/data1# g++ -S -fverbose-asm -g  -std=c++11 test10.cpp  -o test10.s
    root@ubuntu:/data1# cat  test10.s | grep main -A 20
            .global main
            .type   main, %function
    main:
    .LFB331:
            .file 3 "test10.cpp"
            .loc 3 8 0
            .cfi_startproc
            stp     x29, x30, [sp, -32]!    //,,,
            .cfi_def_cfa_offset 32
            .cfi_offset 29, -32
            .cfi_offset 30, -24
            add     x29, sp, 0      //,,
            .cfi_def_cfa_register 29
            mov     w0, 1   // tmp78,
            str     w0, [x29, 20]   // tmp78, __i
            mov     w0, 2   // tmp79,
            str     w0, [x29, 24]   // tmp79, __m
    .LBB5:
    .LBB6:
            .loc 1 369 0
            mov     w1, 65535       //,
            ldr     w0, [x29, 24]   //, __m
            bl      _ZStanSt12memory_orderSt23__memory_order_modifier       //
    --
            .size   main, .-main
            .align  2
            .type   _Z41__static_initialization_and_destruction_0ii, %function
    _Z41__static_initialization_and_destruction_0ii:
    .LFB351:
            .loc 3 14 0
            .cfi_startproc
            stp     x29, x30, [sp, -32]!    //,,,
            .cfi_def_cfa_offset 32
            .cfi_offset 29, -32
            .cfi_offset 30, -24
            add     x29, sp, 0      //,,
            .cfi_def_cfa_register 29
            str     w0, [x29, 28]   // __initialize_p, __initialize_p
            str     w1, [x29, 24]   // __priority, __priority
            .loc 3 14 0
            ldr     w0, [x29, 28]   // tmp73, __initialize_p
            cmp     w0, 1   // tmp73,
            bne     .L9     //,
            .loc 3 14 0 is_stmt 0 discriminator 1
            ldr     w1, [x29, 24]   // tmp74, __priority
    --
            .string "main"
    .LASF92:
            .string "~atomic"
    .LASF90:
            .string "atomic"
    .LASF98:
            .string "__gnu_cxx"
            .ident  "GCC: (Ubuntu/Linaro 5.5.0-12ubuntu1) 5.5.0 20171010"
            .section        .note.GNU-stack,"",@progbits

    使用 objdump 进行反汇编

    如果直接编成机器码,需要使用objdump进行反汇编:

    root@ubuntu:/data1# g++ -save-temps -fverbose-asm -g  -std=c++11 test10.cpp  -o test10.s
    root@ubuntu:/data1# objdump -S --disassemble test10.s | grep main -A 20
    0000000000000640 <__libc_start_main@plt>:
     640:   90000090        adrp    x16, 10000 <__FRAME_END__+0xf594>
     644:   f947d211        ldr     x17, [x16, #4000]
     648:   913e8210        add     x16, x16, #0xfa0
     64c:   d61f0220        br      x17
    
    0000000000000650 <__gmon_start__@plt>:
     650:   90000090        adrp    x16, 10000 <__FRAME_END__+0xf594>
     654:   f947d611        ldr     x17, [x16, #4008]
     658:   913ea210        add     x16, x16, #0xfa8
     65c:   d61f0220        br      x17
    
    0000000000000660 <abort@plt>:
     660:   90000090        adrp    x16, 10000 <__FRAME_END__+0xf594>
     664:   f947da11        ldr     x17, [x16, #4016]
     668:   913ec210        add     x16, x16, #0xfb0
     66c:   d61f0220        br      x17
    
    Disassembly of section .text:
    
    0000000000000670 <_start>:
    --
     6a0:   97ffffe8        bl      640 <__libc_start_main@plt>
     6a4:   97ffffef        bl      660 <abort@plt>
    
    00000000000006a8 <call_weak_fn>:
     6a8:   90000080        adrp    x0, 10000 <__FRAME_END__+0xf594>
     6ac:   f947ec00        ldr     x0, [x0, #4056]
     6b0:   b4000040        cbz     x0, 6b8 <call_weak_fn+0x10>
     6b4:   17ffffe7        b       650 <__gmon_start__@plt>
     6b8:   d65f03c0        ret
     6bc:   00000000        .inst   0x00000000 ; undefined
    
    00000000000006c0 <deregister_tm_clones>:
     6c0:   b0000081        adrp    x1, 11000 <__data_start>
     6c4:   b0000080        adrp    x0, 11000 <__data_start>
     6c8:   91004021        add     x1, x1, #0x10
     6cc:   91004000        add     x0, x0, #0x10
     6d0:   91001c21        add     x1, x1, #0x7
     6d4:   cb000021        sub     x1, x1, x0
     6d8:   f100383f        cmp     x1, #0xe
     6dc:   540000a9        b.ls    6f0 <deregister_tm_clones+0x30>  // b.plast
     6e0:   90000081        adrp    x1, 10000 <__FRAME_END__+0xf594>
    --
    00000000000007b8 <main>:
                                                                                    
    atomic<int> a;
    int j;
                                                                                    
    int main()
    {
     7b8:   a9be7bfd        stp     x29, x30, [sp, #-32]!
     7bc:   910003fd        mov     x29, sp
     7c0:   52800020        mov     w0, #0x1                        // #1
     7c4:   b90017a0        str     w0, [x29, #20]
     7c8:   52800040        mov     w0, #0x2                        // #2
     7cc:   b9001ba0        str     w0, [x29, #24]
          }
    
          _GLIBCXX_ALWAYS_INLINE void
          store(__int_type __i, memory_order __m = memory_order_seq_cst) noexcept
          {
            memory_order __b = __m & __memory_order_mask;
     7d0:   529fffe1        mov     w1, #0xffff                     // #65535
     7d4:   b9401ba0        ldr     w0, [x29, #24]
     7d8:   94000025        bl      86c <_ZStanSt12memory_orderSt23__memory_order_modifier>
     7dc:   b9001fa0        str     w0, [x29, #28]
            __glibcxx_assert(__b != memory_order_acquire);
            __glibcxx_assert(__b != memory_order_acq_rel);
            __glibcxx_assert(__b != memory_order_consume);

    感觉使用objdump效果最好,但还有效果更好的。

    使用 godbolt 可视化结果

    这个网站: https://godbolt.org
    真的非常好用,效果如下:

  • 相关阅读:
    HDU 2955 Robberies(01背包)
    HDU 2602 Bone Collector(01背包)
    HUST 1352 Repetitions of Substrings(字符串)
    HUST 1358 Uiwurerirexb jeqvad(模拟解密)
    HUST 1404 Hamming Distance(字符串)
    HDU 4520 小Q系列故事――最佳裁判(STL)
    HDU 2058 The sum problem(枚举)
    【破解】修改程序版权、添加弹窗
    HDU 1407 测试你是否和LTC水平一样高(枚举)
    HDU 1050 Moving Tables(贪心)
  • 原文地址:https://www.cnblogs.com/dream397/p/14550068.html
Copyright © 2011-2022 走看看