Procedures 2 Intructions Supporting Procedures Making Use of a Stack.

Slides:



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

Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.
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)
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
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
 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.
MIPS Assembly Language
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Intro to Computer Architecture
CSCE 212 Quiz 2 – 2/2/11 1.What is the purpose of the jal instruction? 2.What are the two conditional branching (if, goto; not the slt instruction) instructions.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Procedures 2a MIPS code examples Making Use of a Stack.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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.
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.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
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:
Subroutines, parameters and the stack Bryan Duggan.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
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.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
MIPS Procedures.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
MIPS functions.
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
MIPS Functions.
Program and memory layout
Procedures and Calling Conventions
Program and memory layout
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)
MIPS Functions.
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

Procedures 2 Intructions Supporting Procedures Making Use of a Stack

Outline Requirements for procedures Basic MIPS concepts Procedures calling procedures Using the stack

Last time... Motivation for Procedures Example of procedures Introduced the idea of a stack –push an item onto a stack –pop the “top” of the stack This “data structure” supports the implementation of the procedure idea.

Requirement for Procedures Consider this abstract code: instruction 1#some instruction instruction 2 call procA#go to procedure instruction 3#return here!....#other stuff procA:instructions for procA After procA has completed, control returns to the instruction after the call.

MIPS Implementation... jal procA – j ump a nd l ink jumps to the address specified by procA stores the address of the following instruction in register $31. –this is called the return address. jr $31 –used at the end of the procedure »to return back to the appropriate address

Example add2 - adds the numbers in registers $4 and $5 and puts the result in $6 addi $4,$0,10 #$4 = 10 li $5,20#$5 = 20 jal add2#call add2 add $4,$0,$6 li $5,25 jal add2 sw $6, X($20)#stores result add2 : add $6,$4,$5#procedure body jr $31#return

Procedures Calling Procedures Suppose we want a procedure ‘sprod’ that returns x1*y1 + x2*y2. Abstractly we can think of this as: sprod(x1,y1,x2,y2){ a = prod(x1,y1);#x1*y1 b = prod(x2,y2);#x2*y2 return add2(a,b);#a + b } Procedure ‘sprod’ calls ‘prod’ and ‘add2’.

Problem When Proc call Procs jal procAddress –causes $31 to contain the next address So each time that a procedure is called (eg, when ‘prod’ is called inside ‘sprod’) –$31 gets overwritten (‘clobbered’) »so that the return address for the outer procedure is no longer available! Stacks can get us out of this problem.

How stacks are used A stack is used to store the sequence of return addresses when there are nested calls The calling (outer) procedure –pushes address in $31 onto stack –calls the procedure ($31 contains return address) –pops stack to restore $31 (after procedure returns) The called procedure... before returning –if it has called another procedure, it overwrites $31 with a pop from the stack –jr $31

Main calls ProcA calls ProcB...main program......some instructions... jal procA [1]#[1] goes into $31 procA:...some instructions... push $31 jal procB [2]#[2] goes into $31...some instructions $31 gets pop() jr $31#jumps to [1]

ProcB calls ProcC procB:...some instructions... push $31 jal procC [3]#[3] goes into $31...some instructions $31 gets pop() jr $31#jumps to [2] procC:...some instructions... jr $31#jumps to [3]

Stack Pointer Register $29 is called the ‘stack pointer’ It contains an address in memory of the top of a stack In MIPS stacks grow ‘downwards’ When pushing or popping items onto or from the stack, we must adjust $29 Remember byte addressing!

Summary jal –jump and link »saves return address in $31 jr $31 –jumps to return address in register $31 Stack pointer used to store nested return addresses ($29)

Next Time Example MIPS code for nested procedures