Stack frame

From Academic Kids

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
  show a

  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
  show a

  load a 10
  jump square
  show a:

  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
  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


Academic Kids Menu

  • Art and Cultures
    • Art (
    • Architecture (
    • Cultures (
    • Music (
    • Musical Instruments (
  • Biographies (
  • Clipart (
  • Geography (
    • Countries of the World (
    • Maps (
    • Flags (
    • Continents (
  • History (
    • Ancient Civilizations (
    • Industrial Revolution (
    • Middle Ages (
    • Prehistory (
    • Renaissance (
    • Timelines (
    • United States (
    • Wars (
    • World History (
  • Human Body (
  • Mathematics (
  • Reference (
  • Science (
    • Animals (
    • Aviation (
    • Dinosaurs (
    • Earth (
    • Inventions (
    • Physical Science (
    • Plants (
    • Scientists (
  • Social Studies (
    • Anthropology (
    • Economics (
    • Government (
    • Religion (
    • Holidays (
  • Space and Astronomy
    • Solar System (
    • Planets (
  • Sports (
  • Timelines (
  • Weather (
  • US States (


  • Home Page (
  • Contact Us (

  • Clip Art (
Personal tools