cassemblydisassembly

Is there any C compiler to show you linked program in asm?


I am searching for a compiler or better IDE for C which could generate completely linked program, including functions from CRT, in assembler form. Is there any? I tried Visual C Express 2008. It has a disassembler, but its somewhat strange. I mean for some reason it shows many "strange" lines of code, such as mov a,#3 in about 100 lines.... It for study purposes. Thanks for tips.


Solution

  • gcc will generate ASM files. If you use gcc -Wa,-adhln -g [source.c] gcc and as will interleave the C source lines with the generated assembly code.

    clang with LLVM will generate high quality ASM files.

    Example:

    This C function:

    long Fibonacci(long x) {
      if (x == 0) return 0;  
      if (x == 1) return 1;
      return Fibonacci(x - 1) + Fibonacci(x - 2);
    }
    

    Becomes this ASM file:

    _Fibonacci:
    Leh_func_begin1:
        pushq   %rbp
    Ltmp0:
        movq    %rsp, %rbp
    Ltmp1:
        subq    $32, %rsp
    Ltmp2:
        movq    %rdi, -16(%rbp)
        movq    -16(%rbp), %rax
        cmpq    $0, %rax
        jne LBB1_2
        movq    $0, -8(%rbp)
        jmp LBB1_5
    LBB1_2:
        movq    -16(%rbp), %rax
        cmpq    $1, %rax
        jne LBB1_4
        movq    $1, -8(%rbp)
        jmp LBB1_5
    LBB1_4:
        movq    -16(%rbp), %rax
        movabsq $1, %rcx
        subq    %rcx, %rax
        movq    %rax, %rdi
        callq   _Fibonacci
        movq    -16(%rbp), %rcx
        movabsq $2, %rdx
        subq    %rdx, %rcx
        movq    %rcx, %rdi
        movq    %rax, -24(%rbp)
        callq   _Fibonacci
        movq    -24(%rbp), %rcx
        addq    %rax, %rcx
        movq    %rcx, -8(%rbp)
    LBB1_5:
        movq    -8(%rbp), %rax
        addq    $32, %rsp
        popq    %rbp
        ret
    Leh_func_end1:
    

    When you do this, you will want optimizations OFF.

    It is also possible to interleave the generated ASM code with the higher level source, such as the writer has done HERE.