Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help needed for understanding Stack frame

Status
Not open for further replies.
Advanced Member level 6
Joined
Dec 4, 2012
Messages
4,280
Helped
822
Reputation
1,654
Reaction score
791
Trophy points
1,393
Location
Bangalore, India
Activity points
0
I have a C/C++ code like below


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Function prototype
int _sum(int _op1, int _op2);
 
//Main Function
int main() {
 
    int op1, op2, sum;
 
    op1 = 25;
    op2 = 75;
 
    //Calling function
    sum = _sum(op1, op2);
 
    return (0);
    
}
 
//Function Definition
//Called function
int _sum(int _op1, int _op2) {
 
    int result;
 
    result = _op1 + _op2;
    return result;
 
}



When calling function is executed first the value 75 is pushed to the stack and then value 25 is pushed to the stack. Then return address is pushed on to the stack. Return address will be the address of the next instruction after the calling function. Right? How does the return address calculated?

Then ebp, esi, edi are pushed on the stack and ebp is set to esp. So, ebp and esp will be pointing to the top of the stack which contains edi.

Then when the called function is executed, a local variable result is created on the stack and stack will be pointing to result variable.

then values of _op1 and _op2 on the stack is referenced and value for result is computed and stored in result variable on the stack.

How is the result returned to the calling function?
Is _sum(op1, op2) the calling function or is it main() the calling function?
Is the function definition of _sum() the called function or is it _sum() in the main() the called function?

See the asm code below and complete the process after executing the _sum() function


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
push 75
push 25
push return address
push ebp
mov ebp, esp
push esi
push edi
push result
mov ax, 25
add ax, 75
mov [result], ax
        .
        .
        .
pop edi
pop esi
mov esp, ebp
pop edi
pop esi
ret



Where actually the stack frame gets created? Is it when mov ebp, esp is executed?
old value of esp is stored in ebp and then ebp is used to reference the variables on the stack but ebp never changes but esp changes during stack operation. Finally when returning from the function esp is assigned its old value which is in ebp. Right?

In the asm code show how value of result is returned to main function?
 
Last edited:

In the asm code show how value of result is returned to main function?
Assembler listings don't show how machine instructions ('ret' in this case) work.
You could look at the Instruction set reference manual for the processor that you are using
(you didn't mention it), to learn what 'ret' does.

If you're curious to see the behavior at this low level (which most people aren't, but is certainly
useful to help explain exactly how C instructions functions, which in turn may help people
write better code), then your 2 main sources of information should be:
1. The instruction set reference manual
2. A simulator or debugger - you will be able to step through the code and visibly observe the memory contents and
register contents (SP and PC would obviously be relevant) changing

EDIT - sorry I misread that you wanted to know how program control returns to the main function, but
actually you want to know how the return(x) value is returned to the main function.
The secret is to check up the 'calling convention' of the compiler in question.
See here for info on this.

EDIT2 - Seems the wikipedia link is not too great. See here (PDF doc) for a better explanation. Note that it is
device and compiler specific. So interpret this as an example.
 
Last edited:

Thanks for replying. The question is regarding Pentium, Pro, 2, 3, 4, Core2Duo, Core2Quad, i5, i7 processors. In general x86 processors.

I want to know does mov ebp, esp sets a new stack frame in stack segment?
I wamt to know how the value of variable result which is on the stack returned to main function?

Here is a video but it doesn't explain fully. http://www.youtube.com/watch?v=vcfQVwtoyHY
http://www.youtube.com/watch?v=wlJJgjdgvYE
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top