Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Advertisements

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.
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.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
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)
CS 536 Spring Code generation I Lecture 20.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.
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.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
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.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
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 Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Lecture 4: MIPS Instruction Set
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.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
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.
Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
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
Functions and the Stack
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Functions and Procedures
Assembly Programming using MIPS R3000 CPU
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Procedures.
Topic 3-a Calling Convention 1/10/2019.
Program and memory layout
Procedures and Calling Conventions
Systems Architecture I
Assembly Programming using MIPS R3000 CPU
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)
MIPS Functions.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Function Calling

Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure –Return from the procedure MAL (mips assembly language) has one statement for the first 2 steps –JAL : jump and link

JAL The jump and link –Places the return address (the contents of the PC) into register $31 The PC is incremented during the fetch part of the instruction cycle This is done during the execute phase, so the PC has the address of the instruction after the JAL instruction –Then jumps to the statement with the label used in the JAL statement

JAL usage JAL procedure_name –JAL sqrt –Saves the address of the statement after the JAL in $31 and jumps to the statement with the label sqrt. Register $31 can also be referred to as $ra (return address). Register $31 is implied (not explicit) in the JAL instruction.

Return Now register $31 has the return address. Need to jump to the instruction whose address is in $31. jr $31 –Jump register –Jumps to the address in the register specified.

Nested Calls This process of calling and returning using register $31 works fine for a single call and return. However if the procedure calls a procedure, the first return address is lost when the second JAL is done. Need to save the value of $31 at the beginning of the function so it does not get clobbered inside the function.

Returning Order We return to the most recently made JAL that has not been returned to. This is the process of a stack (LIFO). Use a stack to keep the “return addresses” The system has a stack that we can use for this process.

System Stack The system stack is in main memory starting at the “end” of memory. –The program starts at the “beginning” of memory This stack grows “backwards”. Recall, that when implementing a stack with an array, we need a “top” pointer. The “top” pointer is register $29 –Also called $sp

Using the System Stack The system stack pointer starts at the “end” of memory. It grows “up” not “down”. When you do a push, you need to subtract from the “top” ($sp) When you do a pop, you need to add to the “top” ($sp)

Pushing and Popping To push register $31 onto the stack, use sw $31,0($sp) add $sp, $sp, -4 To pop a value off the stack and put it into register $31, use add $sp, $sp, 4 lw $31, 0($sp)

Stacking Return Addresses The “best” convention to use is to ALWAYS start the procedure with the code to push the return address onto the stack Do this even if your procedure does not call another procedure. You may add a JAL into the procedure later. ALWAYS pop the return address from the stack before doing the return (JR).

Activation Records We have been using local variables in procedures. They are very useful. Our “variables” are registers. The calling procedure wants the values in registers to be the same after the called procedure returns as they were before the procedure was called.

Local Values We can push the values of the registers onto the stack and then pop them off just before we do the return. We need to consider if we want to push all registers or just the registers the procedure uses. –Need to be careful if we make changes to the procedure and start to use a register we have not saved

Communication We also need a technique to “send” values to the procedure (arguments – parameters) We also need a way to send values back to the calling procedure (return values and/or reference arguments) We can have any number of arguments Use registers $a0-$a3 for the first 4 args. Use the stack for passing additional arguments

Passing Additional Arguments Push the arguments onto the stack before calling the procedure. Here is an example of passing 3 arguments (by value) sw $5, 0($sp) sw $8, -4($sp) sw $15, -8($sp) add $sp, $sp, -12 jal myproc

Using Parameters Now to get those arguments, we can have lw $11,4($sp) lw $14, 8($sp) lw $18, 12($sp) Note that these offsets are off by 4 from the stores because we would have pushed the return address at the beginning of the procedure and subtracted 4 from $sp

Returning a value Most languages only allow one return value. Since this is what we have used in the past, we will adhere to this convention. This way, we can return the value in a register Therefore, a register must be “set aside” for return values. –It must not be “restored” before returning from a function

Types of Parameters We know about pass by value and pass by reference. –Pass by reference what you get in C++ when you use & in the parameter list. The examples we used were pass by value. We passed a value in the stack and neither the register or the memory location values were changed.

Pass by Reference Pass by reference passes the address of the variable. To accomplish this in MAL, we would pass the address of the variable by placing it in the stack.

Reference Passing Example la$5,arg1 sw$5,0($sp) add $sp, $sp, -4 jalmyproc ⋮ myproc: sw $31, 0($sp) add $sp, $sp, -4 lw$8, 4($sp) lw$6, 0($8) #now have the value of arg1 ⋮ sw$9, 0($8) #stored result from $9 into arg1 add$sp, $sp, 8#go past return and parameter lw$31, -4($sp) jr$31

Register Usage Conventions I suggest you use the following alternative names for registers and follow the conventional usage –$0 : always has 0 –$at : assembler temporary – never use –$v0-$v1 : expression and function results –$a0-$a3: first 4 args of a function –$t0-$t9: temporaries – not preserved in func. –$s0-$s8: Saved – preserved in functions –$k0-$k1: OS – do not use –$gp: global pointer – usage later –$sp: stack pointer –$ra: return address