EECC250 - Shaaban #1 lec #7 Winter99 12-17-99 Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Procedure Calls Prof. Sirer CS 316 Cornell University.
Stack Frames: Using LINK. CEG 320/5208: Stack frames and LINK2 Assembling source code One source file: –Use ORG statements for data and code –Assemble.
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack.
EECC250 - Shaaban #1 lec #8 Winter Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables,
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C Instructor: Dr Aleksandar Milenkovic Lecture Notes.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
7/23 C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
8-1 EE 319K Introduction to Microcontrollers Lecture 8:Fixed Point Numbers, Local Variables, Binding, Allocation, Access, Deallocation.
CSC 8505 Compiler Construction Runtime Environments.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
Computer Science 210 Computer Organization
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Figure 8.1 of course package
CSCE 212 Computer Architecture
CSCI206 - Computer Organization & Programming
Subroutines … passing data
Procedures (Functions)
Procedures (Functions)
Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)
Functions and Procedures
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
CSCI206 - Computer Organization & Programming
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C
MIPS Instructions.
Passing Parameters Data passed to a subroutine is called a parameter.
The University of Adelaide, School of Computer Science
Reentrant Code a reentrant procedure can have several calls open to it at the same time in an interrupt environment, different ISRs may call the same routine.
; main program ; main move Param2, -(sp) move Param1, -(sp) Call Sub1
Figure 8.1 of course package
Chapter 6 - Procedures and Macros
The MOVE Multiple: MOVEM Instruction
Computer Science 210 Computer Organization
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Subroutines … passing data
Procedures and Calling Conventions
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
ECE511: Digital System & Microprocessor
Lecture 3 - Instruction Set - Al
Presentation transcript:

EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary private variables used in addition to available data registers. Recursion and recursive subroutines: Recursion involves defining the solution of a problem in terms of itself. A recursive subroutine is one that calls itself. Re-entrant subroutines: In multi-tasking systems, a subroutine is re-entrant if more than one task or process are allowed to use (call) the subroutine simultaneously without any ill effects.

EECC250 - Shaaban #2 lec #7 Winter The Stack and Local Subroutine Variables: Stack Frames In order for a subroutine to be recursive or re-entrant, the subroutine’s local workspace must be attached to each use or call of the subroutine. A stack frame (SF) of size d bytes is defined as a region of temporary storage in memory of size d bytes at the top of the current stack. Upon creating a stack frame: –The frame pointer (FP) points to the bottom of the stack frame. Register A6 is normally used as the frame pointer. –The stack pointer, SP is updated to point to the top of the frame. In assembly, the LINK and UNLK instructions are used to facilitate the creation/destruction of local subroutine storage using stack frames.

EECC250 - Shaaban #3 lec #7 Winter Example: Factorial Using Iteration FactorIMOVE.L D0,-(SP) Save the initial value of D0 MOVE.W D0,D1 Set the result to the input value Loop SUBQ.W #1,D0 WHILE N > 1 BEQ Exit N = N - 1 MULU D0,D1 Factorial_N = N * Factorial_N BRA Loop Exit MOVE.L (SP)+,D0 Restore the value of D0 RTS Factorial(N) If N = 1 THEN Factorial(N) := 1 ELSE Factorial(N) = N * Factorial(N-1) ENDIF The factorial of a positive integer is defined as: n! = n x (n-1) x (n-2) x (n-3) x.... x 1 Pseudo code: Assembly Subroutine using iteration: Computation:

EECC250 - Shaaban #4 lec #7 Winter Factorial Using Recursion Assembly program to compute factorial of a number using recursive subroutine calls. Subroutine parameter passing: by value via data registers. Main program: ORG $1000 MAIN MOVE.W NUMB,D0 get number JSR FACTOR go to factorial routine MOVE.W D0,F_NUMB store result STOP#$2700 ORG $2000 NUMB DC.W 5 number to be factorialized F_NUMB DS.W 1 factorial of number

EECC250 - Shaaban #5 lec #7 Winter Factorial Using Recursion: Subroutine * Initial conditions: D0.W = number to compute factorial of * where 0 < D0.W < 9 (range to avoid overflow) * Final conditions: D0.W = factorial of input number * Register usage: D0.W destructively used * Sample case: Input: D0.W = 5 * Output: D0.W = 120 FACTOR MOVE.W D0,-(SP) push input number onto stack SUBQ.W #1,D0 decrement number BNE F_CONT reached 1 yet? MOVE.W (SP)+,D0 yes: factorial = 1 RTS return F_CONT JSR FACTOR no: recursively call FACTOR MULU (SP)+,D0 multiply only after stack * contains all numbers RETURN RTS

EECC250 - Shaaban #6 lec #7 Winter Factorial Using Recursion Example: Effect On Stack

EECC250 - Shaaban #7 lec #7 Winter Creating A Stack Frame of Size d Bytes Word Stack just after a subroutine call Current SP A7 Passed Parameters Return Address Stack just after creating a stack frame of size d by the subroutine LEA -4(SP),A6 LEA -d(SP),SP d Current FP A6 Passed Parameters Return Address Current SP A7 Stack Frame

EECC250 - Shaaban #8 lec #7 Winter Destroying A Stack Frame Current FP A6 Passed Parameters Return Address Current SP A7 Stack Frame Word Stack after destroying the stack frame Current SP A7 Passed Parameters Return Address LEA d(SP),SP d

EECC250 - Shaaban #9 lec #7 Winter LINK An,-# d Allocates or creates a frame in the stack for local use by the subroutine of size d bytes. An is an address register serving as the frame pointer (FP); A6 is used. Function: –Push the contents of address register An onto the stack. (includes pre- decrementing SP by 4). –Save the stack pointer in An (An points to bottom of frame) –Decrement the stack pointer by d (points to the top of the frame) –Similar in functionality to the following instruction sequence: MOVEA.LA6,-(SP) LEA (SP),A6 LEA-d(SP),SP After creating the frame: –Passed parameters are accessed with a positive displacement with respect to FP, A6 i.e MOVE.W 8(A6),D0 –Local temporary storage variables are accessed with negative displacement with respect to A6 i.e. MOVE.L D2,-10(A6) LINK Instruction

EECC250 - Shaaban #10 lec #7 Winter LINK Instruction Operation Word Stack just after a subroutine call before LINK Current SP A7 Passed Parameters Return Address Current FP A6 Passed Parameters Return Address Current SP A7 d Stack Frame original A6 LINK A6,- # d

EECC250 - Shaaban #11 lec #7 Winter UNLK UNLinK Instruction UNLK An Deallocates or destroys a stack frame. Where An is the address register used as frame pointer (FP); usually A6 Function: –Restore the stack pointer to the value in address register An – i.e SP = An or SP = SP + d –Restore register An by popping its value from the stack. (includes post-incrementing SP by 4). Similar in functionality to the following instruction sequence: LEA d(SP),SP MOVEA.L (SP)+,An

EECC250 - Shaaban #12 lec #7 Winter UNLK Instruction Operation Current FP A6 Passed Parameters Return Address Current SP A7 d Stack Frame original A6 Word Stack just after UNLINK A6 Current SP A7 Passed Parameters Return Address UNLK A6 A6

EECC250 - Shaaban #13 lec #7 Winter Example: Using A Stack Frame FP MOVE.WD0,-(SP) Push parameter #1 onto the stack MOVE.WD1,-(SP) Push parameter #2 onto the stack JSR SBRT Jump to subroutine SBRT SBRTLINK A6,-#$8 Establish FP and local storage... MOVE.W 10(A6),D5 Retrieve parameter #1... MOVE.WD4,-4(A6) local write to stack frame... UNLK A6 Deallocate stack frame RTS A segment of a main calling program: A segment of a subroutine using a stack frame for local storage:

EECC250 - Shaaban #14 lec #7 Winter SF Example: Effect On Stack A6 LINK A6,-#$8

EECC250 - Shaaban #15 lec #7 Winter The Effect of Multiple Subroutine Calls on Frame Pointers When Using Local Storage