This is my C program using puts()
:
#include <stdio.h>
int main(void){
puts("testing");
}
After using gcc -S -o sample.s sample.c
to compiled it into Assembly, this is what I got:
.file "sample.c"
.section .rodata
.LC0:
.string "testing"
.text
.globl main
.type main, @function
main:
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 did the same way, 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, @function
main:
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, the printf()
function mov $.LC0
to %eax
, then mov %eax
to (%esp)
while the puts()
function mov %.LC0
directly to (%esp)
.
I don't know why is that.