Generating Mixed Source and Assembly List using GCC

When do debugging and optimization, we sometimes need to generate and investigate into the assembly generated by the compiler. Generating a mixed source and assembly list will help the programmer a lot for debugging and optimization. gcc can achieve this by working the the assembler.

Generate assembly list mixed with the source code

Just add these gcc compile options:

-Wa,-adhln -g

The command:

$ gcc -Wa,-adhln -g source_code.c > assembly_list.s

The options:

-g: Produce debugging information
-Wa,option: Pass option as an option to the assembler
-adhln:
a: turn on listings
d: omit debugging directives; n: omit forms processing
h: include high-level source
l: include assembly

One example

The source code:

helloworld.c:

#include <stdio.h>
int main()
{
  printf("Hello world!\n");
  return 0;
}

The command:

$ gcc -Wa,-adhln -g helloworld.c > helloworld.s

helloworld.s:

    ...
    
    0:helloworld.c  **** #include <stdio.h>
    1:helloworld.c  ****
    2:helloworld.c  **** int main()
    3:helloworld.c  **** {
    19                            .loc 1 4 0
    20                            .cfi_startproc
    21 0000 55                    pushq   %rbp
    22                    .LCFI0:
    23                            .cfi_def_cfa_offset 16
    24 0001 4889E5                movq    %rsp, %rbp
    25                            .cfi_offset 6, -16
    26                    .LCFI1:
    27                            .cfi_def_cfa_register 6
    4:helloworld.c  ****   printf("Hello world!\n");
    28                            .loc 1 5 0
    29 0004 BF000000              movl    $.LC0, %edi
    29      00
    30 0009 E8000000              call    puts
    30      00
    5:helloworld.c  ****   return 0;
    31                            .loc 1 6 0
    32 000e B8000000              movl    $0, %eax
    32      00
    6:helloworld.c  **** }
    
    ...

2 thoughts on “Generating Mixed Source and Assembly List using GCC”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Click to subscribe to Fclose.com Posts by Email if you have not.