Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22104

What's the difference between puts() and printf() in C compiled into Assembly?

$
0
0

This is my C program using puts():

#include <stdio.h>int main(void){  puts("testing");}

After using gcc -S -o sample.s sample.c in order to compile it into Assembly, this is what I got:

        .file   "sample.c"        .section        .rodata.LC0:        .string "testing"        .text.globl main        .type   main, @functionmain:        pushl   %ebp        movl    %esp, %ebp        andl    $-16, %esp        subl    $16, %esp        movl    $.LC0, (%esp)        call    puts        leave        ret        .size   main, .-main        .ident  "GCC: (GNU) 4.4.5 20110214 (Red Hat 4.4.5-6)"        .section        .note.GNU-stack,"",@progbits

I repeated it same way, but this time I was using printf() instead of puts() and this is what I got:

    .file   "sample.c"        .section        .rodata.LC0:        .string "testing"        .text.globl main        .type   main, @functionmain:        pushl   %ebp        movl    %esp, %ebp        andl    $-16, %esp        subl    $16, %esp        movl    $.LC0, %eax        //this is the difference        movl    %eax, (%esp)        call    printf        leave        ret        .size   main, .-main        .ident  "GCC: (GNU) 4.4.5 20110214 (Red Hat 4.4.5-6)"        .section        .note.GNU-stack,"",@progbits

Here is what I don't understand: in the printf() version they mov $.LC0 to %eax, followed by mov %eax to (%esp), but in the puts() version they mov %.LC0 directly to (%esp).
I don't know why.


Viewing all articles
Browse latest Browse all 22104


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>