If you returned a struct object, what would the return statement do? Here is the test code
#include <iostream>
using namespace Std;
struct BIG
{
Char buf[100];
int i;
Long D;
}B,B2;
Big Bigfun (Big B)
{
b.i=100;
return b;
}
int main ()
{
B2=bigfun (B);
return 0;
}
To set a breakpoint at the beginning and end of main
8:int Main ()
19: {
004012A0 Push EBP
004012A1 mov Ebp,esp
004012a3 Sub esp,118h
Puzzled at first, and analyzed for a long time
The original (118h-40h) remaining memory block holds two big variables
Low address put bigfun () function returns the temporary variable of the value
High Address b2.operator= (big &) parameters
004012A9 push EBX
004012AA push ESI
004012AB Push EDI
004012AC Lea edi,[ebp-118h]
004012B2 mov ecx,46h
004012B7 mov eax,0cccccccch
004012BC Rep stos dword ptr [edi]
20:b2=bigfun (B);
004012BE Sub esp,6ch//b parameter into stack
004012C1 mov ecx,1bh
004012C6 mov esi,offset B (00438490)//b's first address
004012CB mov Edi,esp
004012CD Rep movs dword ptr [Edi],dword ptr [esi]//above these instructions complete initialization of B
004012CF Lea Eax,[ebp-0d8h]//eax store Big Type the first address of a return value temporary variable
004012D5 Push eax//note General stack calls without this instruction
004012D6 call @ILT +0 (Bigfun) (00401005)
/*
004012DB Add esp,70h
004012DE mov esi,eax
004012E0 mov ecx,1bh
004012E5 Lea Edi,[ebp-6ch]
004012E8 Rep movs dword ptr [Edi],dword ptr [esi]
004012EA mov ecx,1bh
004012EF Lea Esi,[ebp-6ch]
004012F2 mov edi,offset B2 (00438500)
004012f7 Rep movs dword ptr [Edi],dword ptr [esi]
21st:
22:return 0;
004012f9 XOR Eax,eax
23:
24:}
004012FB Pop EDI
004012FC pop ESI
004012FD pop ebx
004012FE Add esp,118h
00401304 CMP EBP,ESP
00401306 call __chkesp (004081E0)
0040130B mov esp,ebp
0040130D Pop EBP
0040130E ret
*/
@ILT +0 (? bigfun@ @YA? aubig@ @U1 @@z):
00401005 jmp Bigfun (00401250)
11:big Bigfun (Big B)
12: {
00401250 Push EBP
00401251 mov Ebp,esp
00401253 Sub esp,40h
00401256 push EBX
00401257 push ESI
00401258 Push EDI
00401259 Lea edi,[ebp-40h]
0040125C mov ecx,10h
00401261 mov eax,0cccccccch
00401266 Rep stos dword ptr [edi]
13:b.i=100;
00401268 mov dword ptr [ebp+70h],64h
/*
0012fe50 00 00 00 00 64 00 00 00 00 00 00-00
*/
14:return b;
0040126F mov ecx,1bh
00401274 Lea Esi,[ebp+0ch]//esi=b's first address
00401277 mov edi,dword ptr [ebp+8]//edi= return value temporary variable's first address
0040127A Rep movs dword ptr [Edi],dword ptr [esi]
0040127C mov eax,dword ptr [ebp+8]
15:}
0040127F Pop EDI
00401280 pop ESI
00401281 pop ebx
00401282 mov esp,ebp
00401284 Pop EBP
00401285 ret
Return to main function
004012DB add esp,70h//Destroy local parameters
004012DE mov esi,eax//bigfunction () return value temporary variable's first address
004012E0 mov ecx,1bh
004012E5 Lea edi,[ebp-6ch]//b2.operator= (Big &) parameter's first address
004012E8 Rep movs dword ptr [Edi],dword ptr [esi]
004012EA mov ecx,1bh
004012EF Lea Esi,[ebp-6ch]
004012F2 mov edi,offset B2 (00438500)//B2 's first address
004012f7 Rep movs dword ptr [Edi],dword ptr [esi]
21st:
22:return 0;
004012f9 XOR Eax,eax
23:
24:}
004012FB Pop EDI
004012FC pop ESI
004012FD pop ebx
004012FE Add esp,118h
00401304 CMP EBP,ESP
00401306 call __chkesp (004081E0)
0040130B mov esp,ebp
0040130D Pop EBP
0040130E ret
Summarize:
1. The structure of the general function frame is
Local variables within a function
Ebp
Eip
function arguments
If the return value is a struct object function frame
Local variables within a function
Ebp
Eip
Returns the first address of a temporary variable of value//pay special attention to this
function arguments
(because the registers are too small to put a piece of struct memory, the first address of the temporary variable is saved).
The temporary variables for 2.struct objects are on the stack, not on the heap.