Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III

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

The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
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.
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
CS3350B Computer Architecture Winter 2015 Lecture 4
Lecture 8: MIPS Instruction Set
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
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.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CHAPTER 2 ISA Instructions (logical + procedure call)
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture.
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.
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
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.
Lecture 4: MIPS Instruction Set
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
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
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
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III

ECE232: MIPS Instructions-III 2 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Example: Array Access  Access the i-th element of an array A (each element is 32-bit long) # $t0 = address of start of A # $t1 = i sll $t1,$t1,2 # $t1 = 4*i add$t2,$t0,$t1 # add offset to the address of A[0] # now $t2 = address of A[i] lw $t3,0($t2) # $t3 = whatever is in A[i]

ECE232: MIPS Instructions-III 3 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren if statement if ( condition ) { statements } # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, if_end_label # MIPS code for the statements if_end_label:

ECE232: MIPS Instructions-III 4 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren if else statement if ( condition ) { if-statements } else { else-statements } # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, else_label # MIPS code for the if-statements j if_end_label else_label: # MIPS code for the else-statements if_end_label:

ECE232: MIPS Instructions-III 5 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren while statement while ( condition ) { statements } while_start_label: # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, while_end_label # MIPS code for the statements j while_start_label while_end_label:

ECE232: MIPS Instructions-III 6 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren do-while statement do { statements } while ( condition ); do_start_label: # MIPS code for the statements do_cond_label: # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, do_end_label j do_start_label do_end_label:

ECE232: MIPS Instructions-III 7 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren for loop for ( init ; condition ; incr ) { statements } # MIPS code for the init expression for_start_label: # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, for_end_label # MIPS code for the statements # MIPS code for the incr expression j for_start_label for_end_label:

ECE232: MIPS Instructions-III 8 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren switch statement switch ( expr ) { case const1: statement1 case const2: statement2... case constN: statementN default: default-statement }

ECE232: MIPS Instructions-III 9 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren MIPS code for switch statement # MIPS code for $t0= expr beq $t0, const1, switch_label_1 beq $t0, const2, switch_label_2... beq $t0, constN, switch_label_N j switch_default switch_label_1: # MIPS code to compute statement1 switch_label_2: # MIPS code to compute statement2... switch_default: # MIPS code to compute default-statement switch_end_label:

ECE232: MIPS Instructions-III 10 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Logical AND in expression if (cond1 && cond2){ statements } # MIPS code to compute cond1 # Assume that this leaves the value in $t0 # If cond1=false $t0=0 beq $t0, $zero, and_end # MIPS code to compute cond2 # Assume that this leaves the value in $t0 # If cond2=false $t0=0 beq $t0, $zero, and_end # MIPS code for the statements and_end:

ECE232: MIPS Instructions-III 11 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Switch Example switch (i) {//Assume i is in $s1 and j is in $s2; case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17; } main: add$t0, $zero, $zero# $t0 = 0, temp. variable beq$t0, $s1, case0# go to case0 addi$t0, $t0, 1# $t0 = 1 beq$t0, $s1, case1# go to case1 addi$t0, $t0, 1# $t0 = 2 beq$t0, $s1, case2# go to case2 addi$t0, $t0, 1# $t0 = 3 beq$t0, $s1, case3# go to case3 addi$t0, $t0, 1# $t0 = 4 beq$t0, $s1, case4# go to case4 jdefault# go to default case case0: addi$s2, $zero, 3# j = 3 jfinish# exit switch block

ECE232: MIPS Instructions-III 12 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Example: Conditional and unconditional branches  Conditional branch: Jump to instruction L1 if register1 equals register2: beq $s1, $s2, L1 Similarly, bne  Unconditional branch: j L1 jr $s5 (useful for large case statements and big jumps)  Convert to assembly: if (i == j) f = g+i; else f = g-i; bne $s0, $s1, ELSE add $s3, $s2, $s0 j EXIT ELSE: sub $s3, $s2, $s0 EXIT:

ECE232: MIPS Instructions-III 13 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: Example 2  Convert to assembly:  while (save[i] == k) i += 1;  i and k are in $s3 and $s5 and  base of array save[] is in $s6

ECE232: MIPS Instructions-III 14 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Procedures  Each procedure (function, subroutine) maintains a scratchpad of register values – when another procedure is called (the callee), the new procedure takes over the scratchpad – values may have to be saved so we can safely return to the caller parameters (arguments) are placed where the callee can see them control is transferred to the callee acquire storage resources for callee execute the procedure place result value where caller can access it return control to caller

ECE232: MIPS Instructions-III 15 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Registers  The 32 MIPS registers are partitioned as follows: Register 0 : $zero always stores the constant 0 Regs 2-3 : $v0, $v1 return values of a procedure Regs 4-7 : $a0-$a3 input arguments to a procedure Regs 8-15 : $t0-$t7 temporaries Regs 16-23: $s0-$s7 variables Regs 24-25: $t8-$t9 more temporaries Reg 28 : $gp global pointer Reg 29 : $sp stack pointer Reg 30 : $fp frame pointer Reg 31 : $ra return address

ECE232: MIPS Instructions-III 16 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Jump-and-Link  A special register (not part of the register file) maintains the address of the instruction currently being executed – this is the program counter (PC)  The procedure call is executed by invoking the jump-and-link (jal) instruction – the current PC (actually, PC+4) is saved in the register $ra and  jump to the procedure’s address (the PC is accordingly set to this address) jal NewProcedureAddress  Since jal may over-write a relevant value in $ra, it must be saved somewhere (in memory?) before invoking the jal instruction  How do we return control back to the caller after completing the callee procedure?

ECE232: MIPS Instructions-III 17 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Proc A’s values Proc B’s values Proc C’s values … High address Low address Stack grows this way Proc A call Proc B … call Proc C … return The Stack  The register scratchpad for a procedure seems volatile  It may be modified every time we switch procedures  A procedure’s values are therefore backed up in memory on a stack

ECE232: MIPS Instructions-III 18 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren What values are saved? PreservedNot Preserved Saved registers: $s0-$s7Temporary registers: $t0-$t9 Stack Pointer: $spArgument registers: $a0-$a3 Return Address Register: $raReturn registers: $v0-$v1 Stack above the stack pointerStack below the pointer $sp  7fff fffc $gp  pc  Stack Dynamic Data Static Data Text Reserved

ECE232: MIPS Instructions-III 19 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Storage Management on a Call/Return  Arguments are copied into $a0-$a3; the jal is executed  The new procedure (callee) must create space for all its variables on the stack  After the callee creates stack space, it updates the value of $sp  Once the callee finishes, it copies the return value into $v0, frees up stack space, and $sp is incremented  On return, the caller may bring in its stack values, ra, temps into registers  The responsibility for copies between stack and registers may fall upon either the caller or the callee

ECE232: MIPS Instructions-III 20 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Leaf Procedure Example  Procedures that don’t call other procedures  C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } Arguments g, …, j in $a0, …, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0

ECE232: MIPS Instructions-III 21 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Leaf Procedure Example  MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Restore $s0 Result Return int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; }

ECE232: MIPS Instructions-III 22 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Non-Leaf Procedures  Procedures that call other procedures  For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call  Restore from the stack after the call  Example - Recursion (C code): int factorial (int n) { if (n < 1) return 1; else return n * factorial (n - 1); } Argument n in $a0 Result in $v0

ECE232: MIPS Instructions-III 23 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Non-Leaf Procedure Example  MIPS code: factorial: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for i < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and return L1: addi $a0, $a0, -1 # else decrement i jal factorial # recursive call lw $a0, 0($sp) # restore previous i lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return Notes: The callee saves $a0 and $ra in its stack space. Temps are never saved.

ECE232: MIPS Instructions-III 24 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Recursion: Factorial int factorial (int n) { if (n < 1) return 1; else return n * factorial (n - 1); }

ECE232: MIPS Instructions-III 25 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Stack Dynamic data (heap) Static data (globals) Text (instructions) Memory Organization  The space allocated on stack by a procedure is termed the activation record (includes saved values and data local to the procedure)  Frame pointer points to the start of the record and stack pointer points to the end  Variable addresses are specified relative to $fp as $sp may change during the execution of the procedure  $gp points to area in memory that saves global variables  Dynamically allocated storage (with malloc()) is placed on the heap

ECE232: MIPS Instructions-III 26 Adapted from Computer Organization and Design, Patterson&Hennessy, UCB, Kundu,UMass Koren Summary  The jal instruction is used to jump to the procedure and save the current PC (+4) into the return address register  Arguments are passed in $a0-$a3; return values in $v0-$v1  Since the callee may over-write the caller’s registers, relevant values may have to be copied into memory  Each procedure may also require memory space for local variables  A stack is used to organize the memory needs for each procedure