ECE 15B Computer Organization Spring 2010

Similar documents
Lecture 7: Procedures

CS61C : Machine Structures

CS61C : Machine Structures

UCB CS61C : Machine Structures

CS61C : Machine Structures

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC)

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CS 61c: Great Ideas in Computer Architecture

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

Lecture 7: Procedures and Program Execution Preview

ECE 154A Introduction to. Fall 2012

Computer Architecture. The Language of the Machine

MIPS Instruction Set

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Agenda. Strings: C vs. Java. Loading, Storing bytes. Support for Characters and Strings 6/29/2011

MIPS%Assembly% E155%

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

Q1: /30 Q2: /25 Q3: /45. Total: /100

ECE260: Fundamentals of Computer Engineering

MIPS Functions and Instruction Formats

Chapter 2A Instructions: Language of the Computer

Computer Architecture. MIPS Instruction Set Architecture

CS 61C: Great Ideas in Computer Architecture. More MIPS, MIPS Functions

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

CS 110 Computer Architecture MIPS Instruction Formats

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

ECE260: Fundamentals of Computer Engineering

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

Lecture 5: Procedure Calls

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

MIPS Reference Guide

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

ECE232: Hardware Organization and Design

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

MIPS Instruction Set Architecture (2)

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

The MIPS Instruction Set Architecture

SPIM Instruction Set

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

MIPS Instruction Reference

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 9 December 2015

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Three 10 April 2013

CS 61C: Great Ideas in Computer Architecture. Lecture 6: More MIPS, MIPS Func.ons. Instructor: Sagar Karandikar

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Thomas Polzer Institut für Technische Informatik

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

F. Appendix 6 MIPS Instruction Reference

COMPUTER ORGANIZATION AND DESIGN

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

CS61C : Machine Structures

MIPS Assembly Language. Today s Lecture

CENG3420 Lecture 03 Review

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

Computer Architecture

ECE Exam I February 19 th, :00 pm 4:25pm

Assembly Programming

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Instructions: Assembly Language

ECE 2035 A Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 14 December 2016

Question 0. Do not turn this page until you have received the signal to start. (Please fill out the identification section above) Good Luck!

CS3350B Computer Architecture MIPS Instruction Representation

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Computer Architecture

M2 Instruction Set Architecture

Architecture II. Computer Systems Laboratory Sungkyunkwan University

And in Review. Register Conventions (2/4) saved. Register Conventions (1/4) Register Conventions (4/4) Register Conventions (3/4) volatile

Instructions: Language of the Computer

Programming the processor

Chapter 2. Instructions: Language of the Computer

TSK3000A - Generic Instructions

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

Concocting an Instruction Set

Transcription:

ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy, and classes taught by and classes taught by Patterson at Berkeley and Ryan Kastner at UCSB

Review of the last lecture: Logic and Shift Instructions and multiplication/division

Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR or, ori Bitwise NOT ~ ~ nor Useful for extracting and inserting groups of bits in a word

AND Operations Useful to mask bits in a word Select some bits, clear others to 0 and $t0, $t1, $t2 $t2 $t1 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0000 1100 0000 0000

OR Operations Useful to include bits in a word Set some bits to 1, leave others unchanged or $t0, $t1, $t2 $t2 $t1 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0011 1101 1100 0000

NOT Operations Useful to invert bits in a word Change 0 to 1, and 1 to 0 MIPS has NOR 3 operand instruction a NOR b == NOT ( a OR b ) nor $t0, $t1, $zero Register 0: always read as zero $t1 $t0 0000 0000 0000 0000 0011 1100 0000 0000 1111 1111 1111 1111 1100 0011 1111 1111

Shift Operations op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits shamt: how many positions to shift Shift left logical (SLL) Shift left and fill with 0 bits sll by i bits multiplies by 2 i Shift hf right logical l( (SRL) Shift right and fill with 0 bits srl by i bits divides by 2 i (unsigned only) Shift right arithmetic (SRA) Shift right and fill emptied bits by sign extending Notethat that shamt (immediatevalue) is only 5 bits

Uses for Shift Instructions Very convenient operation to extract group of bits (e.g. one byte) within a word (e.g. think of operations on 8 bit pixels or 8 bit characters) For example, suppose we want to get bits 8 to 15 from $t0. The code below will do the job sll $t0, $t0, 16 srl $t0,,$ $t0, 24

Uses for Shift Instructions Since ceshifting is faster than multiplication, utp cato,a good compiler (or a good programmer for that matter) usually notices when C code multiplies by a power of 2 and compiles it to a shift hf instruction For example: a = a*8; (in C) would compile to: sll $s0, $s0, 3 (in MIPS)

Use for Shift Instructions Let s consider fast version for 4 bit unsigned number multiplication. Let s have multiplicand l in $a0 and multiplier l in $a1 and let s assume that we don t have to preserve the values of $a0 and $a1. addi $t1, $0, 4 # initialize loop counter srl $a0, $a0, 4 # align multiplicand with high 4 bit portion of the product loop: andi $t0, $a1, 1 # get the current LSB of the product (i.e. that of the multiplier) beq $t0, $0, skipadd # skip addition of the multiplicand to the product if current last bit of the product is 0 add $a1, $a1, $a0 # add multiplicand to the product skipadd:addi $t1, $t1, 1 # decrement loop counter srl $a1, $a1, 1 # shift to the right product by one bit bne $t1, $0, loop; # repeat loop 4 times The code will have 8 bit product in register $a1

Use for Shift Instructions The previous code have branch instruction in the loop. It is desirable to avoid branching whenever possible so that using shift and logic (masking) )instruction i we can have instead: addi $t1, $0, 4 # initialize loop counter srl $a0, $a0, 4 # align multiplicand with high 4 bit portion of the product loop: sll $t0, $a1, 31 # move the current LSB of the product to the MSB srla $t0, $t0, 24 # perform sign extension which will create mask with LSB value of the product and $t0, $t0, $a0 # if LSB =1 then $t0 = $a0, if LSB = 0 then $t0 = 0 add $a1, $a1, $t0 # add $t0 to the product skipadd:addi $t1, $t1, 1 # decrement loop counter srl $a1, $a1, 1 # shift to the right product by one bit bne $t1, $0, loop; # repeat loop 4 times

Multiplication by Constants Multiplication utp cato by the econstant t could be implemented more efficiently For example: b = a*7 is the same as b = a*8 a which can be implemented as one shift and one sub operation which could be faster than performing mult operation or if mult is not available faster than sequential multiplication algorithm Compilerwill typicallydo the optimization for you

Division: Sign of the remainder divident = quotient*divisor + reminder for division i i of signed numbers the sign of the reminder is always the same as the sign of the dividentid correct: 7 = 3 * 2 1 incorrect: 77 = 44 * 2 + 1

Procedures

C functions main() { int a,b,c;... c = sum(a,b);... } What information must compiler/programmer keep track of? /* really dumb mult function */... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } ret rn + What instructions can accomplish this?

Function Call Bookkeeping Registers play a major role in keeping track of information for function calls. Register conventions: Return address $ra Arguments $a0, $a1, $a2, $a3 Return value $v0, $v1 Local variables $s0, $s1,, $s7 The stack is also used; more later.

Instruction Support for Functions C... 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 M In MIPS, all instructions are 4 I bytes, and stored in memory P just like data. So here we S show the addresses of where the programs are stored.

Instruction Support for Functions... 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 $ 1 $ # y = b I 1008 addi $ra,$zero,1016 #$ra=1016 1012 j sum #jump to sum P 1016 C M S 2000 sum: add $v0,$a0,$a1$a0 $a1 2004 jr $ra # new instruction

Instruction Support for Functions C M I P S... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } 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. 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction

Instruction Support for Functions Syntax for jal (jump and link) is same as for j (jump): jal label jal should really be called laj for link and jump : Step 1 (link): Save address of next instruction into $ra Why next instruction? Why not current one? Step 2 (jump): Jump to the given label

Instruction Support for Functions Syntax for jr (jump register): jr register Instead of providing a label to jump to, the jr instruction provides a register which contains an address to jump to. Very useful for function calls: jal stores return address in register ($ra) jr $ra jumps back to that address

Nested Procedures int sumsquare(int x, int y) { return mult(x,x)+ y; } Something called sumsquare,, now sumsquare is calling mult. So there s a value in $ra that sumsquare wants to jump back to, but this will be overwritten by the call to mult. Need to save sumsquare return address before call to mult.

Nested Procedures In general, may need to save some other info in addition to $ra. When a C program is run, there are 3 important memory areas allocated: Static: Variables declared once per program, cease to exist only after execution completes. E.g., g, C globals Heap: Variables declared dynamically via malloc Stack: Space to be used by procedure during execution; this is where we can save register values

Address C memory Allocation review $sp stack pointer 0 Stack Heap Staticti Code Space for saved procedure information Explicitly created space, i.e., malloc() Variables declared once per program; e.g., globals Program

Using the Stack So we have a register $sp which always points to the last used space in the stack. To use stack, we decrement this pointer by the eamount of space we need and then fill it with info. So, how do we compile this? int sumsquare(int x, int y) { return mult(x,x)+ y; }

Using the Stack Hand compile int sumsquare(int x, int y) { sumsquare: return mult(x,x)+ y; } addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save ret addr sw $a1, 0($sp) # save y add $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $a1, 0($sp) # restore y add $v0,$v0,$a1 # mult()+y lw $ra, 4($sp) # get ret addr addi $sp,$sp,8 $ # restore stack jr $ra mult:... push pop

Procedure Calls Steps for Making a Procedure Call 1. Save necessary values onto stack 2. Assign argument(s), if any 3. jal call 4. Restore values from stack Rules for Procedures Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3 Return value is always in $v0 (and if necessary in $v1) Must follow register conventions So what are they?

MIPS Registers The constant 0 $0 $zero Reserved for Assembler $1 $at Return Values $2 $3 $ $v0 $v1 $ Arguments $4 $7 $a0 $a3 Temporary $8 $15 $t0 $t7 Saved $16 $23 $s0 $s7 More Temporary $24 $25 $25 $t8 $t9$t9 Used by Kernel $26 27 $k0 $k1 Global Pointer $28 $gp Stack Pointer $29 $sp Frame Pointer $30 $fp Return Address $31 $ra Use names for registers code is clearer!

Other Registers $at: may be used by the assembler at any time; unsafe to use $k0-$k1: may be used by the OS at any time; unsafe to use $gp, $fp: don t worry about them Note: Feel free to read up on $gp and $fp in Appendix B, but you can write perfectly good MIPS code without them.

Register Convention Caller The calling function Callee The function being called When the callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged Register conventions A set of generally accepted rules as to which registeres will be unchanged after a procedure call (jal) and which may be changed

Saved Register Convention $0 No change. Always 0 $s0 $s7 $sp Restore if you change. That is whythe are called saved registers. If the callee changes these in any way, it must restore the original values before returning Restore is you change The stack pointer must point to the same place before and after the jl jal call (otherwise the caller won t be able to restore values from the stack) Hint all saved registers starts with S

Volatile Register Convention $ra Can change. The jal call itself will change this register. Note that caller needs to save on stack if nested call $v0 $v1$v1 Can change. These will contain the new returned values $a0 $a3 Can change. These are volatile argument registers. Caller needs to save them if it will need them after the call $t0 $t9 $t9 Can change. Called temporary. Any procedure may change them at any time. Caller needs to save them if it will need them after the call

Register Conventions What do these eseconventions o mean? If function R calls function E, then Function R must save any temporary registers that it may be using onto the stack before making a jal call Function E must save any saved registers it intends to usebeforegarbling uptheir values Remember: Caller/Callee need to save only temporary/saved registers they are using, not all registers s

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

Review Instructions so far: arithmetics: add, addi, sub, addu, addiu, subu arithmetics: mult, div, mfhi, mflo, multu, divu Memory operations: lw, sw, lb, lbu, lh, lhu, sb, shw Control: beq, bne, j, slt, slti, sltu, sltui, jal, jr Logic/shift: and, andi, or, ori, xor, xori, sll, srl, sra, srlv, sllv, nor Registers: All of them: $0 $31