GCC Assembly – Finding Assembly Code for Procedure in GCC Output

assemblyc++disassemblygcclinux

I've been playing around with the assembly output switch for GCC:

gcc -S -c helloworld.c

helloworld.c:

#include <stdio.h>

int main(void){
    printf("Hello World!\n");
    return 0;
}

helloworld.s:

    .file   "helloworld.c"
    .section    .rodata
.LC0:
    .string "Hello World!"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $.LC0, %edi
    call    puts
    movl    $0, %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Debian 4.7.2-5) 4.7.2"
    .section    .note.GNU-stack,"",@progbits

In the output helloworld.s file, I notice that the assembly command to output the "Hello World!" text is simply:

call puts

However, the helloworld.s1 file doesn't have the puts procedure assembly code within it. Where would I be able to view this assembly code?

Best Answer

EDIT: this is not actually an answer to the original question, but since it's getting upvoted the info seems to be useful, so...


It's an optimization by GCC. Since your string does not contain any formatting characters and ends with a newline, GCC replaces the call with puts which produces the same output but is much faster (since it doesn't need to scan the string for formatting specifiers). Try something like:

int main(int argc, char *argv[]){
    printf("Hello World!\nargc=%d", argc);
    return 0;
}

And you will see your printf in the assembly.