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.