473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

[Question]activities of stack pointer and frame pointer when function called

Thanks your reply.
The article I read is from
http://www.hakin9.org/en/attachments...verflow_en.pdf. [at page 5]
And you're right. I don't know it very clearly. And that's why I want
to understand it; for it's useful to help me to solve some basic
problem which I may not perceive before.
I appreciate your help,
sincerely.

Stack frames and things are about how a function call is implemented
by a specific compiler, not necessarily a C compiler, and as such is OTin clc. However, I believe that understanding these practical aspectsof life often helps better understand (the design of) a language itself(...and people both from clc and csc as well). I hope a general answerto this question is not much inappropriate here.

anonymous <ne*******@yahoo.com.tw> wrote:
I'm new to programming c; and have few questions. hope could get
answers from here.
My question is ( I read some post) when a function called the frame
pointer would be pushed onto the stack and then the previously stack pointer becomes the new frame pointer. I'm not very sure how?


Your description doesn't make much sense.
Next time supply some pointers to the article you read, it'll be
easier to answer your query.
or you
can say i don't understand this activities at all.


I suspect you don't understand what as stack frame is.

First try to read:
http://en.wikipedia.org/wiki/Stack_frame
(this is not a particularly good description, but you have
to start somewhere).

Then try to google for web pages with these keywords:
stack frame function call convention

Then if you have more questions, come back here, and I'll prepare
a longer answer.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

Nov 14 '05 #1
4 3572
Great.

Thank you too.

Nov 14 '05 #2
Please don't top-post. Top-posting is considered rude here.
[top-posting fixed]

anonymous <ne*******@yahoo.com.tw> wrote:
[snip]
anonymous <ne*******@yahoo.com.tw> wrote:
I'm new to programming c; and have few questions. hope could get
answers from here.
My question is ( I read some post) when a function called the frame
You have used the word "post", which here usually means "article/message
sent to Usenet". You have mislead me (and probably others), because
what you are (later) referring to is an Internet-published article.
With questions about an article you should go to the proper forum which
the publishing site has dedicated for it.
pointer would be pushed onto the stack and then the previously stack pointer becomes the new frame pointer. I'm not very sure how?
Your description doesn't make much sense.
Next time supply some pointers to the article you read, it'll be
easier to answer your query.
or you
can say i don't understand this activities at all.


I suspect you don't understand what as stack frame is.

First try to read:

[snip]
Thanks your reply.
The article I read is from
http://www.hakin9.org/en/attachments...verflow_en.pdf. [at page 5]
And you're right. I don't know it very clearly. And that's why I want
to understand it; for it's useful to help me to solve some basic
problem which I may not perceive before.
I appreciate your help,
sincerely.


The article ("Overflowing the stack on Linux x86" by Piotr Sobolewski)
deals with a specific implementation on a specific OS. In this context
your question is really OT, because what you ask about is how a function
call in C language is implemented by gcc on Linux-i386 (and how you can
(ab)use it for your own mischievous or educational purposes); it could
just as well be any other functional language. Since I promised,
I'll give you an answer, but I'll try to keep within certain bounds
of generality.
[very OT]
What I write below is OT, I might be slightly wrong, quite wrong, or
I might totally make an idiot of myself. Don't rely on anything I've
written. I intend this to be only a general introduction to how a function
call in C might be implemented. I try to present just one out of many
possibilities. It is also very simplified. It's surely of no
interest to most c.l.c. members.

In the C language each function "owns" its automatic (local) variables
and arguments. You can call a function recursively; the new invocation
obtains a new instance of its variables (they are new objects, although
they are referred to with the same identifiers in the C code); when
you return to the previous invocation, the values of its variables and
arguments are restored.

A processor is a very simple device; it has only a finite number of
registers, whereas the number of recursive function calls is unlimited
(well, actually it is). We have to use memory to store local context
(variables, arguments and everything else that belongs to the function
invocation). Memory is only one-dimensional, and the number of parameters
and local variables may vary from function to function (and is also
unlimited). A function stack frame is a method to keep track of function
calls and their context. It has nothing to do with the languages itself,
it's just how a compiler implements it. Another compiler might do it
a completely different way.

A frame is simply a pool of data that form the context. When we call
a function, we only need to prepare a new frame (pool) for the called
function and just jump into it. At the point of creation the caller is
responsible for setting up some data (arguments), and for some other
(local variables) - it's the callee. When the callee returns to its
caller, it only has to restore the previous context (frame) and just
jump back to the position where the call was made (and also to mark the
previous frame as available for reuse). From there, the caller should
be able to continue as if nothing happened (almost).

In the real machine (with 1-dim memory) frames are often kept on stack
(this means both an abstract LIFO structure, and hardware facilities
to access memory in a stack like manner). A stack frame might look
like this:
--- (higher addresses)
Arguments
<-- FP (frame pointer)
Local Variables
--- (lower addresses)
One register FP (that's in the processor) is specially dedicated to
keeping track of function calls, and points into the middle of the frame.
Above that point are arguments, below - variables; in the machine code
when a function wants to access an argument, it adds some offset to the
address kept in FP, if it wants a variable it subtracts something from FP
(it knows what to add or subtract, because for each function that has been
decided by the compiler at the time of translation). When a function is
called, the caller pushes on stack Arguments, sets new value of FP and
jumps into the callee. The callee sets Local Variables and does its
own stuff. At this point we have two frames:
---
Arguments caller's context

Local Variables
---
Arguments current (callee's) context
<-- FP
Local Variables
---

When the callee returns it just has to reset FP to the old value and
leave its own frame alone (it'll be overwritten by next function calls).

But how does the callee know the old value of FP and what is the address
it is to return to (from which it was called)? That information is
also remembered in the stack frame too; we need more information than just
Arguments and Variables. So the stack frame might look like this:
---
Arguments
Old FP <-- FP
Return address
Local Variables
---
Each caller, when preparing a new frame for a callee additionally pushes
on stack the current value of FP (which it perfectly knows) and the
return address (this will usually be helped by a machine instruction).
The callee (and every other function) references its arguments and
variables relative to the current address in FP, and it also has the
old FP value and return address (these may be considered as a kind of
arguments too), so now it knows how to return to its caller.

The caller machine-code might look like:
push Arguments
push FP
FP = SP ; SP = stack pointer, current end of stack,
; SP is modified automatically by push and pop instructions
call callee ; pushes IP (instruction pointer) on stack and jumps
; to callee
The callee code might look like:
*(FP-8) = 7 ; set variables (access is relative to FP value)
*(FP-12) = 11 ;
*(SP+4) ; access argument
and the return part of the callee might look like:
AX = *(FP-4) ; remember return-address in a register
FP = *(FP) ; restore the old FP value
IP = AX ; jump to where we came from
; the caller will have to adjust SP (it would be better if
; we saved old SP value in the stack frame as well)

The moral: the stack operations are little more than handling linked
list of objects (frames), in a stack-like manner. I haven't told yet
many other details, like returning a value, dynamic stack allocations
(non-standard alloca, or VLA), saving other data, variadic functions etc.
An implementation might have more than one stack, or no stack at
all. A stack might grow upwards or downwards (I assumed downwards).
There might be no stack frame at all (often referred to as "fastcall").
A stack frame might be a dynamic "structure", ie. frames might look
different for different invocations of the *same* function. These are
just a few little problems that are up to the compiler to solve in order
to fulfill the C language and optimization demands, and have nothing to
do with the language itself (the solution will usually be driven by the
hardware architecture, conventions and common sense; this is not hi-tech).

It is now a good time to define what a stack frame is: it is a known
_layout_ of data, in which current function invocation context is stored.
It's worth to note that a debugger must know this layout (among other
things) in order to be able to inspect the current state of a program;
it might work with one compiler, it might not with another (usually there
are some "standards", so it's probably not a problem in practice).

I haven't answered your question directly, but the answer is there.

Advice: it's worth to know roughly the things I said above, but it's not
essential for learning C. If you're interested more in the subject,
study Assembly language(s). Your question is OT here, if you have more
similar questions ask them in a news group dedicated to a particular
compiler, operating system or hardware architecture.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #3
Sorry, I'm aware of it, for originally I simply want to know how a
function was called in C language generally; not relying on any
specific underlying detail implementation. However, being lack of such
background knowledge I post to the wrong forum. I apologize to it.
And thanks your help, sincerely.

"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> wrote in message news:<35*************@individual.net>...
Local Variables
---
Arguments current (callee's) context
<-- FP
Local Variables
---

When the callee returns it just has to reset FP to the old value and
leave its own frame alone (it'll be overwritten by next function calls).

But how does the callee know the old value of FP and what is the address
it is to return to (from which it was called)? That information is
also remembered in the stack frame too; we need more information than just
Arguments and Variables. So the stack frame might look like this:
---
Arguments
Old FP <-- FP
Return address
Local Variables
---
Each caller, when preparing a new frame for a callee additionally pushes
on stack the current value of FP (which it perfectly knows) and the
return address (this will usually be helped by a machine instruction).
The callee (and every other function) references its arguments and
variables relative to the current address in FP, and it also has the
old FP value and return address (these may be considered as a kind of
arguments too), so now it knows how to return to its caller.

....
Nov 14 '05 #4
anonymous <ne*******@yahoo.com.tw> wrote:
Sorry, I'm aware of it, for originally I simply want to know how a
function was called in C language generally; not relying on any
specific underlying detail implementation. However, being lack of such
background knowledge I post to the wrong forum. I apologize to it.
No problem. I tried to be general enough in my reply and give you
a few directions.
And thanks your help, sincerely.


You're welcome with your C questions here.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone...
7
by: NS Develop | last post by:
What's wrong with the following: void SomeClass::someMethod(void) { A *ptrA = NULL; ptrA = &A(); .... }
3
by: anonymous | last post by:
I'm new to programming c; and have few questions. hope could get answers from here. My question is ( I read some post) when a function called the frame pointer would be pushed onto the stack and...
10
by: Steven Blair | last post by:
Hi, Quick overview of the problem: public bool Something( out DataSet ds ) { bool ret=false; try {
1
by: Chuck | last post by:
I am creating a pub/sub broker implementation in C# and I am having some trouble understanding how I should best implement the invoke portion in order to make sure it is as thread-safe as it can...
0
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on...
5
by: xchong.zhou | last post by:
I have a question when I work on Linkedserver The Linkedserver name is ,the Datebase name is Newexec,the Table name is Customers_CoypTest The SQLScript is below: Update ...
6
by: sylcheung | last post by:
Hi, I am new to STL, and I have a memory allocation regarding using ostringstream in STL. I have a class called 'Rect' and it has a method like this: string Rect::toString() {...
3
by: amadain | last post by:
Help. I'm new to php. I wrote a function that did not seem to work so I checked whether the parameter I was passing to the function was actually being used. I found that it wasn't. When I run the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.