Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation.

Similar presentations


Presentation on theme: "Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation."— Presentation transcript:

1 Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation (Chapter 14)

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 10 - 2 Stack 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)

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 10 - 3 PUSH & 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

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 10 - 4 PUSH & POP in LC-2 - 1 POP 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

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 10 - 5 PUSH & POP in LC-2 - 2 PUSH 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

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 10 - 6 PUSH & POP in LC-2 - 3 success LD R1,Sv1 ;Restore reg values LD R2,Sv2 ; AND R5,R5,#0;R5 <-- success RET ; fail LD R1,Sv1;Restore reg values LD R2,Sv2 AND R5,R5,#0 ADD R5,R5,#1;R5 <-- fail RET

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 10 - 7 Activation Records A Record  A collection of contiguous memory locations treated as one entity  The struct construct in C/C++ describes a record Activation Record  Is allocated for each function invocation in memory  The 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

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 10 - 8 Activation Record return address return value dynamic link a b x y z Formal parameters Local variables function This (int a, int b) { int x, y, z; ……… function body …….. return z; }

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 10 - 9 Example 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 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 10 - 10 Example - 2 return address return value dynamic link a b x y z activation record of This R6 return address return value dynamic link a b x y z return address activation record of This x4050 a x return value activation record of That R6 x4050 When This is executing After This calls That When That terminates


Download ppt "Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation."

Similar presentations


Ads by Google