Science Fair Projects Ideas - Stack frame

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Stack frame

In computing, a stack frame is a data structure used to create temporary storage for data and saved state in functions.

Say we wish to implement a function to return a number's square. We could write (in pseudo-assembler)

  load a 20
  jump square
return:
  show a
  halt
square:
  mul a a
  jump return:

This methodology does work, but only in very limited circumstances. For example consider if we wish to call square twice?

  load a 20
  jump square
return:
  show a

  load a 10
  jump square
return:
  show a:

square:
  mul a a
  jump return 

but on returning how does the function know which return label to return to? We could save the return address somewhere, and jump back to that return address, and this scheme works significantly better, however one problem remains - that all registers are essentially in use and the function could use a register with a useful value in it and thus the caller may be adversely affected.

So, functions store all temporary data in the stack. A special register storing the address of the bottom of the stack (stacks grow downward, that is, smaller addresses - this is a convention) is used and a program stores values in offsets off this stack pointer. When a function is called, the stack pointer moves down to provide enough space to store its temporary variables (other functions called will store values below the new value of the stack pointer). Values stored are often local variables and the return address. When the function returns, the stack pointer is moved back up to save space on the stack.

The stack frame is the collection of local variables and return address and other information stored on the stack.

In "pseudo-assembler" the square function may look something like (where arg is a common argument register, ret is the return value register, and jumpr is an opcode to jump and store the return address in ra)

  load arg 20
  jumpr square
  show arg
square:
  sub sp 8    // subtract 8 off the stack pointer - we want to 
              // store two 4 byte integers, a temp variable and
              // the return address
  sp[0]=ra    // store the return address
  sp[4]=a     // store the value of the temporary value a
  load a arg
  mul a a
  store ret a // calculation over, store the value in the return register
  a = sp[4]   // restore the value of a
  ra = sp[0]  // restore the value of the return address
  add sp 8    // restore the stack pointer, so any other items
              // in the stack will be in the same relative positions
  jump ra     // jump back to the return address

Note since we use the temporary register a, we store the previous value of a before we use it to prevent overwriting current values already stored in a

See also

10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice