assembly stack frames

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?
lecosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SuperdaveCommented:
Which of your homework problems do you have trouble with and what are your guesses so far?
0
shajithchandranCommented:
Read this link http://webster.cs.ucr.edu/AoA/Windows/HTML/IntermediateProceduresa2.html

I guess after reading link.. you should be able to answer your homework questions :)
0
lecosAuthor Commented:
thanks for the link. i will read it today and try to solve the question.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

SuperdaveCommented:
Good, post back if you have any specific questions on it.
0
lecosAuthor Commented:

 I'm still having a lot of problems trying to understand this code. let's begin the :

silly :    (i think that 2 arguments should be stored  somewhere in this code :  n   , *p ) . I'm having troubles visualizing the stack and its components and how it is organized. please help! this is very important .



1     pushl     %ebp                                     //                   //
2     movl      %esp,  %ebp                        //         is this the value for n ??           //  
3     subl      $20,  %esp                           // is this reserving 20 bytes for space  in the stack? //
4     pushl     %ebx                                      //  value for *p ??? //
5     movl      8(%ebp) , %ebx                      // *p is stored at this location 8(%ebp) //
6     testl      %ebx , %ebx                         //   a bitwise AND is performed , to check for 0 or 1 //
7     jle       .L3                                              // if lower or equal is jumps to L3 //
8     addl       $-8, %esp                              //   not sure how this works....//
9     leal       $-4(%ebp) , %eax                                      
10    pushl      %eax
11    leal        (%ebx, %ebx), %eax
12    pushl       %eax
13    call        silly                     // call silly from within silly?? //
14    jmp        .L4                     //    apparently here you jump to L4 which is the last stage where you will get a "return" //
15    .p2align 4, , 7

0
SuperdaveCommented:
My comments are in caps so you can find them:

1     pushl     %ebp                                     //                   //
2     movl      %esp,  %ebp                        //         is this the value for n ??           //  
                                                NO IT'S THE STACK POINTER AND BASE POINTER;
                                                PART OF THE STACK FRAME
3     subl      $20,  %esp                           // is this reserving 20 bytes for space  in the stack? //
                                                YES
4     pushl     %ebx                                      //  value for *p ??? //
                                                NO, JUST SAVING THE PREVIOUS REGISTER VALUE
5     movl      8(%ebp) , %ebx                      // *p is stored at this location 8(%ebp) //
                                                NO, n: 4(%ebp)=RETURN ADDR, 8(%ebp)=n,
                                                12(%ebp)=p
6     testl      %ebx , %ebx                         //   a bitwise AND is performed , to check for 0 or 1 //
                                                YES EXCEPT IT'S SETTING ITS FLAGS TO BE USED BY
                                                 JLE, NOT TESTING FOR 0 OR 1
7     jle       .L3                                              // if lower or equal is jumps to L3 //
                                                CORRECT
8     addl       $-8, %esp                              //   not sure how this works....//
                                                ME EITHER...
9     leal       $-4(%ebp) , %eax   THIS IS &val                                  
10    pushl      %eax
11    leal        (%ebx, %ebx), %eax    n << 2
12    pushl       %eax
13    call        silly                     // call silly from within silly?? //
                                                YES
14    jmp        .L4                     //    apparently here you jump to L4 which is the last stage where you will get a "return" //
                                                THIS IS THE END OF THE TRUE PART OF THE IF; NOW
                                                IT'S AT THE ELSE.  IT'S JUMPING TO LINE 9 IN THE
                                                SOURCE CODE
15    .p2align 4, , 7
      
0
shajithchandranCommented:
addl       $-8, %esp    
Its preparing space for passing parameters to silly function. It takes two parameters...4 bytes each.
0
SuperdaveCommented:
I don't think so, it's using push instructions to pass parameters, on lines 10 and 12.  Considering that it's allocating 32 bytes for local data when there are only 8 bytes declared (and one of them is only stored in a register), I wonder if it's trying to align the stack on cache-line boundaries.
0
lecosAuthor Commented:


Does anybody know the answers to the questions though?   I would like the answer so I may be able to understand this problem by my own and the help of my friend. Also, I don't have much time  left , thanks..
0
SuperdaveCommented:
Still seems to be homework, but I'll give you hints for all of them:

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?

See line 9 in the assembly listing.  Consider whether it's possible to write the code in a way that it could be stored in a register instead of 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?

The question should be "val2".  I don't see it anywhere in the assembly. As another hint, the return value of a function is in %eax, and you can see from the source that val2 is the return value of silly.

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

See lines 4 and 23.

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

I don't see it anywhere.
0
lecosAuthor Commented:
Hey thanks for the comments, I understand this much better. please check the comments i made , and explain better if im wrong.

thanks again



Still seems to be homework, but I'll give you hints for all of them:

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?

See line 9 in the assembly listing.  Consider whether it's possible to write the code in a way that it could be stored in a register instead of the stack.

I THINK THAT VAL IS BEING STORED IN THE STACK BECAUSE IT WILL BE USED BY SILLY FUNCTION LATER.

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?


The question should be "val2".  I don't see it anywhere in the assembly. As another hint, the return value of a function is in %eax, and you can see from the source that val2 is the return value of silly.

VAL 2 WAS NOT IN THE STACK, BUT WAS SAVED IN A REGISTER. I WAS ABLE TO NOTICED THAT VAL2 WAS BEING USED BY  %eax , I THINK THAT IT CAN BE SAVED AS A REGISTER BECAUSE ITS VALUE WONT BE USED BY THE RECURSIVE FUNCTION.

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

See lines 4 and 23.

THE VALUE OF "n"  IS STORED AT THAT ADDRESS, BUT IM NOT SURE WHY IT NEEDS TO BE PLACED IN THE STACK.  IT MAY BE RELATED TO LINE 11 BUT IM NOT  SURE.

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

I don't see it anywhere.  ME NEITHER


0
SuperdaveCommented:
I THINK THAT VAL IS BEING STORED IN THE STACK BECAUSE IT WILL BE USED BY SILLY FUNCTION LATER.

Yes, that's one reason, and there is another; consider whether you could have an equivalent of line 9 (&val in the source) if it were stored in a register instead of stack.

VAL 2 WAS NOT IN THE STACK, BUT WAS SAVED IN A REGISTER. I WAS ABLE TO NOTICED THAT VAL2 WAS BEING USED BY  %eax , I THINK THAT IT CAN BE SAVED AS A REGISTER BECAUSE ITS VALUE WONT BE USED BY THE RECURSIVE FUNCTION.

Yes.

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

See lines 4 and 23.

THE VALUE OF "n"  IS STORED AT THAT ADDRESS, BUT IM NOT SURE WHY IT NEEDS TO BE PLACED IN THE STACK.  IT MAY BE RELATED TO LINE 11 BUT IM NOT  SURE.

Not exactly, although it does allow n to be saved across recursive calls.  Line 11, and also 5 & 6, show you where n is really stored.  But then the compiler has to deal with the fact that it needs a different version of n in each recursive frame yet it put in in one register.  This is what the -24(%ebp) is related to.

ME NEITHER

I don't believe anything is stored at -8(%ebp).
      
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Assembly

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.