Procedure Basics Computer Organization I 1 October 2009 ©2006-09 McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,

Slides:



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

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.
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
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Computer Architecture CSCE 350
MIPS Function Continued
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
Procedure call frame: Hold values passed to a procedure as arguments
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
CS 536 Spring Code generation I Lecture 20.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
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.
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.
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
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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:
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.
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
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
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)
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Instructions - Type and Format
MIPS Procedures.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Procedures and Calling Conventions
Computer Architecture Procedure Calls
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)
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
MIPS Functions.
MIPS function continued
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages, we know the basic issues: -declaration: header, body, local variables -call and return -parameters of various types, with or without type checking, and a return value -nesting and recursion At the machine language level, there is generally little if any explicit support for procedures. This is especially true for RISC architectures. There are, however, many conventions at the assembly language level.

Procedure Basics Computer Organization I 2 October 2009 © McQuain, Feng & Ribbens Procedure Call and Return Calling a procedure requires transferring execution to a different part of the code… in other words, a branch or jump operation: MIPS reserves register $31, aka $ra, to store the return address. jal # $ra = PC + 4 # PC = The called procedure must place the return value (if any) somewhere from which the caller can retrieve it. The convention is that registers $v0 and $v1 can be used to hold the return value. We will discuss what to do if the return value exceeds 4 bytes later… Returning from the procedure requires transferring execution to the return address the jal instruction placed in $ra : jr $ra # PC = $ra

Procedure Basics Computer Organization I 3 October 2009 © McQuain, Feng & Ribbens Passing Parameters In most cases, passing parameters is straightforward, following the MIPS convention: The called procedure can then access the parameters by following the same convention. What if a parameter needs to be passed by reference? Simply place the address of the relevant data object in the appropriate register, and design the called procedure to treat that register value accordingly. What if a parameter is smaller than a word? Clever register manipulation in the callee. What if there are more than four parameters? We'll discuss that later… $a0 # 1st parameter $a1 # 2nd parameter $a2 # 3rd parameter $a3 # 4th parameter

Procedure Basics Computer Organization I 4 October 2009 © McQuain, Feng & Ribbens Procedure Example 1 Let's implement a MIPS procedure to get a single integer input value from the user and return it: get_integer: # Prompt the user to enter an integer value. Read and return # it. It takes no parameters. li $v0, 4 # system call code for printing a # string = 4 la $a0, prompt # address of string is argument 0 to # print_string syscall # call operating system to perform # print operation li $v0, 5 # get ready to read in integers syscall # system waits for input, puts the # value in $v0 jr $ra Since this doesn't use any registers that it needs to save, there's no involvement with the run-time stack.

Procedure Basics Computer Organization I 5 October 2009 © McQuain, Feng & Ribbens Procedure Call Example 1 Since the procedure does not take any parameters, the call is simple. The return value will, by convention, have been placed in $v0.....data# Data declaration section prompt:.asciiz "Enter an integer value\n".text main: # Start of code section jal get_integer # Call procedure move $s0, $v0 # Put returned value in "save" reg...

Procedure Basics Computer Organization I 6 October 2009 © McQuain, Feng & Ribbens Procedure Documentation Here's a sample header comment for this procedure: ################################################### get_integer # Prompts user for an integer, reads it, returns it. # # Arg registers used: none # Tmp registers used: none # # Pre: a global string, labeled "prompt" exists # Post: $v0 contains the value entered by the user # Returns: value entered by the user # # Called by: main # Calls: none Since this procedure is extremely simple, the header is rather dull.

Procedure Basics Computer Organization I 7 October 2009 © McQuain, Feng & Ribbens Procedure Example 2 Let's design a procedure to take some integer parameters and compute a value from them and return that value. Say the expression to be computed is: (a + b) – (c + d) Then the caller needs to pass four arguments to the procedure; the default argument registers are sufficient for this. The procedure only returns a single one-word value, so the default register is enough. The procedure will to use at least two registers to store temporary values while computing the expression, and a third register to hold the final result. The procedure will use $t0 and $t1 for the temporaries, and $s0 for the result. (This could be done with fewer registers, but it's more illustrative this way.)

Procedure Basics Computer Organization I 8 October 2009 © McQuain, Feng & Ribbens Procedure Example 2 This one's a little more interesting: proc_example: addi $sp, $sp, -4 # adjust stack pointer to make # room for 1 item sw $s0, 0($sp) # save the value that was in # $s0 when the call occurred add $t0, $a0, $a1 # $t0 = g + h add $t1, $a2, $a3 # $t1 = i + j sub $s0, $t0, $t1 # $s0 = (g + h) - (i + j) move $v0, $s0 # put return value into $v0 lw $s0, 0($sp) # restore value of $s0 addi $sp, $sp, 4 # restore the stack pointer jr $ra # jump back to the return # address

Procedure Basics Computer Organization I 9 October 2009 © McQuain, Feng & Ribbens Procedure Call Example 2 This time we must place the parameters (presumably obtained by calling the procedure get_integer ), into the default argument registers.... move $a0, $s0 # position the parameters move $a1, $s1 move $a2, $s2 move $a3, $s3 jal proc_example # make the call move $a0, $v0 # return value will be in $v0 li $v0, 1 # system call code for print_int syscall # print it...

Procedure Basics Computer Organization I 10 October 2009 © McQuain, Feng & Ribbens Procedure Documentation Here's another sample header comment: ################################################### get_integer # Computes and returns (a + b) – (c + d). # # Arg registers used: $a0 - $a3 # Tmp registers used: $t0, $t1 # # Pre: $a0 = a, $a1 = b, $a2 = c, $a3 = d # Post: $v0 contains (a + b) – (c + d) # Returns: value ($a0 + $a1) – ($a2 + $a3) # # Called by: main # Calls: none

Procedure Basics Computer Organization I 11 October 2009 © McQuain, Feng & Ribbens Procedure-related Conventions By convention, the caller will use: -registers $s0 - $s7 for values it expects to be preserved across any procedure calls it makes -registers $t0 - $t9 for values it does not expect to be preserved It is the responsibility of the called procedure to make sure that if it uses any of the registers $s0 - $s7 it backs them up on the system stack first, and restores them before returning. Obviously, the called procedure also takes responsibility to: -allocate any needed space on the stack for local data -place the return value onto the stack, if necessary -ensure the value of the stack pointer is the same after the call as it was before the call In some situations, it is useful for the caller to also maintain the value that $sp held when the call was made, called the frame pointer. The register $fp would be used for this purpose.