© Craig Zilles (adapted from slides by Howard Huang)

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
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.
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.
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.
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
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
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.
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)
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
Run time vs. Compile time
1 Today  Finish-up procedures/stack  strlen example  Machine language, the binary representation for instructions. —We’ll see how it is designed for.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
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.
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,
February 3, 2003Functions in MIPS1 CS 232 It is important that students bring a certain ragamuffin, barefoot irreverence to their studies; they are not.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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:
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
Function Calls in MIPS To call a function: jal func
Rocky K. C. Chang Version 0.1, 25 September 2017
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.
Digital Logic Design Alex Bronstein
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
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Procedures and Calling Conventions
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
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 R3000 Subroutine Calls and Stack
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

©2003-2006 Craig Zilles (adapted from slides by Howard Huang) MP#1: Functions in MIPS We’ll talk about the 3 steps in handling function calls: The program’s flow of control must be changed. Arguments and return values are passed back and forth. Local variables can be allocated and destroyed. And how they are handled in MIPS: New instructions for calling functions. Conventions for sharing registers between functions. Use of a stack. July 16, 2019 ©2003-2006 Craig Zilles (adapted from slides by Howard Huang)

Control flow in C Invoking a function changes the control flow of a program twice. Calling the function Returning from the function In this example the main function calls fact twice, and fact returns twice—but to different locations in main. Each time fact is called, the CPU has to remember the appropriate return address. Notice that main itself is also a function! It is, in effect, called by the operating system when you run the program. int main() { ... t1 = fact(8); t2 = fact(3); t3 = t1 + t2; } int fact(int n) int i, f = 1; for (i = n; i > 1; i--) f = f * i; return f; July 16, 2019 Functions in MIPS

Control flow in MIPS MIPS uses the jump-and-link instruction jal to call functions. The jal saves the return address (the address of the next instruction) in the dedicated register $ra, before jumping to the function. jal is the only MIPS instruction that can access the value of the program counter, so it can store the return address PC+4 in $ra. jal Fact To transfer control back to the caller, the function just has to jump to the address that was stored in $ra. jr $ra Let’s now add the jal and jr instructions that are necessary for our factorial example. July 16, 2019 Functions in MIPS

Data flow in C Functions accept arguments and produce return values. The blue parts of the program show the actual and formal arguments of the fact function. The purple parts of the code deal with returning and using a result. int main() { ... t1 = fact( 8 ); t2 = fact( 3 ); t3 = t1 + t2; } int fact(int n) int i, f = 1; for (i = n; i > 1; i--) f = f * i; return f; July 16, 2019 Functions in MIPS

Data flow in MIPS MIPS uses the following conventions for function arguments and results. Up to four function arguments can be “passed” by placing them in argument registers $a0-$a3 before calling the function with jal. A function can “return” up to two values by placing them in registers $v0-$v1, before returning via jr. These conventions are not enforced by the hardware or assembler, but programmers agree to them so functions written by different people can interface with each other. Later we’ll talk about handling additional arguments or return values. July 16, 2019 Functions in MIPS

A note about types Assembly language is untyped—there is no distinction between integers, characters, pointers or other kinds of values. It is up to you to “type check” your programs. In particular, make sure your function arguments and return values are used consistently. For example, what happens if somebody passes the address of an integer (instead of the integer itself) to the fact function? July 16, 2019 Functions in MIPS

The big problem so far There is a big problem here! The main code uses $t1 to store the result of fact(8). But $t1 is also used within the fact function! The subsequent call to fact(3) will overwrite the value of fact(8) that was stored in $t1. July 16, 2019 Functions in MIPS

Nested functions A similar situation happens when you call a function that then calls another function. Let’s say A calls B, which calls C. The arguments for the call to C would be placed in $a0-$a3, thus overwriting the original arguments for B. Similarly, jal C overwrites the return address that was saved in $ra by the earlier jal B. A: ... # Put B’s args in $a0-$a3 jal B # $ra = A2 A2: ... B: ... # Put C’s args in $a0-$a3, # erasing B’s args! jal C # $ra = B2 B2: ... jr $ra # Where does # this go??? C: ... jr $ra July 16, 2019 Functions in MIPS

Spilling registers The CPU has a limited number of registers for use by all functions, and it’s possible that several functions will need the same registers. We can keep important registers from being overwritten by a function call, by saving them before the function executes, and restoring them after the function completes. But there are two important questions. Who is responsible for saving registers—the caller or the callee? Where exactly are the register contents saved? July 16, 2019 Functions in MIPS

Who saves the registers? Who is responsible for saving important registers across function calls? The caller knows which registers are important to it and should be saved. The callee knows exactly which registers it will use and potentially overwrite. However, in the typical “black box” programming approach, the caller and callee do not know anything about each other’s implementation. Different functions may be written by different people or companies. A function should be able to interface with any client, and different implementations of the same function should be substitutable. So how can two functions cooperate and share registers when they don’t know anything about each other? July 16, 2019 Functions in MIPS

The caller could save the registers… One possibility is for the caller to save any important registers that it needs before making a function call, and to restore them after. But the caller does not know what registers are actually written by the function, so it may save more registers than necessary. In the example on the right, frodo wants to preserve $a0, $a1, $s0 and $s1 from gollum, but gollum may not even use those registers. frodo: li $a0, 3 li $a1, 1 li $s0, 4 li $s1, 1 # Save registers # $a0, $a1, $s0, $s1 jal gollum # Restore registers add $v0, $a0, $a1 add $v1, $s0, $s1 jr $ra July 16, 2019 Functions in MIPS

…or the callee could save the registers… Another possibility is if the callee saves and restores any registers it might overwrite. For instance, a gollum function that uses registers $a0, $a2, $s0 and $s2 could save the original values first, and restore them before returning. But the callee does not know what registers are important to the caller, so again it may save more registers than necessary. gollum: # Save registers # $a0 $a2 $s0 $s2 li $a0, 2 li $a2, 7 li $s0, 1 li $s2, 8 ... # Restore registers jr $ra July 16, 2019 Functions in MIPS

…or they could work together MIPS uses conventions again to split the register spilling chores. The caller is responsible for saving and restoring any of the following caller-saved registers that it cares about. $t0-$t9 $a0-$a3 $v0-$v1 In other words, the callee may freely modify these registers, under the assumption that the caller already saved them if necessary. The callee is responsible for saving and restoring any of the following callee-saved registers that it uses. (Remember that $ra is “used” by jal.) $s0-$s7 $ra Thus the caller may assume these registers are not changed by the callee. $ra is tricky; it is saved by a callee who is also a caller. Be especially careful when writing nested functions, which act as both a caller and a callee! July 16, 2019 Functions in MIPS

Registers and the callee July 16, 2019 Functions in MIPS

Register spilling example This convention ensures that the caller and callee together save all of the important registers—frodo only needs to save registers $a0 and $a1, while gollum only has to save registers $s0 and $s2. frodo: li $a0, 3 li $a1, 1 li $s0, 4 li $s1, 1 # Save registers # $a0, $a1, $ra jal gollum # Restore registers add $v0, $a0, $a1 add $v1, $s0, $s1 jr $ra gollum: # Save registers # $s0 and $s2 li $a0, 2 li $a2, 7 li $s0, 1 li $s2, 8 ... # Restore registers jr $ra July 16, 2019 Functions in MIPS

How to fix factorial In the factorial example, main (the caller) should save two registers. $t1 must be saved before the second call to fact. $ra will be implicitly overwritten by the jal instructions. But fact (the callee) does not need to save anything. It only writes to registers $t0, $t1 and $v0, which should have been saved by the caller. July 16, 2019 Functions in MIPS

Where are the registers saved? Now we know who is responsible for saving which registers, but we still need to discuss where those registers are saved. It would be nice if each function call had its own private memory area. This would prevent other function calls from overwriting our saved registers—otherwise using memory is no better than using registers. We could use this private memory for other purposes too, like storing local variables. July 16, 2019 Functions in MIPS

Function calls and stacks Notice function calls and returns occur in a stack-like order: the most recently called function is the first one to return. 1. Someone calls A 2. A calls B 3. B calls C 4. C returns to B 5. B returns to A 6. A returns Here, for example, C must return to B before B can return to A. 1 6 A: ... jal B A2: ... jr $ra B: ... jal C B2: ... jr $ra C: ... 2 5 3 4 July 16, 2019 Functions in MIPS

Stacks and function calls It’s natural to use a stack for function call storage. A block of stack space, called a stack frame, can be allocated for each function call. When a function is called, it creates a new frame onto the stack, which will be used for local storage. Before the function returns, it must pop its stack frame, to restore the stack to its original state. The stack frame can be used for several purposes. Caller- and callee-save registers can be put in the stack. The stack frame can also hold local variables, or extra arguments and return values. July 16, 2019 Functions in MIPS

The MIPS stack In MIPS machines, part of main memory is reserved for a stack. The stack grows downward in terms of memory addresses. The address of the top element of the stack is stored (by convention) in the “stack pointer” register, $sp. MIPS does not provide “push” and “pop” instructions. Instead, they must be done explicitly by the programmer. 0x7FFFFFFF stack $sp 0x00000000 July 16, 2019 Functions in MIPS

Pushing elements To push elements onto the stack: Move the stack pointer $sp down to make room for the new data. Store the elements into the stack. For example, to push registers $t1 and $t2 onto the stack: subi $sp, $sp, 8 sw $t1, 4($sp) sw $t2, 0($sp) An equivalent sequence is: sw $t1, -4($sp) sw $t2, -8($sp) Before and after diagrams of the stack are shown on the right. word 1 word 2 Before word 1 word 2 $t1 $sp $t2 After July 16, 2019 Functions in MIPS

Accessing and popping elements You can access any element in the stack (not just the top one) if you know where it is relative to $sp. For example, to retrieve the value of $t1: lw $s0, 4($sp) You can pop, or “erase,” elements simply by adjusting the stack pointer upwards. To pop the value of $t2, yielding the stack shown at the bottom: addi $sp, $sp, 4 Note that the popped data is still present in memory, but data past the stack pointer is considered invalid. word 1 word 2 $t1 $sp $t2 word 1 word 2 $sp $t1 $t2 July 16, 2019 Functions in MIPS

Summary Today we focused on implementing function calls in MIPS. We call functions using jal, passing arguments in registers $a0-$a3. Functions place results in $v0-$v1 and return using jr $ra. Managing resources is an important part of function calls. To keep important data from being overwritten, registers are saved according to conventions for caller-save and callee-save registers. Each function call uses stack memory for saving registers, storing local variables and passing extra arguments and return values. Assembly programmers must follow many conventions. Nothing prevents a rogue program from overwriting registers or stack memory used by some other function. In section, we’ll look at writing recursive functions. Which we’ll need for MP#3 July 16, 2019 Functions in MIPS