Computer Science 210 Computer Organization

Slides:



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

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)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
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
CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More.
Procedure call frame: Hold values passed to a procedure as arguments
1 Today  Remember to use your late days wisely —We can’t post solutions until all HWs have been turned in  One complete example —To put together the.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Intro to Computer Architecture
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
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.
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.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
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.
Section 5: Procedures & Stacks
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
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 structure: Procedure Calls
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Functions and the Stack
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Pick up the handout on your way in!!
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
MIPS Procedures.
Topic 3-a Calling Convention 1/10/2019.
10/4: Lecture Topics Overflow and underflow Logical operations
Procedures and Calling Conventions
Systems Architecture I
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 R3000 Subroutine Calls and Stack
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Computer Science 210 Computer Organization Introduction to MIPS Assembly Language Subroutines

MIPS Memory Allocation Stack grows down Register $sp points to top of stack Push decreases $sp Pop increases $sp pc (program counter) is register that keeps up with current location in the program Note: The stack is in memory; so stack operations are slow.

Subroutines Subroutines in assembly language provide the low level mechanism for high level functions, procedures, subroutines, methods, etc.

Important Assumption Unless stated otherwise, we assume that our subroutines are completely independent of the calling program or subroutine The subroutine knows nothing about labeled data of the caller – only receives data via appropriate registers or system stack. Subroutine can have no labeled data (doesn’t know labels used by caller)

Issues with Subroutines We must learn How to call a subroutine How do we know the return location How to pass parameters to subroutine How to deal with local variables of subroutine How to protect register values of caller How to return values to caller How to protect caller’s stack values How to return to caller

1) Calling the Subroutine 2) Where to return To call a subroutine, we use the jal (jump and link) instruction. This is an unconditional branch instruction jal label The label is that of the subroutine This instruction Puts 4($pc) into register $ra (return address) so we’ll know where to return. Puts the address of the label in $pc.

3) Passing parameters to subroutine To pass up to 4 parameters by value, the caller uses registers $a0 - $a3 to hold the values. To pass up to 4 parameters by reference, use these same registers for the addresses. If we need to pass more parameters, say 6, we could use these registers for 4 parameters and put the other two on the stack. Of course, the specifics must be agreed upon between callers and the callee. Arrays are usually passed by reference – pass the base address and length of array.

4) Local variables The basic way to handle the need for local variables is to Use registers if possible Use the stack otherwise

5) Saved registers – One MIPS convention It is the callee’s responsibility to preserve the values in $s0 - $s7. So if the subroutine uses these registers, their original values should be placed on stack and then restored before returning. It is the caller’s responsibility to preserve the values in $t0 - $t7. So if there are needed values in these registers, the caller should put these on stack before calling the subroutine and then restore them after the return.

5) Saved registers – Other conventions Caller Saves Convention: It is the caller’s responsibility to preserve the values in $s0 - $s7 and $t0 - $t7 before calling and then restore after the call. Callee Saves Convention: It is the callee’s responsibility to preserve the values in $s0 - $s7 and $t0 - $t7 for any of the registers that it uses. So if there are needed values in these registers, the caller should put these on stack before calling the subroutine and then restore them after the return. Unless stated otherwise,we will use Callee Saves.

6) Returning values Registers $v0 and $v1 are used for returning values from a subroutine. If more values need to be returned, these could be on the stack. Of course, the caller would need to get them from the stack.

7) Preserving stack Often the caller will have valuable data on the stack. Therefore, the callee must be sure to return to the caller with the stack as before the call, possibly with return values on top of the stack. The special register $fp (frame pointer) is used to hold the value of the stack pointer when the subroutine first gets control move $fp, $sp Before returning, move $sp, $fp We refer to the section of stack memory used by the subroutine as a frame. If the frame is of fixed size, we may not need $fp.

8) Returning After doing the appropriate housekeeping, jr $ra since $ra holds the appropriate return address.

What if subroutine calls a subroutine Frames will get stacked on frames – that’s ok. One problem is that when a subroutine1 calls a subroutine2, $ra gets the return address back into subroutine1. The return address that subroutine1 will need when it returns will be lost. So, subroutine1 must keep it in a saved register or on the stack.

Subroutine Example

Subroutine Example

Subroutine Example