Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Advertisements

The University of Adelaide, School of Computer Science
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
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:
CS 536 Spring Code generation I Lecture 20.
Intro to Computer Architecture
Run time vs. Compile time
Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Chapter 8 :: Subroutines and Control Abstraction
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
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.
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.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
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.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
MIPS Instructions.
The University of Adelaide, School of Computer Science
Topic 3-a Calling Convention 1/10/2019.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Procedures in more detail

CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for procedures

CMPE12cCyrus Bazeghi 3 Procedures Procedure call {.. x = larger (a, b);.. } Procedure header and parameters int larger (int x, int y) Procedure body { if (x > y) bigger = x; else bigger = y; return (bigger); }

CMPE12cCyrus Bazeghi 4 Procedures Steps in the execution of the procedure: 1.Save return address 2.Procedure call 3.Execute procedure 4.Return What is the return address? What is the procedure call? What is the return? MAL uses registers rather than variables for return address

CMPE12cCyrus Bazeghi 5 Procedures Modern load/store machines (MIPS, SPARC,…) have a jump and link instruction for procedure calls jal procname Places the address of the instruction following it into the register $ra ($31) $31 is an arbitrary (and common) choice made at architecture time. Why isn’t $31 specified in the instruction as: jal $31, procname #NOT VALID CODE Branches (jumps) to the address given by the label (procname).

CMPE12cCyrus Bazeghi 6 Procedures The example becomes: jal proc1 # use of $ra is implied.. jal proc1.. proc1:# 1 st instruction of procedure here.. jr $ra# $ra is the alias for $31

CMPE12cCyrus Bazeghi 7 Nested Procedures What happens if a procedure calls another procedure (nesting) using jal? jal proc1... proc1:. jal proc2. jr $ra proc2:.. jr $ra

CMPE12cCyrus Bazeghi 8 Nested Procedures Even more exciting, what happens if a procedure calls itself (recursion)? jal proc1.... proc1:.. jal proc1.. jr $ra

CMPE12cCyrus Bazeghi 9 Nested Procedures Must save return address as generated Unknown how many to save Needed in reverse order of saved Use a …

CMPE12cCyrus Bazeghi 10 System Stack A stack is so frequently used for procedure call/return, that many computer systems predefine a system stack.

CMPE12cCyrus Bazeghi 11 The system stack is for dynamic data (vs static, known before runtime) –Return addresses –Saving registers to move other data into register bank (“spilling”) –Local variables – several instances may be defined at once with multiple calls –Dynamic memory allocation The system stack is very large In a multi-user environment, how many system stacks are there? System Stack

CMPE12cCyrus Bazeghi 12 System Stack The MIPS system stack (as with most machines) is defined to grow toward smaller addresses The stack pointer points to an empty location at the top of the stack The stack pointer is register $29, also called $sp It is defined before program execution begins If $sp ($29) is used for any other purpose, then the stack pointer is lost. This would be bad, very bad.

CMPE12cCyrus Bazeghi 13 Smaller address your program here Larger address system stack here Grows towards smaller addresses System Stack Memory

CMPE12cCyrus Bazeghi 14 System Stack push, in MAL: sw$?, ($sp) # the $? is replaced by some register sub$sp, $sp, 4 # contains the data to be pushed Or sub$sp, $sp, 4 sw$?, 4($sp) pop, in MAL: add$sp, $sp, 4 lw$?, ($sp) Or lw$?, 4($sp) add$sp, $sp, 4 Which forms are better if there is an interrupt that uses the same stack?

CMPE12cCyrus Bazeghi 15 System Stack Often a procedure pushes/pops many things. add$sp, $sp, -4 sw$s2, 4($sp) add$sp, $sp, -4 sw$s3, 4($sp) add$sp, $sp, -4 sw$s4, 4($sp) But we do not need to change $sp each time. add$sp, $sp, -12 sw$s2, 12($sp) sw$s3, 8($sp) sw$s4, 4($sp) Can do this for the pop as well

CMPE12cCyrus Bazeghi 16 System Stack: Saving Return Addresses jaldoit. jaldoit. doit:sub $sp, $sp, 4#push return address sw $ra, 4($sp). jal another#overwrites $ra. lw$ra, 4($sp)#pop return address add$sp, $sp, 4 jr$ra

CMPE12cCyrus Bazeghi 17 Note how every pop has a push. Never leave a procedure without popping everything that was pushed. Always match up your pushes and pops. System Stack: Saving Return Addresses

CMPE12cCyrus Bazeghi 18 Parameter Passing Parameter = Argument There is even less architectural support for parameter passing Need to create a convention

CMPE12cCyrus Bazeghi 19 Follow the convention –Follow the convention carefully –Follow the convention consistently –Never change the convention once defined Place data in a specific parameter location Both the calling program and the procedure need to know where the parameters are. Procedure may place return values there. Parameter Passing

CMPE12cCyrus Bazeghi 20 Parameter Passing Call by value (C, C++) The parameter passed may not be modified by the procedure. This can be implemented by passing a copy of the value. The procedure can modify the value (copy) passed to it, but the value is not changed outside the scope of the procedure. Call by reference (Fortran, C/C++ &, Pascal var) The parameter passed to the subroutine can be modified. The modification is seen outside the scope of the subroutine. Similar to having access to global variable.

CMPE12cCyrus Bazeghi 21 Parameter Passing Simplest method: Use registers The calling program puts the parameter(s) into specific registers, and the procedure uses them... move$s4, $s2# put parameter in register $s4 jaldecrement move$s2, $s4# copy parameter back to its # correct place.. decrement: add$s4, $s4, -1 jr$ra Why not just use $s2 within the procedure?

CMPE12cCyrus Bazeghi 22 Another method: Use system Stack When there aren’t enough registers for all parameters, use the system stack in something called an activation record (AR). Used for all parameters in machines with few registers. eg. HC11, 6502, 8086, … Parameter Passing

CMPE12cCyrus Bazeghi 23 sub$sp, $sp, 8# allocate space for parameters sw$s2, 8($sp)# place parameter 1 into AR of proc sw$s3, 4($sp)# place parameter 2 into AR of proc jalproc. proc: sub$sp, $sp, 12# allocate remainder of AR for proc # assume fixed size (too big) # activation record lw$t0, 20($sp)# retrieve parameter 1 lw$t1, 16($sp)# retrieve parameter 2 # use parameters in procedure calculations add$sp, $sp, 20# remove AR of proc jr$ra Parameters on system stack

CMPE12cCyrus Bazeghi 24 Parameters on system stack Calling program Allocate space for parameters Places parameters into stack Calls procedure Procedure: Allocates AR (or remainder of AR) De-allocates AR of procedure (or at least most of it) MIPS convention The first 4 parameters are passed in register The alias for $4-$7 is $a0-$a3. The first parameter is passed in $a0. Space for all parameters passed in $a0-$a3 is allocated in the procedure’s AR.

CMPE12cCyrus Bazeghi 25 MIPS Convention If there are nested subroutine calls, and registers $a0- $a3 are used for parameters, the values would be lost procA:#receives 3 parameters in $a0, $a1, $a2 … # set up procB’s parameters move $a0, $t0# overwrites procA’s parameter in $a0 move $a1, $t1# overwrites procA’s parameter in $a1 jal procB# the nested procedure call # procA continues after procB returns # procA’s passed parameters are needed, but have been # overwritten Solutions…

CMPE12cCyrus Bazeghi 26 MIPS Convention Need to expand on the MIPS convention to now preserve the argument registers also Caller Place parameters in $a0 to $a3 jal procedure Procedure Allocate remainder of AR and push $ra Procedure calculations

CMPE12cCyrus Bazeghi 27 Place current parameters ($a0-$a3) into AR Set up parameters to proc2 in $a0-$a3 Call proc2 (jal proc2) Copy any return values out of $v0-v1, $a0-$a3 Restore current parameters from AR back to $a0-$a3 MIPS Convention Procedure continued… To call proc2 More procedure calculations Get procedure’s return address from AR De-allocate AR Return (jr $ra)

CMPE12cCyrus Bazeghi 28 Summary: Parameter Passing Styles 1.Use registers Advantages Disadvantages 2.Use some registers, and place the rest on the stack Advantages Disadvantages 3.Put all parameters on the stack (an unsophisticated compiler might do this) Advantages Disadvantages 4.Put parameters in memory set aside for them Advantages Disadvantages

CMPE12cCyrus Bazeghi 29 Register Spilling When in a procedure may need to use registers that are already being used, what to do? Callee saved A procedure clears out some registers for its own use Register values are preserved across procedure calls MIPS calls these saved registers: $s0-$s8 (aliases for $16-$23, $30) The called procedure Saves register values in its AR, Uses the register for local variables, Restores register values before it returns.

CMPE12cCyrus Bazeghi 30 Register Spilling Caller Saved The calling program saves the registers that it does not want a called procedure to overwrite These register values are NOT preserved across procedure calls MIPS calls these temporary registers: $t0-$t9 (aliases for $8-$15, $24-$25) Procedures use these registers for local variables The values do not need to be preserved outside the scope of the procedure. How about $v0-$v1, and $a0-$a3?

CMPE12cCyrus Bazeghi 31 Procedure Calls From the compiler’s point of view, a procedure call looks like: call setup procedure call return cleanup. procedure: prologue. calculations. epilogue

CMPE12cCyrus Bazeghi 32 MIPS Procedure Calls The full MIPS convention includes… Call Setup Save any callee-save registers that are currently in use Place current parameters into current stack frame Allocate space for all parameters for procedure to be called Change $sp by the total parameter size Place first 4 parameters to procedure into $a0-$a3 Place remaining parameters on stack Procedure Call jal

CMPE12cCyrus Bazeghi 33 Prologue Allocate space for remainder of stack frame Save return address in stack frame Copy needed parameters from stack frame into registers Save any needed save registers into current stack frame Epilogue Restore (copy) return address from stack frame into $ra Restore any saved registers De-allocate stack frame (or most of it) Move $sp so the space for the procedure’s frame is gone Return Cleanup Copy needed return values and parameters from $v0-$v1, $a0-$a3, or stack frame to correct places De-allocate remainder of procedure’s stack frame Move $sp so the space for the procedure’s frame is gone Restore any saved registers from call setup MIPS Procedure Calls

CMPE12cCyrus Bazeghi 34 Summary: Procedure Calls Minimal ASM support Need formal and consistent mechanism Why? Activation record includes Return addresses parameters Save registers Saved arguments … Caller must… Callee must…