MIPS functions.

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

Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
The University of Adelaide, School of Computer Science
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.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Solution 2nd Exam.
10/6: Lecture Topics Procedure call Calling conventions The stack
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
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
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
Solution to Problem Recitation #1 (Week 2) ECEN350 Prof. Choi.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
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
CSCE 212 Quiz 2 – 2/2/11 1.What is the purpose of the jal instruction? 2.What are the two conditional branching (if, goto; not the slt instruction) instructions.
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.
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.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
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.
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 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.
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
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Solutions Chapter 2.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
Lecture 5: Procedure Calls
MIPS Procedures.
Review.
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
MIPS Functions.
Program and memory layout
Program and memory layout
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.
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

MIPS functions

Question A stack is a data structure that is first in first out. N

Question In MIPS, accessing the stack involves two instructions. Y, addi $sp, $sp, -4, sw …,

Question A function can save registers values on the stack but need not pay attention to the order when restoring them. False

Questions Which of the following statements about MIPS stack is true? (a) If a function has to modify $sp, it can save $sp on the stack by a “sw $sp, 0($sp)” instruction and restore it later by a “lw $sp, 0($sp)” instruction. (b) A “push” operation adds a new number to the stack. “push $t0” can be implemented as a pseudo instruction as “sw $t0, -4($sp).” (c) Both of the above. (d) None of the above. d

Function calls inside a function What if we need to call another function inside a function? Will this work? twofun: addi $sp, $sp, -4 sw $s0, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

Function calls inside a function The problem is that the value of $ra is changed whenever you use jal somelabel. How to deal with it? twofun: addi $sp, $sp, -4 sw $s0, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

The working versions twofun2: addi $sp, $sp, -8 sw $s0, 4($sp) twofun1: addi $sp, $sp, -4 sw $s0, 0($sp) sw $ra, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0, lw $ra, 0($sp) addi $sp, $sp, 4 lw $s0, 0($sp) jr $ra twofun2: addi $sp, $sp, -8 sw $s0, 4($sp) sw $ra, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0, lw $ra, 0($sp) lw $s0, 4($sp) addi $sp, $sp, 8 jr $ra

Saving registers In case of nested function calls, before calling a function, to be safe, the caller should save $t0-$t9 save $a0-$a3 If such registers are needed later. and save $ra Because $ra is going to be needed later and it will be changed The callee should save $s0-s7

Question Which of the following statements about MIPS stack is true? (a) If a function does not call another function, it does not have to save $ra on the stack. (b) If $sp is modified inside the function, it must be saved on the stack. (c) If a function calls another function, it must save $v0 on the stack. (d) None of the above. A

c