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.

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
Lecture 9: MIPS Instruction Set
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
MIPS Calling Convention Chapter 2.7 Appendix A.6.
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
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)
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
 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 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
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
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.
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,
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.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: 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)
Functions and Procedures
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Addressing in Jumps jump j Label go to Label op address 2 address
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 5: Procedure Calls
MIPS Procedures.
MIPS Functions.
Program and memory layout
Lecture 6: Assembly Programs
Procedures and Calling Conventions
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
MIPS Functions.
Presentation transcript:

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 from. The instruction that invokes a procedure is a ‘caller’ The procedure itself is often referred to as ‘callee’ Must remember address called from Must have access to parameters, if any Must be able to return values

3 Shared Memory The simple way is to simply use global memory This is hard to design for in advance Don’t forget to define returns If procedure has many calls, one return may not work Dangerous if procedure overwrites something Usually end up allocating extra space to be sure Called routine may crush registers used by caller. Easy to program, though…

4 4 Function Calls All function calls are made using either jal or jalr: Since we save the return address (either Label or Rd above), we can easily return using the jr instruction While in a procedure, any memory can be accessed On return, execution will continue after the call. # Label = Name of function jal Label # Rd should be $ra, Rs holds procedure address jalr Rd, Rs

5 Example Function.data param1:.word 0x0h param2:.word 0x0h return:.word 0x0h.test … # some code before this, then set up for the call sw $t0, param1 # set the first parameter sw $s2, param2 # set the second parameter jal Sum # call the proceedure lw $s1, $return # get the result … # more code and eventually a program exit. After that: Sum: lw $t0, param1# get the first parameter lw $t1, param2# get the second parameter add $t1, $t1, St0# add them sw $t1, return# save the result jr $ra# return to caller

Better Procedure Structure Save registers before calling Any register the callee may use Any register passing values Pass values or pointers through registers Pass return values or pointers back through registers Callee can also save and restore registers May be necessary anyway if nested calls Use stack to simplify process

7 Better Sum Function # Sum # Takes two numbers passed in $t0 and $t1 and adds them # together. The result is passed back in $t2. No other # registers are affected. Sum: addiu $sp, -8# allocate 2 words on stack sw $t0, 0($sp)# put $t0 on stack sw $t1, 4($sp)# put $t1 on stack # don’t bother with $t2 add $t2, $t1, St0# add them lw $t1, 4($sp)# restore $t1 lw $t0, 0($sp) # restore $t0 addiu $sp, 8# deallocate 2 words on stack jr $ra# return to caller

8 Better Sum call … # Allocate space for registers I want to save. addiu $sp, -12# allocate 3 words on stack # Save my important registers (these are just an example) sw $t0, 0($sp)# put $t0 on stack sw $t1, 4($sp)# put $t1 on stack sw $t2, 8($sp)# I know Sum is changing $t2 jal Sum# call the routine sw $t2, result# store the return # Restore registers and stack pointer lw $t2, 8($sp)# saved so I can restore it lw $t1, 4($sp)# restore $t1 lw $t0, 0($sp) # restore $t0 addiu $sp, 12# deallocate 3 words on stack …

9 Better Still… … # Allocate space for registers I want to save. addiu $sp, -4# allocate 1 word on stack # Save my important registers. I read the function header # and know that only $t2 is in danger. sw $t2, 0($sp)# I know Sum is changing $t2 jal Sum# call the routine sw $t2, result# store the return # Restore registers and stack pointer lw $t2, 0($sp)# saved so I can restore it addiu $sp, 4# deallocate 1 word on stack …

10 About the register file $a_ registers are used for parameter passing by convention Callee needs to save and restore if making nested calls Should also normally preserve $t_ registers are the caller’s responsibility Save before calling Restore after calling $s_ registers are the callee’s responsibility to preserve Don’t depend on other programmer’s to obey the rules Other ISAs have similar but different conventions.