Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Al Williams

Dr. Dobb's Bloggers

See Assembly

July 14, 2014

Last time, I talked about finding the parts of your program that are costing you the most execution time. The best way to improve performance is — usually — to use a better algorithm to get your work done. However, sometimes you just have to tweak what's there to get the best bang for the buck.

The problem is, how do you know what's really there? A statement like x=x*3 might cause the compiler to generate a hardware multiplication, for example. Or it might realize that it can do the same job with a left shift and an add. Depending on the CPU architecture, one of these might be faster than the other.

Of course, you can dump the output of an executable or run it in a debugger to look at the machine code (objdump is handy and most debuggers will let you step into machine code). That's painful, though. There is a –S switch to gcc that will cause the output to be the assembly language that the compiler produces.

Starting with the program from last time, I ran:

gcc –o profiler.S –S profiler.c

The output is readable, if not uninspiring. Here's part of it (the rest is available online):

        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        subq    $16, %rsp
        movq    %rdi, -8(%rbp)
        cmpq    $1, -8(%rbp)
        jne     .L6

Of course, this is on a 64-bit Intel-based machine. Here's a sample of same code for the AVR (the brains in an Arduino):

        ldd r22,Y+1
        ldd r23,Y+2
        ldd r24,Y+3
        ldd r25,Y+4
        ldd r18,Y+5
        ldd r19,Y+6
        ldd r20,Y+7
        ldd r21,Y+8
        rcall __umulsidi3
        mov r10,r18
        mov r11,r19

Obviously, you need to understand your underlying hardware to make sense of this, much less to improve upon it. However, even to someone who knows assembly, it can be painful to relate this to your C code.

There is a way to make gcc cough up more details. Try this command line:

gcc –c –g –Wa,-ahl=profiler-a.S profiler.c

The output (in profiler-a.S) looks like this:

44                    .LFB3:
  14:profiler.c    **** 
  15:profiler.c    **** /* Recursion factorial */
  16:profiler.c    **** unsigned long b_fact(unsigned long x)
  17:profiler.c    **** {
  45                            .loc 1 17 0
  46                            .cfi_startproc
  47 0031 55                    pushq   %rbp
  48                            .cfi_def_cfa_offset 16
  49                            .cfi_offset 6, -16
  50 0032 4889E5                movq    %rsp, %rbp
  51                            .cfi_def_cfa_register 6
  52 0035 4883EC10              subq    $16, %rsp
  53 0039 48897DF8              movq    %rdi, -8(%rbp)
  18:profiler.c    ****   if (x==1) return 1; else return x*b_fact(x-1);
  54                            .loc 1 18 0
  55 003d 48837DF8              cmpq    $1, -8(%rbp)
  55      01
  56 0042 7507                  jne     .L6

That's a lot easier to read because you find the line of C code you are interested in and then see the corresponding assembly language.

There is also the –fverbose-asm flag, but I don't find it as useful:

        pushq   %rbp    #
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp      #,
        .cfi_def_cfa_register 6
        subq    $16, %rsp       #,
        movq    %rdi, -8(%rbp)  # x, x
        cmpq    $1, -8(%rbp)    #, x
        jne     .L6     #,

There's some commenting, but not nearly as much as in the first technique.

It is unlikely to happen, but these techniques can really help track down code generation bugs, as well. In some ways, that's even easier because you just have to understand what the compiler is doing (or not doing), you don't have to improve on it.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.