FACTOID # 39: The eight most developed countries all speak Germanic languages.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


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


  Results from FactBites:
 
Der Stack Frame auf Intel - Prozessoren (1877 words)
Wenn Daten auf dem Stack abgelegt werden, dann "wächst" der Stack in Richtung niedrigerer Speicheradressen; dies stellt für Anfänger häufig eine mentale Hürde dar, weil ein "normales Array" in C oder C++ zum Beispiel in höhere Speicheradressen "wächst".
Daher kann sie auch nicht ihren Stack Frame selbst aufbauen, und sie kann den Stack auch nicht anschließend wieder zurückführen.
Dies ist zum Beispiel dann vorteilhaft, wenn keine oder wenige Parameter übergeben werden, oder keine oder wenige lokale Variablen existieren.
Call stack - Wikipedia, the free encyclopedia (2193 words)
Since the call stack is organized as a stack, the caller pushes the return address onto the stack, and the called subroutine, when it finishes, pops the return address off the call stack (and transfers control to that address).
The stack of such operands, rather like that in an RPN calculator, is called an evaluation stack, and may occupy space in the call stack.
For some purposes, the stack frame of a subroutine and that of its caller can be considered to overlap, the overlap consisting of the area where the parameters are passed from the caller to the callee.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.