这是我使用puts()
的C程序:
#include <stdio.h>
int main(void){
puts("testing");
}
使用gcc -S -o sample.s sample.c
将其编译为汇编代码,这是我得到的结果:
.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
我以相同的方式进行了操作,只不过这次我使用了printf()
,这是我的结果:
.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
我不理解的是,printf()
函数将 mov $.LC0
到 %eax
,然后将 %eax
移动到 (%esp)
,而 puts()
函数直接将 mov %.LC0
移动到 (%esp)
。我不知道这是为什么。
printf
是一个变参函数,与有固定参数数量的函数调用方式不同。 - Barmar