EECC250 - Shaaban #1 lec #8 Winter99 1-3-2000 Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables,

Slides:



Advertisements
Similar presentations
1 Procedural Programming Paradigm Stacks and Procedures.
Advertisements

The University of Adelaide, School of Computer Science
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
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.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
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.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C Instructor: Dr Aleksandar Milenkovic Lecture Notes.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Runtime Environments Compiler Construction Chapter 7.
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
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
9/20/6Lecture 2 - Prog Model Architecture, Data Types and Addressing Modes.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/
1 Stacks, Subroutines, I/O Routines Today: First Hour: Stacks, Subroutines –Section 3.9,3.10 of Huang’s Textbook –In-class Activity #1 Second Hour: I/O.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
CSC 8505 Compiler Construction Runtime Environments.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
© 2010 Kettering University, All rights reserved..
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.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Chapter 14 Functions.
Computer Architecture & Operations I
Figure 8.1 of course package
Subroutines … passing data
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)
Lecture 3 - Instruction Set - Al
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C
Passing Parameters Data passed to a subroutine is called a parameter.
68000 Architecture, Data Types and Addressing Modes
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
Subroutines … passing data
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
ECE511: Digital System & Microprocessor
Chapter 14 Functions.
Implementing Functions: Overview
Lecture 3 - Instruction Set - Al
Presentation transcript:

EECC250 - Shaaban #1 lec #8 Winter Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables, return addresses, and frame pointers are stored on the stack when a main program calls a procedure "Process" as well as when the procedure calls itself again in a recursion. We assume the following: The stack pointer initially has the value value $00000F00 just before Process is invoked (before any parameters are pushed onto the stack). Array "X", "Y", "Z" and "ALPHA" are passed by reference. Parameter "N" is passed by value (both ways - i.e. into the called procedure and also copied by value back into the calling routine). A6 is used as the frame pointer (assumed to have initial value $ ). Procedure "Process" uses registers D0 - D4 as well as registers A0 - A4. Array X starts at location $1800, Y starts at $17F8, Z is at $17FC, ALPHA is at $17FD, and N is at $17FE.

EECC250 - Shaaban #2 lec #8 Winter Recursive Subroutine Calls Example Problem specification (continued): {main routine} X: array [0..30] of words Y: longword Z, ALPHA, N: byte Process(var: X, var: Y, var: Z, var: ALPHA, N ) We are to show all the M68000 assembly language instructions necessary to pass these parameters as well as to copy the return value N into its regular storage location (off the stack) (at $17FE).

EECC250 - Shaaban #3 lec #8 Winter Recursive Subroutine Calls Example Problem specification (continued): Procedure Process ( A, B, C, D, E ) A: array [0..?] of words {passed by reference} B: longword {passed by reference} C, D: byte {passed by reference} E: byte {passed both ways by value} local variables - T: longword U: word V: byte { some place within the first invocation of "Process" it calls itself as follows:} Process( var: A, var: T, var: C, var: V, E) {Note that some input parameters are passed through to the next iteration.}

EECC250 - Shaaban #4 lec #8 Winter Recursive Subroutine Calls Example Solution The main program is assumed to allocate the original storage for: ORG $17F8 Y DS.L 1 This will resolve to address $000017F8 Z DS.B 1 This will resolve to address $000017FC ALPHA DS.B 1 This will resolve to address $000017FD N DS.B 1 This will resolve to address $000017FE * ORG $1800 X DS.W 31 an array of longwords 0..30

EECC250 - Shaaban #5 lec #8 Winter Recursive Subroutine Calls Example Solution (Continued ) ORG $1000 (assumed where main program starts - not critical) * * In main program the procedure (subroutine) is called in HLL: * * Process ( var:X, var:Y, var:Z, var:ALPHA, N) where N is the only one passed by value * The assembly language version/translation of this invocation is: * CLR.W D2 zeroes out an entire word for pushing on stack MOVE.B N,D2 copies value of byte N into lowest byte of D2 MOVE.W D2,-(A7) pushes that word containing value of N on stack PEA ALPHA pushes pointers to other arguments in reverse PEA Z order PEA Y PEA X JSR Process actually call the subroutine here MOVE.B 17(A7),N copy returned value back into N ADDA.L #18,A7 fix up stack from all parameters pushed for * subroutine call.

EECC250 - Shaaban #6 lec #8 Winter Recursive Subroutine Calls Example Solution (Continued) Recursive Subroutine Calls Example Solution (Continued) Stack Utilization Diagram 0E5E | not used | 0E94 | local 2 "T" | 0ECA | A0 | 0E60 | | | (longword) | | | | | 0E98 | local 2 "U" | 0ECE | A1 (high) | 0E64 | not used | 0E9A | - - | "V" 2| | A1 (low) | | | ** 0E9C | link reg val| 0ED2 | A2 | 0E68 | not used | | = $00000EE6 | | | | | 0EA0 | return addr | 0ED6 | A3 | 0E6C | D0 (high) 2| | into Process| | | | D0 (low) | 0EA4 | Addr of "X" | 0EDA | A4 | 0E70 | D1 2 | | ="A" in Proc| | | | | 0EA8 | Addr of "T"1| 0EDE | local 1 "T" | 0E74 | D2 2 | | = $00000EDE | | (longword) | | | 0EAC | Addr of "Z" | 0EE2 | local 1 "U" | 0E78 | D3 2 | | equiv "C" 1 | 0EE4 | - - | "V" 1| | | 0EB0 | Addr of "V"1| *0EE6 | orig linkreg| 0E7C | D4 2 | | = $00000EE5 | | = $ | | | 0EB4 | $00 | "E"2| 0EEA | return addr | 0E80 | A0 2 | 0EB6 | D0 (high) 1| | into main pr| | | | D0 (low) | 0EEE | Addr of "X" | 0E84 | A1 2 | 0EBA | D1 1 | | = $ | | | | | 0EF2 | Addr of "Y" | 0E88 | A2 2 | 0EBE | D2 1 | | = $000017F8 | | | | | 0EF6 | Addr of "Z" | 0E8C | A3 2 | 0EC2 | D3 1 | | = $000017FC | | | | | 0EFA | Addr "ALPHA"| 0E90 | A4 2 | 0EC6 | D4 1 | | = $000017FD | | | | | 0EFE | $00 |"N"val| * indicates the value of link register A6 during first call of Process ** indicates the value of link register A6 during the second call to Process

EECC250 - Shaaban #7 lec #8 Winter The coding of procedure Process would be something like this: Procedure Process ( var:A, var:B, var:C, var:D, E ) * where A: is an array of words [0.. ?] passed by reference * B: longword passed by reference * C, D: byte passed by reference * E: byte passed by value (in BOTH directions) * and local variables: * T: longword * U: word * V: byte Aptr equ 8 displacements for finding pass by reference Bptrequ12addresses from the frame pointer: A6 Cptrequ16 Dptr equ 20 E equ 25 this one is a byte which is passed by value Vequ-1 Uequ-4 Tequ -8 Recursive Subroutine Calls Example Solution (Continued) procedure Process

EECC250 - Shaaban #8 lec #8 Winter * The start of the code of Process looks like this: * Process LINK A6,#-8 MOVEM.L D0-D4/A0-A4,-(A7)save registers as required * * The invocation of Process from within Process: * * Process ( A, T, C, V, E) * CLR.W D0 MOVE.B E(A6),D0 note how we access "E" - we could have MOVE.W D0,-(A7) modified "E" before sending it PEA V(A6) this is basically how we can use "V" too MOVE.L Cptr(A6),-(A7) we push the pointer to "Z" on stack PEA T(A6),A0 push pointer to local variable "T" on stack MOVE.L Aptr(A6),-(A7) push pointer to "X" ("A" in Process) JSR Process MOVE.B 17(A7),E(A6) copy return value of "E" to local copy ADDA.L #18,A7 fix up stack from all parameters pushed * Recursive Subroutine Calls Example Solution (Continued) procedure Process

EECC250 - Shaaban #9 lec #8 Winter * This is how we'd access some of the variables in the subroutine: * MOVEA.L Aptr(A6),A0 This is how we'd copy the first array MOVE.L (A0),U(A6) element of X ("A" in procedure) into "U” * MOVEA.L Bptr(A6),A1 This is how we'd copy input parameter "B" MOVE.W (A1),T(A6) into local word "T” * MOVEA.L Cptr(A6),A2 This is how we actually reference "C" MOVE.B (A2),D1 * MOVEA.L Dptr(A6),A3 This is how we could access/change CLR.B (A3) "D” in procedure = "ALPHA" in main * * Before leaving the procedure we'd need to restore registers and destroy stack frame: * MOVEM.L (A7)+,D0-D4/A0-A4 UNLK A6 RTS Recursive Subroutine Calls Example Solution (Continued) procedure Process