Pick up the handout on your way in!!

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

MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
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.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Comp Sci Control structures 1 Ch. 5 Control Structures.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Lecture 4. MIPS Instructions #3 Branch Instructions Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced Computer Architecture.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
Lecture 8: MIPS Instruction Set
1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos.
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.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
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.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
March 21, 2016© Craig ZIlles (adapted from slides by Howard Huang) 1 What does this code do? label:sub$a0, $a0, 1 bne$a0, $zero, label Arf.
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
What does this code do? label: sub $a0, $a0, 1 bne $a0, $zero, label
Computer Architecture & Operations I
Computer Science 210 Computer Organization
CSCI206 - Computer Organization & Programming
Prof. Hsien-Hsin Sean Lee
Lecture 6: Assembly Programs
CS2100 Computer Organisation
Procedures 101: There and Back Again
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
Procedures (Functions)
The University of Adelaide, School of Computer Science
CSCI206 - Computer Organization & Programming
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
The University of Adelaide, School of Computer Science
Lecture 6: Assembly Programs
Systems Architecture I
Conditionals and Functions
Computer Architecture
9/27: Lecture Topics Memory Data transfer instructions
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
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

Pick up the handout on your way in!! CS 232 Definition: Recursion: If you still don't get it, see: "Recursion". Pick up the handout on your way in!! 11/13/2018 If/Else and Recursion in MIPS

Control flow in high-level languages The instructions in a program usually execute one after another, but it’s often necessary to alter the normal control flow. Conditional statements execute only if some test expression is true. // Find the absolute value of *a0 v0 = *a0; if (v0 < 0) v0 = -v0; // This might not be executed v1 = v0 + v0; Loops cause some statements to be executed many times. // Sum the elements of a five-element array a0 v0 = 0; t0 = 0; while (t0 < 5) { v0 = v0 + a0[t0]; // These statements will t0++; // be executed five times } 11/13/2018 If/Else and Recursion in MIPS

MIPS control instructions In section, we introduced some of MIPS’s control-flow instructions j // for unconditional jumps bne and beq // for conditional branches slt and slti // set if less than (w/ and w/o an immediate) And how to implement loops And branch pseudo instructions: blt $t0, $t1, L1 // Branch if $t0 < $t1 ble $t0, $t1, L2 // Branch if $t0 <= $t1 bgt $t0, $t1, L3 // Branch if $t0 > $t1 bge $t0, $t1, L4 // Branch if $t0 >= $t1 Today, we’ll talk about if/else 11/13/2018 If/Else and Recursion in MIPS

Translating an if-then statement We can use branch instructions to translate if-then statements into MIPS assembly code. v0 = *a0; if (v0 < 0) v0 = -v0; v1 = v0 + v0; Sometimes it’s easier to invert the original condition. In this case, we changed “continue if v0 < 0” to “skip if v0 >= 0”. This saves a few instructions in the resulting assembly code. lw $v0, 0($a0) bge $v0, 0, skip sub $v0, $zero, $v0 skip: add $v1, $v0, $v0 11/13/2018 If/Else and Recursion in MIPS

Translating an if-then-else statements If there is an else clause, it is the target of the conditional branch And the then clause needs a jump over the else clause // increase the magnitude of v0 by one if (v0 < 0) bge $v0, $0, E v0 --; sub $v0, $v0, 1 j L else v0 ++; E: add $v0, $v0, 1 v1 = v0; L: move $v1, $v0 Dealing with else-if code is similar, but the target of the first branch will be another if statement. 11/13/2018 If/Else and Recursion in MIPS

If/Else and Recursion in MIPS Last time we talked about function calls… Recursion is just a special case of function calls Two parts: Base case: no recursion, often doesn’t call any functions Recursive body: calls itself 11/13/2018 If/Else and Recursion in MIPS

Recursion in MIPS (single function call) Suggestions for simply implementing recursive function calls in MIPS Handle the base case first Before you allocate a stack frame if possible Allocate stack frame Save return address Recursive Body: Save any registers needed after the call Compute arguments Call function Restore any registers needed after the call Consume return value (if any) Deallocate stack frame and return. 11/13/2018 If/Else and Recursion in MIPS

Recursion in MIPS (multiple function calls – caller save) Suggestions for simply implementing recursive function calls in MIPS Handle the base case first Before you allocate a stack frame if possible Allocate stack frame Save return address For each function call: (suggestion: use $s registers if >1 call) Save any registers needed after the call Compute arguments Call function Restore any registers needed after the call Consume return value (if any) Deallocate stack frame and return. 11/13/2018 If/Else and Recursion in MIPS

Recursion in MIPS (multiple function calls – callee save) Suggestions for simply implementing recursive function calls in MIPS Handle the base case first Before you allocate a stack frame if possible Allocate stack frame Save return address Save enough $s registers to hold your local variables Copy your local variables to $s registers For each function call: Save any registers needed after the call Compute arguments Call function Restore any registers needed after the call Consume return value (if any) Restore $s registers Deallocate stack frame and return. 11/13/2018 If/Else and Recursion in MIPS