#include <stdio.h>
int fun(int a)
{
a += 1;
return a;
}
int main()
{
int a;
a = 1;
a = fun(a);
printf("a=%d\n", a);
a = fun(a);
printf("a=%d\n", a);
return 0;
}
debug对应汇编
fun 函数debug版对应汇编代码
PUBLIC ?fun@@YAHH@Z ; fun
; COMDAT ?fun@@YAHH@Z
_TEXT SEGMENT
_a$ = 8
?fun@@YAHH@Z PROC NEAR ; fun, COMDAT
; 4 : {
push ebp
mov ebp, esp
sub esp, 64 ; 00000040H
push ebx
push esi
push edi
;局部变量全部初始化为0xcccccccc
lea edi, DWORD PTR [ebp-64]
mov ecx, 16 ; 00000010H
mov eax, -858993460 ; ccccccccH
rep stosd
; 5 : a += 1;
mov eax, DWORD PTR _a$[ebp] ; _a$[ebp]=[ebp+8]为接收参数 [ebp+4] 为函数返回地址
add eax, 1
mov DWORD PTR _a$[ebp], eax
; 6 : return a;
mov eax, DWORD PTR _a$[ebp]
; 7 : }
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
?fun@@YAHH@Z ENDP ; fun
_TEXT ENDS
main 函数debug版对应汇编代码
; COMDAT _main
_TEXT SEGMENT
_a$ = -4
_main PROC NEAR ; COMDAT
; 10 : {
push ebp
mov ebp, esp
sub esp, 68 ; 00000044H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-68]
mov ecx, 17 ; 00000011H
mov eax, -858993460 ; ccccccccH
rep stosd
; 11 : int a;
; 12 :
; 13 : a = 1;
mov DWORD PTR _a$[ebp], 1
; 14 :
; 15 : a = fun(a);
mov eax, DWORD PTR _a$[ebp]
push eax
call ?fun@@YAHH@Z ; fun
add esp, 4
mov DWORD PTR _a$[ebp], eax
; 16 :
; 17 : printf("a=%d\n", a);
mov ecx, DWORD PTR _a$[ebp]
push ecx
push OFFSET FLAT:??_C@_05LEGA@a?$DN?$CFd?6?$AA@ ; `string'
call _printf
add esp, 8
; 18 :
; 19 : a = fun(a);
mov edx, DWORD PTR _a$[ebp]
push edx
call ?fun@@YAHH@Z ; fun
add esp, 4
mov DWORD PTR _a$[ebp], eax
; 20 :
; 21 : printf("a=%d\n", a);
mov eax, DWORD PTR _a$[ebp]
push eax
push OFFSET FLAT:??_C@_05LEGA@a?$DN?$CFd?6?$AA@ ; `string'
call _printf
add esp, 8
; 22 :
; 23 : return 0;
xor eax, eax
; 24 : }
pop edi
pop esi
pop ebx
add esp, 68 ; 00000044H
cmp ebp, esp
call __chkesp ;检查栈平衡
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END
release版对应汇编代码:
fun 函数release版对应汇编代码
PUBLIC ?fun@@YAHH@Z ; fun
; COMDAT ?fun@@YAHH@Z
_TEXT SEGMENT
_a$ = 8
?fun@@YAHH@Z PROC NEAR ; fun, COMDAT
; 5 : a += 1;
mov eax, DWORD PTR _a$[esp-4]
inc eax
; 6 : return a;
; 7 : }
ret 0
?fun@@YAHH@Z ENDP ; fun
_TEXT ENDS
main 函数release版对应汇编代码
; COMDAT _main
_TEXT SEGMENT
_main PROC NEAR ; COMDAT
; 10 : {
push esi
; 11 : int a;
; 12 :
; 13 : a = 1;
; 14 :
; 15 : a = fun(a);
push 1 ;直接把a局部变量优化掉了
call ?fun@@YAHH@Z ; fun
add esp, 4
mov esi, eax
; 16 :
; 17 : printf("a=%d\n", a);
push esi
push OFFSET FLAT:??_C@_05LEGA@a?$DN?$CFd?6?$AA@ ; `string'
call _printf
; 18 :
; 19 : a = fun(a);
push esi
call ?fun@@YAHH@Z ; fun
add esp, 12 ; 0000000cH
; 20 :
; 21 : printf("a=%d\n", a);
push eax
push OFFSET FLAT:??_C@_05LEGA@a?$DN?$CFd?6?$AA@ ; `string'
call _printf
add esp, 8
; 22 :
; 23 : return 0;
xor eax, eax
pop esi
; 24 : }
ret 0
_main ENDP
_TEXT ENDS
END
#include <stdio.h>
int fun(int a)
{
a += 1;
return a;
}
int main()
{
int a;
a = 1;
int (*f)(int);
f = fun;
a = f(a);
printf("a=%d\n", a);
a = f(a);
printf("a=%d\n", a);
return 0;
}
#include <stdio.h>
inline int fun(int a)
{
a += 1;
return a;
}
int main()
{
int a;
a = 1;
//int (*f)(int);
//f = fun;
a = fun(a);
printf("a=%d\n", a);
a = fun(a);
printf("a=%d\n", a);
return 0;
}
#include <stdio.h>
class Base
{
public:
virtual int fun(int a)
{
a += 1;
printf("Base : a=%d\n", a);
return a;
}
int fun_no_virtual(int a)
{
a += 1;
printf("Base : a=%d\n", a);
return a;
}
};
class Derived:public Base
{
public:
virtual int fun(int a)
{
a += 10;
printf("Derived : a=%d\n", a);
return a;
}
int fun_no_virtual(int a)
{
a += 10;
printf("Derived : a=%d\n", a);
return a;
}
};
int main()
{
int a;
a = 1;
Base *p = new Base;
p->fun(a);
p->fun_no_virtual(a);
a = 1;
p = new Derived;
p->fun(a);
p->fun_no_virtual(a);
return 0;
}
#include <stdio.h>
class Base
{
public:
virtual int fun(int a)
{
a += 1;
printf("Base : a=%d\n", a);
return a;
}
int fun_no_virtual(int a)
{
a += 1;
printf("Base : a=%d\n", a);
return a;
}
};
int main()
{
int a;
a = 1;
static Base base;
base.fun(a);
base.fun_no_virtual(a);
return 0;
}