C程序如下
#include <stdio.h>
void func(long long a) { }
int main()
{
func(0x400000002);
return 0;
}
编译成汇编语言后如下
.file "test.c"
.text
.globl func
.type func, @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
movl 8(%ebp), %eax ;; 这句
movl %eax, -8(%ebp) ;; 这句
movl 12(%ebp), %eax ;; 这句
movl %eax, -4(%ebp) ;; 这句
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size func, .-func
.globl main
.type main, @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)
call func
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (GNU) 4.7.0 20120507 (Red Hat 4.7.0-5)"
.section .note.GNU-stack,"",@progbits
麻烦各位看下func函数中有注释的那四句汇编代码,它居然把参数a复制到自己的堆
栈中了,我发现如果func的参数是int型的话则不会出现这种现象,请问各位这种设定是为
什么。。
------解决方案--------------------
vc 好像没有这样做
; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.21022.08
TITLE M:\kc1.cpp
.686P
.XMM
include listing.inc
.model flat
INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES
PUBLIC ?func@@[email protected] ; func
; Function compile flags: /Odtp
_TEXT SEGMENT
_a$ = 8 ; size = 8
?func@@[email protected] PROC ; func
; File m:\kc1.cpp
; Line 3
push ebp
mov ebp, esp
pop ebp
ret 0
?func@@[email protected] ENDP ; func
_TEXT ENDS
PUBLIC _main
; Function compile flags: /Odtp
_TEXT SEGMENT
_main PROC
; Line 6
push ebp
mov ebp, esp
; Line 7
push 4
push 2
call ?func@@[email protected] ; func
add esp, 8
; Line 8
xor eax, eax
; Line 9
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END