Upload
jean-mathews
View
219
Download
3
Embed Size (px)
Citation preview
Introduction to Computing Systemsfrom bits & gates to C & beyond
Chapter 10 Chapter 10
The Stack
Stack data structureActivation records and function invocation
(Chapter 14)
10 - 2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Stack Data StructureStack Data Structure
LIFO (last in - first out)Operations:
Push (enter item at top of stack) Pop (remove item from top of stack)
Error conditions: Underflow (try to pop from empty stack) Overflow (try to push onto full stack)
A register (eg. R6) holds address of top of stack (TOS)
10 - 3
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
PUSH & POPPUSH & POP POP, including underflow check
If (( TOS - EMPTY) == 0)
R5 = 1 /* R5 gets error code */
Else {
R0 = C[TOS]; /*R0 gets value at TOS*/
TOS = TOS - 1;
R5 = 0;
PUSH, including overflow check
If (( TOS - MAX) == 0)
R5 = 1 /* R5 gets error code */
Else {
TOS = TOS + 1;
C[TOS] = r0; /*TOS gets value in R0*/
R5 = 0;
EMPTY
TOS
MAX
10 - 4
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
PUSH & POP in LC-2 - 1PUSH & POP in LC-2 - 1POP ST R2,Sv2 ;save, needed by POP
ST R1,Sv1 ;save, needed by POP
LD R1,BASE ;BASE contains -x4000
ADD R1,R1,#1 ;R1 has -x3FFF
ADD R2,R6,R1 ;Compare SP to 3FFF
BRz fail ;Branch if stk empty
LDR R0,R6,#0 ;The actual ‘pop’
ADD R6,R6,#-1 ;Adjust stack pointer
BRnzp success
BASE .FILL xC000 ;Base has -x4000
MAX .FILL xBFFC ;Max has -x4004
10 - 5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
PUSH & POP in LC-2 - 2PUSH & POP in LC-2 - 2PUSH ST R2,Sv2 ;needed by PUSH
ST R1,Sv1 ;needed by PUSH
LD R1,MAX ;MAX has -4004
ADD R2,R6,R1 ;Compare SP to x4004
BRz fail ;Branch is stk full
ADD R6,R6,#1 ;Adjust SP
STR R0,R6,#0 ;The actual ‘push’
Sv1 .FILL x0000
Sv2 .FILL x0000
10 - 6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
PUSH & POP in LC-2 - 3PUSH & POP in LC-2 - 3
success LD R1,Sv1 ;Restore reg valuesLD R2,Sv2 ;AND R5,R5,#0 ;R5 <-- successRET;
fail LD R1,Sv1 ;Restore reg valuesLD R2,Sv2AND R5,R5,#0ADD R5,R5,#1 ;R5 <-- failRET
10 - 7
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Activation RecordsActivation Records
A RecordA collection of contiguous memory locations treated as one
entityThe struct construct in C/C++ describes a record
Activation RecordIs allocated for each function invocation in memoryThe area of memory where activation records are allocated is
called the stack memory In LC-2, R6 is the stack pointer: it points to the top of the stack
(TOS)Each element on the stack is an activation record
10 - 8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Activation RecordActivation Record
return address
return value
dynamic link
a
b
x
y
z
Formalparameters
Localvariables
function This (int a, int b){
int x, y, z;
………
function body……..
return z;}
10 - 9
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
ExampleExample
function This (int a, int b){
int x, y, z;
………
y = That(a);……..
return z;}
function That (int a){
int x;
………body……..
return x;}
10 - 10
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Example - 2Example - 2
return addressreturn valuedynamic link
abxyz
activation recordof This
R6
return addressreturn valuedynamic link
abxyz
return address
activation recordof This
x4050ax
return value activation recordof That
R6
x4050
When This is executing
After This calls That
When That terminates