为什么编译器要把函数的参数复制到函数的堆栈中
本帖最后由 nwcfafniw 于 2013-11-06 13:16:07 编辑 C程序如下
#include <stdio.h>
void func(long long a) { }
int main()
{
func(0x400000002);
return 0;
}
.file"test.c"
.text
.globlfunc
.typefunc, @function
func:
.LFB0:
.cfi_startproc
pushl%ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl%esp, %ebp
.cfi_def_cfa_register 5
subl$8, %esp
movl8(%ebp), %eax ;; 这句
movl%eax, -8(%ebp) ;; 这句
movl12(%ebp), %eax ;; 这句
movl%eax, -4(%ebp) ;; 这句
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.sizefunc, .-func
.globlmain
.typemain, @function
main:
.LFB1:
.cfi_startproc
pushl%ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl%esp, %ebp
.cfi_def_cfa_register 5
andl$-8, %esp
subl$8, %esp
movl$2, (%esp)
movl$4, 4(%esp)
callfunc
movl$0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE1:
.sizemain, .-main
.ident"GCC: (GNU) 4.7.0 20120507 (Red Hat 4.7.0-5)"
.section.note.GNU-stack,"",@progbits
; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.21022.08
TITLEM:\kc1.cpp
.686P
.XMM
include listing.inc
.modelflat
INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES
PUBLIC?func@@YAX_J@Z; func
; Function compile flags: /Odtp
_TEXTSEGMENT
_a$ = 8; size = 8
?func@@YAX_J@Z PROC; func
; File m:\kc1.cpp
; Line 3
pushebp
movebp, esp
popebp
ret0
?func@@YAX_J@Z ENDP; func
_TEXTENDS
PUBLIC_main
; Function compile flags: /Odtp
_TEXTSEGMENT
_mainPROC
; Line 6
pushebp
movebp, esp
; Line 7
push4
push2
call?func@@YAX_J@Z; func
addesp, 8
; Line 8
xoreax, eax
; Line 9
popebp
ret0
_mainENDP
_TEXTENDS
END