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

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools