Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation (Chapter 14)
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 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)
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 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
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 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
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 PUSH ST R2,Sv2 ;needed by PUSH ST R1,Sv1 ;needed by PUSH LD R1,MAX ;MAX has 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
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 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
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 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
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 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; }
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 function This (int a, int b) { int x, y, z; ……… y = That(a); …….. return z; } function That (int a) { int x; ……… body …….. return x; }
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 - 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