We help IT Professionals succeed at work.
Get Started

assembly stack frames

lecos
lecos asked
on
1,275 Views
Last Modified: 2012-06-27
I got this problem related to stack frames and  I need some help.  tips or comments are welcome.



1 int silly(int n , int *p)
2 {
3        int val , val2;
4        if ( n > 0)
5                  val2 = silly( n << 1 , &val);
6        else
7                  val = val2 = 0;
8        
9        *p = val + val2 + n;
10
11       return val + val2;





silly :

1     pushl     %ebp
2     movl      %esp,  %ebp
3     subl      $20,  %esp
4     pushl     %ebx
5     movl      8(%ebp) , %ebx
6     testl      %ebx , %ebx
7     jle       .L3
8     addl       $-8, %esp
9     leal       $-4(%ebp) , %eax
10    pushl      %eax
11    leal        (%ebx, %ebx), %eax
12    pushl       %eax
13    call        silly
14    jmp        .L4  
15    .p2align 4, , 7


.L3

16    xorl         %eax , %eax    
17    movl         %eax , -4(%ebp)

.L4
18    movl          $-4(%ebp) , %edx
19    addl          %eax , %edx
20    movl          12(%ebp) , %eax
21    addl          %edx , %ebx
22    movl          %ebx, (%eax)
23    movl          -24(%ebp),%ebx
24    movl          %edx ,%eax
25    movl          %ebp , %esp
26    popl          %ebp
      ret





a) is the variable var stored on the stack? if so, at what byte offset (relative to %ebp) is it stored,
and why is it necessary to store it on the stack?

b) is the variable var2 stored on the stack? if so, at what byte offset(relative to %ebp) is it stored,
and why is it necessary to store it on the stack?

c)  what if anything is stored at -24 (%ebp)?  if something is stored there,
 why is it necessary to store it there?

d)  what if anything is stored at -8 (%ebp)?  if something is stored there,
 why is it necessary to store it there?
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE