Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.

Slides:



Advertisements
Similar presentations
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.
Advertisements

The University of Adelaide, School of Computer Science
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
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
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
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
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.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
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.
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.
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
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Simple Procedure Calls jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Ellen Spertus MCS 111 October 16, 2001 Review. 2 What you will learn this semester Given devices that implement simple boolean functions… Understand how.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Rocky K. C. Chang Version 0.1, 25 September 2017
CSCI206 - Computer Organization & Programming
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Prof. Hsien-Hsin Sean Lee
Computer Architecture Instruction Set Architecture
Computer Architecture Instruction Set Architecture
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Program and memory layout
Systems Architecture I
Program and memory layout
Computer Architecture
Program and memory layout
MIPS function continued
Presentation transcript:

Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003

2 MIPS instructions Arithmetic –add –add immediate (addi) –load immediate (li) –sub –subtract immediate (subi) –multiply (mult) Data transfer –load byte (lb) –load word (lw) –store byte (sb) –store word (sw) Conditional branch –branch on equal (beq) –branch on not equal (bne) –set on less than (slt) Unconditional jump –jump (j) –jump register (jr)

3 Use of new instructions Program counter (PC): An immediate is a constant (ordinary number).

4 Calling conventions Convention observed by caller and callee Analogies –Spies –Classes –Car use

5 Conventions for borrowing car

6 Caller’s responsibilities Place arguments where procedure can access them ($a0..$a3) Transfer control addi $a0, $zero, 3 addi $a1, $zero, 6 j sum Example: sum(3, 6)

7 Callee’s responsibilities Do the work, using the arguments in $a0..$a3 Put return value where caller can access it ($v0) Return control to the calling procedure public int sum(int a, int b) { return a+b; } sum: 200: 204:

8 Caller’s responsibilities Place arguments where procedure can access them ($a0..$a3) Save current program counter before transferring control: jump and link (jal) –$ra  PC+4 –PC  Example: sum(3, 6) 0: addi $a0, $zero, 3 4: addi $a1, $zero, 6 8: jal sum 12:...

9 Sample call Caller 200: add $a0, $zero, 3 204: add $a1, $zero, 6 208: jal sum 212:... Callee sum: 500: add $v0, $a0, $a1 504: jr $ra Program counter (PC): Return address ($ra):

10 Practice public int ____(int a, int b) { if (a < b) return b; else return a; }

11 Factorial public int fact (int n) { if (n == 0) return 1; else return n * fact(n-1); } fact: # n is in $a0 bne $a0, $zero, else_clause addi $v0, $zero, 1 j return else_clause: return:

12 Factorial (2) fact: bne $a0, $zero, else_clause # compare n to 0 addi $v0, $zero, 1 # return value = 1 jr $ra # return to caller else_clause: #Save $ra and $a0 subi $a0, $a0, 1 # n = n - 1 jal fact # make recursive call #Restore old values of $ra and $a0 mult $v0, $v0, $a0 # multiply fact(n-1)*n jr $ra # return to caller

13 Where? Registers Memory –Fixed location –Stack

14 Stack Operations –push –pop Implementation –A free region of memory –Top of stack indicated by stack pointer ($sp)

15 Implementation Stack pointer ($sp) points to last element pushed; e.g., $sp = 216. Push –Decrement stack pointer –Store value to top of stack Pop –Load value from top of stack –Increment stack pointer

16 Optimization Straightforward code # Push $r1 # Push $r2 # Push $r3 Optimized code

17 Factorial (3) else_clause: subi $sp, $sp, __ # create space on stack for two items sw $ra, 4($sp) # store $ra on stack sw $a0, 0($sp) # store $a0 on stack subi $a0, $a0, 1 # n = n - 1 jal fact # make recursive call lw $ra, __($sp) # restore $ra from stack lw $a0, __($sp) # restore $a0 from stack addi $sp, $sp, __ # restore $sp to original value mult $v0, $v0, $a0 # multiply fact(n-1)*n jr $ra # return to caller

18 Register convention “caller saved” “callee saved”

19 Practice fact(5) + fact(6) put 5 in $a0 call fact save result of fact(5) put 6 in $a0 call fact add result of fact(6) to saved result of fact(5)

20 Procedure to-do list Allocate any needed storage Save callee-saved registers that might be modified To make a procedure call –Save caller-saved registers on stack –Put arguments in $a0..$a3 –Jump and link (jal) –Restore caller-saved registers from stack Restore callee-saved registers Put return value in $ra Jump to the return address (jr $ra)

21 Factorial (4) fact: # Store callee-saved values subi $sp, $sp, 8 # create space on stack for two items sw $ra, 4($sp) # store $ra (return address) on stack sw $a0, 0($sp) # store $a0 (n) on stack bne $a0, $zero, else_clause # if (n>0) goto else_clause # Base case addi $v0, $zero, 1 # prepare to return the value 1 addi $sp, $sp, 8 # return stack pointer to its original value jr $ra # return to caller else_clause: subi $a0, $a0, 1 # n = n - 1 jal fact # make recursive call lw $ra, 4($sp) # restore $ra from stack lw $a0, 0($sp) # restore $a0 from stack addi $sp, $sp, 8 # return stack pointer to its original value mult $v0, $v0, $a0 # multiply fact(n-1)*n jr $ra # return to caller