AvinashK
AvinashK

Reputation: 3423

function calls during a standard c++ program execution

I have the following code:

int func(int a)
{
  int b=2;
  int c,d,e,f,g;
  //some code which involves a,b,c,d,e,f,g
} 

int main()
{
 int s=3;
 func(s);
}

Now what happens is that when main begins execution:
1.It pushes s onto stack
2.It calls func()
3.func() pushes b,c,d,e,f,g onto stack
4.Now when the code involving a,b,c,d,e,f.g is executed, in order to know the value of a all local variables of func() will have to be popped. Then a's value is retrieved. Now if again b.c.d.e.f.g are to be used, how will their values be retrieved (because they have already been popped)?

Upvotes: 1

Views: 231

Answers (3)

Abhineet
Abhineet

Reputation: 5389

The equivalent code for void func(int a)

    void func(int a)
{
00413880  push        ebp  
00413881  mov         ebp,esp 
00413883  sub         esp,108h 
00413889  push        ebx  
0041388A  push        esi  
0041388B  push        edi  
0041388C  lea         edi,[ebp-108h] 
00413892  mov         ecx,42h 
00413897  mov         eax,0CCCCCCCCh 
0041389C  rep stos    dword ptr es:[edi] 
  int b=2;
0041389E  mov         dword ptr [b],2 
  int c,d,e,f,g;
  //some code which involves a,b,c,d,e,f,g
} 

Now lets see the equivalent assembly code for the below code::

    void func(int a)
{
  int b=2;
  int c,d,e,f,g;
  c = 10 ;
  d = 15 ;
  e = 20 ;
  a = a + 2 ;
} 

Assembly Code::

    void func(int a)
{
00413880  push        ebp  
00413881  mov         ebp,esp 
00413883  sub         esp,108h 
00413889  push        ebx  
0041388A  push        esi  
0041388B  push        edi  
0041388C  lea         edi,[ebp-108h] 
00413892  mov         ecx,42h 
00413897  mov         eax,0CCCCCCCCh 
0041389C  rep stos    dword ptr es:[edi] 
  int b=2;
0041389E  mov         dword ptr [b],2 
  int c,d,e,f,g;
  c = 10 ;
004138A5  mov         dword ptr [c],0Ah 
  d = 15 ;
004138AC  mov         dword ptr [d],0Fh 
  e = 20 ;
004138B3  mov         dword ptr [e],14h 
  a = a + 2 ;
004138BA  mov         eax,dword ptr [a] 
004138BD  add         eax,2 
004138C0  mov         dword ptr [a],eax 
} 

So, although they are pushed in Stack (ESP or SP) but the pointer to each of the local variables are stored too, so that, they can be accessed easily when needed.

Like, when the code needed to use variable a, the dword ptr [a] is simply moved to register EAX.

NOTE: Technically not pushed but adjusted to fit in all variables. ( Courtesy: Joachim Pileborg)

Upvotes: 1

Kaunteya
Kaunteya

Reputation: 3090

in order to know the value of a all local variables of func() will have to be popped This line is although grammatically ambiguous. I ll assume you mean to say the varibles are popped when being used by function..

But the actual case is local variable are popped out only when function returns to the caller. Provided they are automatic. On the other hand when function wants to access(read/write) the variable. It uses the offset(distance) from the base to access them, so there is no question of variables being popped out while being accessed for evaluation.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

The local variables, as well as the argument, aren't actually pushed on the stack. Instead the compiler adds code to change the stack pointer by enough to fit all variables, and then when referencing a local variable the compiler have code to get the value from an offset from the stack pointer.

I recommend that you look at the assembler output of your example program to understand how it works.

Upvotes: 6

Related Questions