Computer Science 210 Computer Organization Recursive Subroutines System Stack Management.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

The University of Adelaide, School of Computer Science
Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
1 System Calls (TRAPS) and Subroutines Patt and Patel Ch. 9.
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 System Calls.
S. Barua – CPSC 240 CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
L12-Computing Factorial
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
Computer Science 210 Computer Organization Introduction to Subroutines.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Chapter 9 TRAP Routines and Subroutines. 9-2 System Calls Certain operations require specialized knowledge and protection: specific knowledge of I/O device.
Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Science 210 Computer Organization
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir. A.C. Verschueren Eindhoven University of Technology Section of Digital.
Introduction to Computer Engineering
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Function Calls in MIPS To call a function: jal func
COSC121: Computer Systems: LC3 Traps and Subroutines
Computer Science 210 Computer Organization
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Chapter 9 TRAP Routines and Subroutines
Pick up the handout on your way in!!
Application Binary Interface (ABI)
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Computer Science 210 Computer Organization
MIPS Instructions.
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
The MOVE Multiple: MOVEM Instruction
Introduction to Computer Engineering
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Introduction to Computer Engineering
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Introduction to Computer Engineering
Introduction to Computer Engineering
Introduction to Computer Engineering
MIPS R3000 Subroutine Calls and Stack
Runtime Stack Activation record for hanoi;
Introduction to Computer Engineering
Chapter 9 TRAP Routines and Subroutines
Presentation transcript:

Computer Science 210 Computer Organization Recursive Subroutines System Stack Management

Saving and Restoring Registers Generally use “callee-save” strategy, except for return values. –Save anything that the subroutine will alter internally that shouldn’t be visible when the subroutine returns. –It’s good practice to restore incoming arguments to their original values (unless overwritten by return value). Remember: You MUST save R7 if you call any other subroutine or TRAP.

;; Author: Ken Lambert ;; Calls the subroutine ORDER to guarantee that FIRST <= SECOND.ORIG x3000 ;; Main program register usage: ; R1 = initial value of FIRST ; R2 = initial value of SECOND ; Main program code LD R1, FIRST LD R2, SECOND JSR ORDER ST R1, FIRST ST R2, SECOND HALT ; Main program data FIRST.BLKW 1 SECOND.BLKW 1 ;; Subroutine ORDER ; Guarantees that R1 <= R2 ; Input parameters: R1 and R2 ; Output parameters: R1 and R2 ; R3 = temporary working storage ORDERST R3, ORDERR3; Save R3 JSR SUB BRnz ENDORD; Exit if difference <= 0 ADD R3, R2, #0; Swap values in R1 and R2 ADDR2, R1, #0 ADD R1, R3, #0 ENDORDLD R3, ORDERR3; Restore R3 RET ; Data variable for subroutine ORDER ORDERR3.BLKW 1 ;; Subroutine SUB... Example: ORDER makes R1 <= R2 Is there a bug here?

;; Author: Ken Lambert ;; Calls the subroutine ORDER to guarantee that FIRST <= SECOND.ORIG x3000 ;; Main program register usage: ; R1 = initial value of FIRST ; R2 = initial value of SECOND ; Main program code LD R1, FIRST LD R2, SECOND JSR ORDER ST R1, FIRST ST R2, SECOND HALT ; Main program data FIRST.BLKW 1 SECOND.BLKW 1 ;; Subroutine ORDER ; Guarantees that R1 <= R2 ; Input parameters: R1 and R2 ; Output parameters: R1 and R2 ; R3 = temporary working storage ORDERST R3, ORDERR3; Save R3 STR7, ORDERR7 ; Save R7 JSR SUB BRnz ENDORD; Exit if difference <= 0 ADD R3, R2, #0; Swap values in R1 and R2 ADDR2, R1, #0 ADD R1, R3, #0 ENDORDLD R3, ORDERR3; Restore R3 LDR7, ORDERR7 ; Restore R7 RET ; Data variables for subroutine ORDER ORDERR3.BLKW 1 ORDERR7.BLKW 1 ;; Subroutine SUB... Example: ORDER makes R1 <= R2 Back up ret address before another call

def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) Iteration vs Recursion def factorial(n): product = 1 while n > 0: product *= n n -= 1 return product Including 0 in the domain makes the assembler version easier

; Main program code LD R1, NUMBER JSR FACTORIAL ST R2, NUMBER HALT ; Main program data variables NUMBER.BLKW 1 ;; Subroutine FACTORIAL ; Returns the factorial of R1 in R2 ; Input parameter: R1 (the number) ; Output parameter: R2 (the product) ; R3 = temporary working storage ;; Pseudocode: ; product = 1 ; while number > 0 ; product *= number ; number -= 1 FACTORIALST R7, FACTTEMPR7; Save my return address ST R3, FACTTEMPR3; Save the working storage ST R1, FACTTEMPR1; Save my input parameter LD R2, FACTONE; Initialize the product to 1 WHILEJSR MUL; R3 = R1 * R2 ADDR2, R3, #0; Shift the product back to R2 ADD R1, R1, #-1; Decrement the number BRp WHILE LD R1, FACTTEMPR1; Restore my input parameter LD R3, FACTTEMPR3; Restore the working storage LD R7, FACTTEMPR7; Restore my return address RET ; Data for subroutine FACT FACTONE.FILL 1 FACTTEMPR1.BLKW 1 FACTTEMPR3.BLKW 1 FACTTEMPR7.BLKW 1 Iterative factorial routine in assembler

; Main program code LD R1, NUMBER JSR FACTORIAL ST R2, NUMBER HALT ; Main program data variables NUMBER.BLKW 1 ;; Subroutine FACTORIAL ; Returns the factorial of R1 in R2 ; Input parameter: R1 ; Output parameter: R2 ; R3 = temporary working storage ;; Pseudocode: ; if number > 0 ; return number * fact(number – 1) ; else ; return 1 FACTORIALADD R1, R1, #0; If number > 0 then recurse BRp RECURSE LD R2, FACTONE; Base case: return 1 RET RECURSEST R7, FACTTEMPR7; Save my return address ST R3, FACTTEMPR3; Save the working storage ST R1, FACTTEMPR1; Save my input parameter ADD R1, R1, #-1; Decrement the number JSR FACTORIAL LD R1, FACTTEMPR1; Restore my input parameter JSR MUL; R3 = R1 * R2 [number * fact(number – 1)] ADD R2, R3, #0; Shift the product back to R2 LD R3, FACTTEMPR3; Restore the working storage LD R7, FACTTEMPR7; Restore my return address RET ; Data for subroutine FACT FACTONE.FILL 1 FACTTEMPR1.BLKW 1 FACTTEMPR3.BLKW 1 FACTTEMPR7.BLKW 1 Recursive factorial routine in assembler

Problem Each call of the subroutine overwrites R7 with its return address, but all of the calls save R7 in the same data variable Each call must return to a context in which its own parameter R1 was saved, but they’re all saved in the same data variable Restoration of registers for more than one recursive call is impossible

Solution We need a means of stacking up saved values so the contexts of calls can be saved and restored in a last in, first out manner (many instances of R1, R7, etc.) A system stack provides for this

The Stack Interface The stack structure consists of a stack pointer (in R6, which now can’t be used for anything else) and two subroutines, PUSH and POP At program startup, the stack pointer is initialized to an address just above the keyboard status register R0 provides the interface for data pushed or popped

The Stack Implementation The stack pointer moves up (to a smaller memory address) during a PUSH The stack pointer moves down (to a larger memory address) during a POP Won’t bother with stack overflow or underflow for now

The Place of the Runtime Stack in Memory System memory Main program (code and data) Subroutines (code and data) Runtime System Device registers STACK BOTTOM: xFDFF Each push decrements the stack pointer by one Stack grows upwards toward user memory Main program.ORIG: x3000 System.ORIG: x0000

; Main program code LD R6, STACKBOTTOM; Initialize the stack pointer LD R1, NUMBER JSR FACT ST R2, NUMBER HALT ; Main program data variables STACKBOTTOM.FILL xFDFF; Address of the bottom of the stack NUMBER.BLKW 1 ;; Subroutine PUSH ; Copies R0 to the top of the stack and decrements the stack pointer ; Input parameters: R0 (the datum) and R6 (the stack pointer) ; Output parameter: R6 (the stack pointer) PUSHADD R6, R6, #-1 STR R0, R6, #0 RET ;; Subroutine POP ; Copies the top of the stack to R0 and increments the stack pointer ; Input parameter: R6 (the stack pointer) ; Output parameters: R0 (the datum) R6 (the stack pointer) POPLDR R0, R6, #0 ADD R6, R6, #1 RET Defining the Stack Resource

;; Subroutine FACTORIAL ; Returns the factorial of R1 in R2 ; Input parameter: R1 ; Output parameter: R2 ; R3 = temporary working storage ;; Pseudocode: ; if number > 0 ; return number * fact(number – 1) ; else ; return 1 FACTORIALADD R1, R1, #0; If number > 0 then recurse BRp RECURSE LD R2, FACTONE; Base case: return 1 RET RECURSEADD R0, R7, #0; Save my return address JSR PUSH ADD R0, R3, #0; Save the working storage JSR PUSH ADD R0, R1, #0; Save my input parameter JSR PUSH ADD R1, R1, #-1; Decrement the number JSR FACTORIAL JSR POP; Restore my input parameter ADD R1, R0, #0 JSR MUL; R3 = R1 * R2 [number * fact(number – 1)] ADD R2, R3, #0; Shift the product back to R2 JSR POP; Restore the working storage ADDR3, R0, #0 JSR POP; Restore my return address ADD R7, R0, #0 RET ; Data for subroutine FACT FACTONE.FILL 1 Recursive factorial routine in assembler

Using the Stack The stack can eliminate the need for declaring subroutine data variables in many cases For example, any routine that calls another one must save and restore R7 Stack ‘em up!

Costs and Benefits Using a stack incurs the cost of extra subroutine calls for PUSH and POP A large chunk of memory might go to waste Subroutines in early languages like FORTRAN, with no recursion, could be completely static (no extra overhead for subroutine calls)