ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, 2-15 01 Procedures Essential ingredient of high level.

Slides:



Advertisements
Similar presentations
Week 8 Stack and Subroutines. Stack  The stack is a section of data memory (or RAM) that is reserved for storage of temporary data  The data may represent.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
The University of Adelaide, School of Computer Science
CPU Review and Programming Models CT101 – Computing Systems.
Machine Instructions Operations
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
There are two types of addressing schemes:
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
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.
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
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
1 ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides8.ppt Modification date: Nov 3, 2014 Random Logic Approach The approach described so far.
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)
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.
Run time vs. Compile time
Specifying the Actions Internal Architecture of a Simple Processor
Implementation of a Stored Program Computer
ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides6.ppt Modification date: Oct 30, Processor Design Specifying the Actions Internal Architecture.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
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.
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.
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.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
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.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
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
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.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
Procedures (Functions)
Microcomputer Programming
Functions and Procedures
Chapter 7 Subroutines Dr. A.P. Preethy
Chapter 10 The Stack.
Arithmetic using a stack
The University of Adelaide, School of Computer Science
by Richard P. Paul, 2nd edition, 2000.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Program and memory layout
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Computer Organization and Assembly Language
© Craig Zilles (adapted from slides by Howard Huang)
Presentation transcript:

ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level language programs – facility to execute procedures, code sequences to perform computations that are needed repeatedly through a main program, rather than duplicate the code. (In Java procedures created with static void method)

2 In C, we might write: int max(int a, int b) { int c; if (a > b) c = a; else c = b; return(c); } main {. z = max(x,y);. p = max(r,s);. return; } Function max called in two places in the main program and returns the maximum of the two arguments (a function being a procedure that returns a value). Note main is also a procedure.

3 Implementing Procedures Key issues 1. A mechanism must be in place to be able to jump to the procedure from various locations in the calling program (or procedure), and to be able to return from the called procedure to the right place in the calling program (or procedure). 2. A mechanism must be place to handle the situation that a procedure may call another procedure (nested procedures) or even itself (recursive procedures) when necessary. 3. A mechanism must be in place to handle passing arguments to the procedure, and to return results (if a function). 4. The registers being used by the calling procedure must be saved, so that they can be reused by the called procedure.

4 1. Jumping to procedure and returning Jumping to a procedure (without considering the return) could be done by a simple jump instruction. However, the return cannot be a simple jump as each call returns to a different place, the location after the corresponding call. Necessary to store the address of this location (return address) at the time of the procedural “call”, which is then used by the return instruction.

5 Jump and Link Instruction Simplest solution is to use a register, say R31, to hold the return address and provide a special instruction that loads this register prior to jumping to the procedure, so-called jump and link instruction: JAL proc1;load return address in R31 ;and then goto proc1 Return then is simple an indirect jump: J[R31];goto location whose addess is ;in R31 Note: Return address is the value held in the PC counter at the time of JAL execution as PC is incremented to point to the next instruction after the current instruction has been fetched.

6 “return address” is the address of the instruction immediately following the procedural jump instruction, i.e. address of next instruction.

7 2. Nested and recursive calls Some procedures call other procedures or even call themselves. In those situations, a single register to hold the return address is insufficient, as get a series of return addresses to maintain. For that, a stack is used - – last-in-first-out queue (LIFO): Can be implemented in main memory or using registers within processor. Historically, main memory stacks have been used because they allow almost limitless nesting and recursion of procedures.

8 Stack Pointer A register called a stack pointer provided inside processor to hold the address of the “top” of the stack (the end of the stack where items are loaded or retrieved). Depending upon design, stack pointer either holds address of the next free location on the stack or the address of the last item placed on the stack. Question: Any advantage of each approach? We shall assume the stack pointer holds the address of the last item placed on the stack.

9 Stack grow downwards As items are placed on the stack, the stack grows, and as items are removed from the stack, the stack contracts. Although not shown in figures here, normally memory stacks are made to grow downwards, i.e., items are added to locations with decreasing addresses. Why?

10 CALL/RET Instructions CISC machine instructions for both procedural call and procedural return. CALL proc1 – unconditional jump to start of procedure, with added feature that the return address ( address of instruction after call) put on the stack. Stack pointer adjusted accordingly (decremented). RET – return to calling program after execution of procedure. An unconditional jump to location having address given by return address as found on top of stack. Stack pointer adjusted accordingly (incremented).

11 Suppose 32-bit addresses are stored on the stack. Then 4 bytes needed for each address, and stack pointer decremented by four each time an address is added to stack, and incremented by four as addresses are removed from stack. Part of CALL instruction is to decrement the stack pointer by 4. Part of RET instruction is to increment stack pointer by 4.

12 Procedure Call Before Call Main program Call Proc1 Next instruction Procedure Proc1 Ret Here Stack Stack pointer Top of Stack

13 Procedure Call After Call Main program Call Proc1 Next instruction Procedure Proc1 Ret Here Stack Stack pointer Top of Stack -4 Return address is the address of the instruction after the Call instruction. Return address

14 Procedure Call After Return Main program Call Proc1 Next instruction Procedure Proc1 Ret Here Stack Stack pointer Top of Stack +4 Return address is the address of the instruction after the Call instruction. Return address

15 Nested Procedure Calls Stack provides storage for each return address for nested/recursive calls.

16 Using JAL instruction with a stack RISCs (Reduced Instruction Set Computers) usually do not provide complex CALL and RET instructions. Instead, they rely on using JAL instruction and indirect jump instructions, assigning one general purpose register as the stack pointer, say R29. Suppose JAL stores return address in R31:

17 Calling a procedure Before the JAL instruction to call a procedure, instructions decrement R29 and copy R31 onto the memory stack: call:SUB R29,R29,4;Decrement stack pointer ST [R29],R31;Return address on stack JAL proc1;jump to proc1 and store ;return address in R31

18 Returning from a procedure Use J [R31] to return and then copy top the memory stack in R31 and increment R29 : JAL proc1 LD R31,[R29] ;restore return address in R31 ADD R29,R29,4 ;Increment stack pointer J[R31] End of procedure

19 3. Passing Arguments (actual parameters) and Returning Results For a procedure which does not call another procedure (leaf procedure) registers could be used for passing arguments and returning results. For non-leaf procedures, stack can be used to pass arguments to procedure by placing arguments on stack before call and extracting them off stack from within procedure. Similarly results can be placed on stack prior to returning and extracted off stack afterwards. Need instructions to place values onto stack and instruction to extract values from stack (both, adjusting stack pointer accordingly).

20 Stack Data Transfer Instructions CISCs (Complex Instruction Set Computers) usually provide a named stack pointer (SP) and special push and pop instructions: PUSH Instruction PUSH R2;Copy R2 register onto stack Copies the contents of a location onto the top of the stack, adjusting stack pointer according (decrementing SP by 4 for a 4-byte transfer). POP Instruction POP R2;Copy top of stack into R2 register Copies top of the stack into a location, adjusting the stack pointer accordingly (incrementing SP by 4 for a 4-byte transfer).

21 Passing Parameter thro Stack to Procedure Assume a procedure with one int parameter, x, i.e. proc1(x). x held in R1: Mechanics of getting parameter from stack a little complicated as it requires a study of contents of the stack to find where R1 is on stack. An addition pointer called frame pointer used.. PUSH R1 CALL proc1 POP R1. Procedure proc1.. ; access stack get R1. RET

22 4. Saving registers The stack can be used to save the contents of registers prior to being reused inside the procedure. Afterwards, registers can be restored from the contents of the stack. Saving/restoring registers can be done by called procedure or by calling program - Probably best done inside called procedure.

23 Saving Registers within Procedure. CALL proc1. Procedure proc1 PUSH R4 ;save registers to be PUSH R5 ;reused. POP R5 ;restore registers POP R4 RET R4 and R5 used in main program Notice reverse order Argument, says R1 Return address Stack R4 R5

24 Questions