Lecture 8. MIPS Instructions #4 – Branch Instructions #2

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

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.
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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.
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.
10/6: Lecture Topics Procedure call Calling conventions The stack
MIPS Calling Convention Chapter 2.7 Appendix A.6.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
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.
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
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
INF 123 SW ARCH, DIST SYS & INTEROP LECTURE 9 Prof. Crista Lopes.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Procedures I Fall 2005 C functions main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */ int mult (int mcand,
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Lecture 3. RAS Issues in Lucida Prof. Taeweon Suh Computer Science Education Korea University COM609 Topics in Embedded Systems.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
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.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Lecture 19: Control Abstraction (Section )
Lecture 0. Course Introduction Prof. Taeweon Suh Computer Science Education Korea University COM515 Advanced Computer Architecture.
What is a function? Functions are nothing but sub-programs of a program. Is a part of a program which performs a particular task as desired by the programmer.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
Spring 2002INEL 4206 Microprocessors Lecture 5 1 Programming Universal Computers Instruction Sets Prof. Bienvenido Velez Lecture 5.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Introduction to the DE0 Board Prof. Taeweon Suh Computer Science & Engineering Korea University COSE221, COMP211 Computer Logic Design.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Lecture 4. MIPS Instructions #3 Branch Instructions Prof. Taeweon Suh Computer Science & Engineering Korea University COSE222, COMP212 Computer Architecture.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 6: Assembly Programs
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions in C Mrs. Chitra M. Gaikwad.
Functions and Procedures
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Lecture 4 Single Cycle Machine Prof. Xiaoyao Liang 2015/3/18
User Defined Functions
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS Functions.
CS149D Elements of Computer Science
Computer Science 210 Computer Organization
Predefined Functions Revisited
MIPS Functions.
Presentation transcript:

Lecture 8. MIPS Instructions #4 – Branch Instructions #2 2010 R&E Computer System Education & Research Lecture 8. MIPS Instructions #4 – Branch Instructions #2 Prof. Taeweon Suh Computer Science Education Korea University

Procedure (Function) Programmers use a procedure (or function) to structure programs To make the program modular and easy to understand To allow code to be re-used Procedures allow the programmer to focus on just one portion of the task at a time Parameters (arguments) act as an interface between the procedure and the rest of the program

High-level code example Procedure Calls Definitions Caller: calling procedure (in this case, main) Callee: called procedure (in this case, sum) High-level code example void main() { int y; y = sum(42, 7); ... } int sum(int a, int b) return (a + b);

MIPS Instructions for Accessing Procedures Procedure call instruction: jal ProcedureAddress #jump and link # $ra <- pc + 4 # pc <- jump target Saves PC+4 in register $ra to have a link to the next instruction for the procedure return Instruction format (J format): 26-bit address 0x03 High-level code int main() { simple(); a = b + c; } void simple() { return; MIPS assembly code 0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2 ... 0x00401020 simple: jr $ra PC compile PC+4 jal: jumps to simple and saves PC+4 in the return address register ($ra). In this case, $ra = 0x00400204 after jal executes void means that simple doesn’t return a value.

MIPS Instructions for Accessing Procedures Return instruction: jr $ra #return // pc <- $ra Instruction format (R format): 31 0x08 High-level code int main() { simple(); a = b + c; } void simple() { return; MIPS assembly code 0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2 ... 0x00401020 simple: jr $ra compile $ra contains 0x00400204 jr $ra: jumps to address in $ra, in this case 0x00400204.

Procedure Calls Procedure calling conventions: MIPS conventions: Caller: passes arguments to callee. jumps to the callee Callee: performs the procedure returns the result to caller returns to the point of call Must not overwrite registers or memory needed by the caller MIPS conventions: Call procedure: jump and link (jal) Return from procedure: jump register (jr) Argument values: $a0 - $a3 Return values: $v0, $v1

Input Arguments and Return Values High-level code int main() { int y; ... y = diffofsums(2, 3, 4, 5); // 4 arguments } int diffofsums(int f, int g, int h, int i) int result; result = (f + g) - (h + i); return result; // return value

Input Arguments and Return Values MIPS assembly code # $s0 = y main: ... addi $a0, $0, 2 # argument 0 = 2 addi $a1, $0, 3 # argument 1 = 3 addi $a2, $0, 4 # argument 2 = 4 addi $a3, $0, 5 # argument 3 = 5 jal diffofsums # call procedure add $s0, $v0, $0 # y = returned value # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller High-level code int main() { int y; ... y = diffofsums(2, 3, 4, 5); // 4 arguments } int diffofsums(int f, int g, int h, int i) int result; result = (f + g) - (h + i); return result; // return value

Input Arguments and Return Values MIPS assembly code # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller We need a place (called stack) to temporarily store arguments and registers because … diffofsums overwrote 3 registers: $t0, $t1, and $s0 , which may be being used in the main routine What if the callee (diffofsums) needs to use more registers than allocated to argument ($a0 - $a3) and return values ($v0, $v1)

The Stack Memory used to temporarily save variables Like a stack of dishes, stack is a data structure for spilling registers organized as a last-in-first-out (LIFO) queue

The Stack - Spilling Registers Stack is a data structure for spilling registers organized as a last-in-first-out (LIFO) queue One of the general registers, $sp ($29), is used to point to the top of the stack The stack “grows” from high address to low address in MIPS Push: add data onto the stack $sp = $sp – 4 data on stack at new $sp Pop: remove data from the stack Data from stack at $sp $sp = $sp + 4 low addr high addr $sp top of stack Main Memory

Stack Pointer (SP) Stack pointer ($sp) points to the top of the stack Main Memory Main Memory

How Procedures use the Stack Called procedures (callee) must not have any unintended side effects to the caller But, diffofsums overwrites 3 registers ($t0, $t1, $s0) as duplicated below: # MIPS assembly # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

Storing Register Values on the Stack # $s0 = result diffofsums: addi $sp, $sp, -12 # make space on stack # to store 3 registers sw $s0, 8($sp) # save $s0 on stack sw $t0, 4($sp) # save $t0 on stack sw $t1, 0($sp) # save $t1 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $t1, 0($sp) # restore $t1 from stack lw $t0, 4($sp) # restore $t0 from stack lw $s0, 8($sp) # restore $s0 from stack addi $sp, $sp, 12 # deallocate stack space jr $ra # return to caller “Push” (back up) the registers to be used in the callee to the stack “Pop” (restore) the registers from the stack prior to returning to the caller

Preserved and NonPreserved Registers In the previous example, if the calling procedure does not use the temporary registers ($t0, $t1), the effort to save and restore them is wasted To avoid this waste, MIPS divides registers into preserved and nonpreserved categories The preserved registers include $s0 ~ $s7 (saved) The nonpreserved registers include $t0 ~ $t9 (temporary) So, a procedure must save and restore any of the preserved registers it wishes to use, but it can change the nonpreserved registers freely The callee must save and restore any preserved registers it wishes to use The callee may change any of the nonpreserved registers But, if the caller is holding active data in a nonpreserved register, the caller needs to save and restore it Preserved (Callee-Saved) Nonpreserved (Caller-Saved) $s0 - $s7 $t0 - $t9 $ra $a0 - $a3 $sp $v0 - $v1 stack above $sp stack below $sp

Storing Saved Registers on the Stack # $s0 = result diffofsums: addi $sp, $sp, -4 # make space on stack to # store one register sw $s0, 0($sp) # save $s0 on stack # no need to save $t0 or $t1 add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $s0, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller

Nested Procedure Calls Procedures that do not call others are called leaf procedures Life would be simple if all procedures were leaf procedures, but they aren’t For example, the main program calls procedure A with an argument of 3 (by placing the value 3 into register $a0 and then using jal A) Procedure A calls procedure B via jal B with an argument 7 (also placed in $a0) There is a conflict over the use of register $a0 and $ra Use stack to preserve registers The caller pushes any argument registers ($a0~$a4) or temporary registers ($t0~$t9) that are needed after the call proc1: addi $sp, $sp, -4 # make space on stack sw $ra, 0($sp) # save $ra on stack jal proc2 ... lw $ra, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller

Recursive Procedure Call Recursive procedures invoke “clones” of themselves MIPS assembly code 0x90 factorial: addi $sp, $sp, -8 # make room 0x94 sw $a0, 4($sp) # store $a0 0x98 sw $ra, 0($sp) # store $ra 0x9C addi $t0, $0, 2 0xA0 slt $t0, $a0, $t0 # a <= 1 ? 0xA4 beq $t0, $0, else # no: go to else 0xA8 addi $v0, $0, 1 # yes: return 1 0xAC addi $sp, $sp, 8 # restore $sp 0xB0 jr $ra # return 0xB4 else: addi $a0, $a0, -1 # n = n - 1 0xB8 jal factorial # recursive call 0xBC lw $ra, 0($sp) # restore $ra 0xC0 lw $a0, 4($sp) # restore $a0 0xC4 addi $sp, $sp, 8 # restore $sp 0xC8 mul $v0, $a0, $v0 # n * factorial(n-1) 0xCC jr $ra # return High-level code int factorial(int n) { if (n <= 1) return 1; else return (n * factorial(n-1)); }

Stack during Recursive Call