Rocky K. C. Chang Version 0.1, 25 September 2017

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
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
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.
Ch. 8 Functions.
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
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS61C L8 Introduction to MIPS : Decisions II & Procedures I (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L11 Introduction to MIPS: Procedures I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
CS61C L8 MIPS Procedures (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #8 – MIPS.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,
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.
Lecture 6: Procedure Call (Part I)
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Machine Structures Lecture 4 – Introduction to MIPS Procedures I
Procedures I Fall 2010 Lecture 6: Procedures. Procedures I Fall 2010 Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);...
CS61C L8 Decisions(2), Procedures(1) (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I Internet2, a non-profit advanced.
CS 61C: Great Ideas in Computer Architecture Lecture 6: More MIPS, MIPS Functions Instructor: Sagar Karandikar
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
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.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
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.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 1 Instructors: John Wawrzynek & Vladimir Stojanovic
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
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.
IT 251 Computer Organization and Architecture Introduction to MIPS Procedures Chia-Chi Teng.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I Apple took a bold step recently.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
1. CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 2 Instructors: Vladimir Stojanovic & Nicholas Weaver
Decisions (cont), Procedures. Review Memory transfer: lw, sw, lb, sb A Decision allows us to decide what to execute at run-time rather than compile-time.
Computer Architecture & Operations I
Computer structure: Procedure Calls
Procedures (Functions)
Procedures (Functions)
Krste Asanović and Randy H. Katz
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Program and memory layout
Systems Architecture I
Program and memory layout
Procedures and addressing
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Presentation transcript:

Rocky K. C. Chang Version 0.1, 25 September 2017 2. Instruction sets II Rocky K. C. Chang Version 0.1, 25 September 2017

Goals of this lecture Understand the hardware support for procedures. Understand the two kinds of registers according to whether they are preserved across procedures. Able to use stack to store spill registers. Able to translate procedure calls to assembly code.

Six Fundamental Steps in Calling a Function Put parameters in a place where function can access them. Transfer control to function. Acquire (local) storage resources needed for function. Perform desired task of the function. Put result value in a place where calling program can access it and restore any registers that the function has used. Return control to point of origin, since a function can be called from several points in a program.

MIPS registers for function calls $a0–$a3: four argument registers to pass parameters $v0–$v1: two value registers to return values $ra (register 31): one return address register to return to the point of origin $at – assembler temporary register, used for pseudo instructions

Instruction Support for Functions (1/4) ... sum(a,b);... /* a,b:$s0,$s1 */ int sum(int x, int y) { return x+y; } address (shown in decimal) 1000 1004 1008 1012 1016 … 2000 2004 C M I P S In MIPS, all instructions are 4 bytes, and stored in memory just like data. So here we show the addresses of where the programs are stored.

Instruction Support for Functions (2/4) ... sum(a,b);... /* a,b:$s0,$s1 */ int sum(int x, int y) { return x+y; } address (shown in decimal) 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b 1008 addi $ra,$zero,1016 # $ra=1016 1012 j sum # jump to sum 1016 … # next instruction … 2000 sum: add $v0,$a0,$a1 2004 jr $ra # jump register instruction C M I P S

Instruction Support for Functions (3/4) ... sum(a,b);... /* a,b:$s0,$s1 */ int sum(int x, int y) { return x+y; } 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction C M I P S Question: Why use jr here? Why not use j? Answer: sum might be called by many places, so we can’t return to a fixed place. The calling proc to sum must be able to say “return here” somehow.

Instruction Support for Functions (4/4) Single instruction to jump and save return address: jump and link (jal) Before: 1008 addi $ra,$zero,1016 # $ra=1016 1012 j sum # goto sum After: 1008 jal sum # $ra=1012,goto sum Why have a jal? Make the common case fast: function calls very common. Don’t have to know where code is in memory with jal!

Function calling and return (1/2) Invoke function: jump and link instruction (jal) jal X # X is a label for a function Return from function: jump register instruction (jr) jr $ra For example, jal X # call the function # the next instruction … X: # start of the function jr $ra # end of the function

Function calling and return (2/2) The calling program (caller) $a0-$a3parameters Use jal X to invoke the function at address X. $raPC+4 (each instruction is of 4 bytes) PC holds the address of the current address being executed. The called program (callee) After performing the function, invokejr $ra: PC$ra. PC+4

Using more registers Besides the 4 argument and 2 return value registers, any registers (e.g., $s0-$s7 and $t0-$t7) used by the function should be recovered after the function call. Using stack: last-in-first-out queue (e.g., stack of plates) Push: placing data onto stack Pop: removing data from stack Stack in memory, so need a register to point to it. $sp (register 29) is the stack pointer in MIPS Convention is grow from high to low addresses Push decrements $sp, Pop increments $sp

Example We also need one temporary register $t0 int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; } Parameter variables g, h, i, and j in argument registers $a0, $a1, $a2, and $a3; and f in $s0 We also need one temporary register $t0

Stack Before, During, After Function Need to save old values of $s0 and $t0. Contents of $t0 Contents of $s0

MIPS Code for leaf_example addi $sp,$sp,-8 # adjust stack for 2 items sw $t0, 4($sp) # save $t0 for use afterwards sw $s0, 0($sp) # save $s0 for use afterwards add $s0,$a0,$a1 # f = g + h add $t0,$a2,$a3 # t0 = i + j sub $v0,$s0,$t0 # return value (g + h) – (i + j) lw $s0, 0($sp) # restore register $s0 for caller lw $t0, 4($sp) # restore register $t0 for caller addi $sp,$sp,8 # adjust stack to delete 2 items jr $ra # jump back to calling routine Better yet, temporary registers do not need to be preserved across a function call. So we can drop the sw and lw for $t0.

Review questions

Nested procedures (1/2) The previous example is a leaf function which does not call another function. For a non-leaf function, say A, it calls another function B. $a0a parameter jal A Within function A: jal B Problem: Conflict over the use of $a0 as well as $ra, and so are other registers used within A that need to be accessed after calling B. Save $ra on the stack

Nested Procedures (2/2) The solution is similar to saving the saved registers in a stack in the previous example. The caller saves the argument registers in the stack. The callee saves $ra in the stack and restore it before returning. That is, the callee now saves both the saved registers that it is going to use and $ra. What other registers need to be saved and restored?

Preservation of registers (1/3) To reduce expensive loads and stores from spilling and restoring registers, MIPS divides registers into two categories: Preserved across function call Caller can rely on values being unchanged $ra, $sp, $gp, $fp, “saved registers” $s0- $s7 Not preserved across function call Caller cannot rely on values being unchanged Return value registers $v0,$v1, Argument registers $a0-$a3, “temporary registers” $t0-$t9

Preservation of registers (2/3)

Preservation of registers (3/3) For caller A It does not need to worry about the preserved registers. For the nonpreserved registers that it is going to use after calling B, it must store them in a stack and restore it after the call. For callee B For the nonpreserved registers, it does not need to do anything. For the preserved registers, it must first store them in the stack and restore them before return.

Review questions

Example (1/2) How do we compile this? int sumSquare(int x, int y) { return mult(x,x)+ y; }

Example (2/2) # assume we have already passed the two parameters (x and y) to # $a0 and $a1, respectively. sumSquare: addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save return addr sw $a1, 0($sp) # save y add $a1,$a0,$zero # prepare the second parameter for multi() jal mult # call mult() lw $a1, 0($sp) # restore y add $v0,$v0,$a1 # mult()+y lw $ra, 4($sp) # get return addr addi $sp,$sp,8 # restore stack jr $ra mult: ...

Allocating Space on Stack C has two storage classes: automatic and static Automatic variables are local to function and discarded when function exits Static variables exist across exits from and entries to procedures Use stack for automatic (local) variables that don’t fit in registers Procedure frame or activation record: segment of stack with saved registers and local variables Some MIPS compilers use a frame pointer ($fp) to point to first word of frame

Review questions

Stack Before, During, After Call

Basic Structure of a Function Prologue entry_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp) # save $ra save other regs if need be ... restore other regs if need be lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra Body (call other functions…) Epilogue

Basic structure of a function call Setup the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack (in reverse order arg5 is at the top of the stack). Save caller-saved registers Save registers $t0-$t9 if they contain live values at the call site. Execute a jal instruction.

Where is the Stack in Memory? Stack starts in high memory and grows down Hexadecimal (base 16) : 7fff fffchex MIPS programs (text segment) in low end 0040 0000hex Static data segment (constants and other static variables) above text for static variables MIPS convention global pointer ($gp) points to static Heap above static for data structures that grow and shrink ; grows up to high addresses E.g., malloc in C, new in Java

MIPS Memory Allocation

Conclusion (1/2) Functions called with jal, return with jr $ra. The stack is your friend: Use it to save anything you need. Just leave it the way you found it! Instructions we know so far… Arithmetic: add, addi, sub, addu, addiu, subu Memory: lw, sw, lb, sb Decision: beq, bne, slt, slti, sltu, sltiu Unconditional Branches (Jumps): j, jal, jr Registers we know so far All of them (except for 3)! $a0-$a3 for function arguments, $v0-$v1 for return values $sp, stack pointer, $fp frame pointer, $ra return address

Conclusion (1/2)

Reading Read 2.8 in David Patterson and John Hennessy, Computer Organization and Design, 5th edition, Morgan Kaufmann, 2014.

acknowledgements This set of slides are prepared mainly based on The slides prepared by K. Asanovic & V. Stojanovic for CS61C at UC/Berkeley (http://inst.eecs.Berkeley.edu/~cs61c/sp15)

End