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.

Slides:



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

The University of Adelaide, School of Computer Science
Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
The University of Adelaide, School of Computer Science
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
Ch. 8 Functions.
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
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Assembly Language
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.
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
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
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.
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.
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.
Simple Procedure Calls jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return.
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.
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:
Subroutines, parameters and the stack Bryan Duggan.
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.
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.
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 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
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
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.
MIPS Functions.
MIPS functions.
MIPS Procedures.
MIPS function continued
Program and memory layout
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
MIPS Functions.
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

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 address in $31 Note: If subroutine calls are nested $31 must be saved on the stack and restored by the user.

Example Subroutine Call & Return #calling program #subroutine A. subroutineA:. #call subroutineA. jal subroutineA.. #return.jr $31.

MIPS Stack Operations All stack (LIFO) operations must be hand coded by using other MIPS instructions $29 points to the top of the Stack memory ($29 is also called the Stack Pointer, SP) The Stack starts at a high memory address and grows to a lower memory address The Stack grows by adding –4 and shrinks by adding +4 to Stack Pointer Putting data on the stack is called a “PUSH” and removing data from the Stack is called a “POP”

MIPS Stack PUSH # $SP = $SP – 4 # adjust stack pointer with addi addi $sp,$sp,-4 # memory($sp) = register value # save data on stack with sw sw$reg,0($sp)

MIPS Stack POP # register value = memory($sp) # get data from stack with lw lw$reg,0($sp) # $SP = $SP + 4 # adjust stack pointer with addi addi $sp,$sp,4

Programming trick when multiple Stack Ops needed Use only one addi instruction! Example: addi $sp,$sp,-8 #PUSH sw$reg1,4($sp) sw$reg2,0($sp). lw$reg1,4($sp) lw$reg2,0($sp) addi$sp,$sp,8 #POP Saves both execution time and memory!

Subroutine & Stack Ops Complex subroutines will need to PUSH several registers and the return address (if it has a jal) upon entry Next, the subroutine performs all of it’s operations. These will destroy or modify the registers that were saved on stack Prior to returning, it will POP saved registers and the return address (net $sp change must be 0! – this will cause major problems if it is not correct!!!!) Finally, it returns to the calling program with a jr $31

Example MIPS Subroutines with Stack Operations Factorial & Sort example programs found in Chapter 3 Factorial example program in Appendix A