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

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

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

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

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

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

CS61C : Machine Structures

UCB CS61C : Machine Structures

CS61C : Machine Structures

Lecture 7: Procedures

CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and MIPS Instruction Set Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CS61C : Machine Structures

CS 110 Computer Architecture MIPS Instruction Formats

ECE 15B Computer Organization Spring 2010

CS 61c: Great Ideas in Computer Architecture

Reduced Instruction Set Computer (RISC)

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

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 )

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

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

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

CS61C : Machine Structures

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

Control Instructions

MIPS Functions and Instruction Formats

CENG3420 Lecture 03 Review

CS61C : Machine Structures

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

CS 61C: Great Ideas in Computer Architecture. Lecture 5: Intro to Assembly Language, MIPS Intro. Instructor: Sagar Karandikar

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

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

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

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

UCB CS61C : Machine Structures

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

COE608: Computer Organization and Architecture

CS61C Machine Structures. Lecture 10 - MIPS Branch Instructions II. 2/8/2006 John Wawrzynek. (

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

Lecture 5: Procedure Calls

UCB CS61C : Machine Structures

Review. Lecture #7 MIPS Decisions So Far...

CSE Lecture In Class Example Handout

Thomas Polzer Institut für Technische Informatik

Chapter 2A Instructions: Language of the Computer

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

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

Lecture 7: Procedures and Program Execution Preview

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

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

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

Computer Architecture

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Levels of Programming. Registers

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

Lecture 6: Assembly Programs

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CMPE324 Computer Architecture Lecture 2

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Lecture 5: Procedure Calls

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Chapter 2. Instructions:

CS 110 Computer Architecture Review for Midterm I

Chapter 2. Instruction Set Architecture (ISA)

COMPUTER ORGANIZATION AND DESIGN

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

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

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Chapter 2: Instructions:

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

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

CENG3420 L03: Instruction Set Architecture

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

ECE369. Chapter 2 ECE369

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

CSE Lecture In Class Example Handout

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

Instruction Set Design and Architecture

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

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

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

Instructions: Assembly Language

Course Administration

Chapter 2. Instructions: Language of the Computer

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

MIPS Instruction Set Architecture (2)

ECE260: Fundamentals of Computer Engineering

CS 110 Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro

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

CS 61c: Great Ideas in Computer Architecture

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

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

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Chapter 3. Instructions:

Transcription:

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University Slides based on UC Berkley's CS61C 1

Machine Interpretation Levels of Representation/Interpretation High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Assembler Machine Language Program (MIPS) Hardware Architecture Description (e.g., block diagrams) Architecture Implementation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) Anything can be represented as a number, i.e., data or instructions 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 Logic Circuit Description (Circuit Schematic Diagrams) 2

From last lecture Computer words and vocabulary are called instructions and instruction set respectively MIPS is example RISC instruction set used here Rigid format: 1 operation, 2 source operands, 1 destination add,sub,mul,div,and,or,sll,srl,sra lw,sw,lb,sb to move data to/from registers from/to memory beq, bne, j, slt, slti for decision/flow control Simple mappings from arithmetic expressions, array access, in C to MIPS instructions 3

Review: Components of a Computer Processor Control Enable? Read/Write Memory Input Datapath Program Counter Address Program Bytes Registers Write Data Arithmetic & Logic Unit (ALU) Read Data Data Output Processor-Memory Interface I/O-Memory Interfaces 4

How Program is Stored Memory Program Bytes One MIPS Instruction = 32 bits Data 5

foo.s Assembler to Machine Code (more later in course) bar.s Assembler source files (text) Assembler foo.o Assembler bar.o Assembler converts humanreadable assembly code to instruction bit patterns Machine code object files Linker lib.o Pre-built object file libraries a.out Machine code executable file 6

Processor Control Executing a Program Read Instruction Bits Memory Datapath PC Registers Arithmetic & Logic Unit (ALU) Instruction Address Program Bytes Data The PC (program counter) is internal register inside processor holding byte address of next instruction to be executed. Instruction is fetched from memory, then control unit executes instruction using datapath and memory system, and updates program counter (default is add +4 bytes to PC, to move to next sequential instruction) 7

Computer Decision Making Based on computation, do something different In programming languages: if-statement MIPS: if-statement instruction is beq register1,register2,l1 means: go to statement labeled L1 if (value in register1) == (value in register2) L1: instruction #this is a label.otherwise, go to next statement beq stands for branch if equal Other instruction: bne for branch if not equal 8

Types of Branches Branch change of control flow Conditional Branch change control flow depending on outcome of comparison branch if equal (beq) or branch if not equal (bne) Unconditional Branch always branch a MIPS instruction for this: jump (j) 9

Example if Statement Assuming translations below, compile if block f $s0 g $s1 h $s2 i $s3 j $s4 if (i == j) bne $s3,$s4,exit f = g + h; add $s0,$s1,$s2 Exit: May need to negate branch condition 10

Example if-else Statement Assuming translations below, compile f $s0 g $s1 h $s2 i $s3 j $s4 if (i == j) f = g + h; else f = g h; bne $s3,$s4,else add $s0,$s1,$s2 j Exit Else: sub $s0,$s1,$s2 Exit: 11

Inequalities in MIPS Until now, we ve only tested equalities (== and!= in C). General programs need to test < and > as well. Introduce MIPS Inequality Instruction: Set on Less Than Syntax: slt reg1,reg2,reg3 Meaning: if (reg2 < reg3) reg1 = 1; else reg1 = 0; set means change to 1, reset means change to 0. 12

Inequalities in MIPS Cont. How do we use this? Compile by hand: if (g < h) goto Less; # g:$s0, h:$s1 Answer: compiled MIPS code slt $t0,$s0,$s1 bne $t0,$zero,less # $t0 = 1 if g<h # if $t0!=0 goto Less Register $zero always contains the value 0, so bne and beq often use it for comparison after an slt instruction sltu treats registers as unsigned 13

Immediates in Inequalities slti an immediate version of slt to test against constants Loop:... slti $t0,$s0,1 # $t0 = 1 if # $s0<1 beq $t0,$zero,loop # goto Loop # if $t0==0 # (if ($s0>=1)) 14

Loops in C/Assembly Simple loop in C; A[] is an array of ints do { g = g + A[i]; i = i + j; } while (i!= h); Use this mapping: g, h, i, j, &A[0] $s1, $s2, $s3, $s4, $s5 Loop: sll $t1,$s3,2 # $t1= 4*i addu $t1,$t1,$s5 # $t1=addr A+4i lw $t1,0($t1) # $t1=a[i] add $s1,$s1,$t1 # g=g+a[i] addu $s3,$s3,$s4 # i=i+j bne $s3,$s2,loop # goto Loop # if i!=h 15

Control-flow Graphs: A visualization bne $s3,$s4,else add $s0,$s1,$s2 j Exit Else:sub $s0,$s1,$s2 Exit: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: 16

Question! addi $s0,$zero,0 Start: slt $t0,$s0,$s1 beq $t0,$zero,exit sll $t1,$s0,2 addu $t1,$t1,$s5 lw $t1,0($t1) add $s4,$s4,$t1 addi $s0,$s0,1 j Start What is the code above? A: while loop B: do while loop Exit: C: for loop D: A or C E: Not a loop 17

Administrivia HW3 is published Quicksort in MIPS due April 2 nd. HW2 is due next Monday! Go to OH if you have problems don t ask your fellow students Use piazza frequently. Class schedule has been updated: Look at website! http://shtech.org/course/ca/18s/ Mid-Term I: April 19 Mid-Term II: May 17 18

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

MIPS Function Call Conventions Registers faster than memory, so use them $a0 $a3: four argument registers to pass parameters ($4 - $7) $v0,$v1: two value registers to return values ($2,$3) $ra: one return address register to return to the point of origin ($31) 20

Instruction Support for Functions (1/4) C M I P S... 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 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. 21

Instruction Support for Functions (2/4) C M I P S... 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 # new instr. jump register 22

Instruction Support for Functions (3/4) 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 instr. jump register 23

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! 24

MIPS Function Call Instructions Invoke function: jump and link instruction (jal) (really should be laj link and jump ) link means form an address or link that points to calling site to allow function to return to proper address Jumps to address and simultaneously saves the address of the following instruction in register $ra ($31) jal FunctionLabel Return from function: jump register instruction (jr) Unconditional jump to address specified in register jr $ra 25

Notes on Functions Calling program (caller) puts parameters into registers $a0-$a3 and uses jal X to invoke (callee) at address labeled X Must have register in computer with address of currently executing instruction Instead of Instruction Address Register (better name), historically called Program Counter (PC) It s a program s counter; it doesn t count programs! What value does jal X place into $ra????? jr $ra puts address inside $ra back into PC 26

Where Are Old Register Values Saved to Restore Them After Function Call? Need a place to save old values before call function, restore them when return, and delete Ideal is 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 register to point to it $sp is the stack pointer in MIPS ($29) Convention is grow from high to low addresses Push decrements $sp, Pop increments $sp 27

Example int Leaf (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 Assume need one temporary register $t0 28

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

MIPS Code for Leaf() Leaf: 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 30

What If a Function Calls a Function? Recursive Function Calls? Would clobber values in $a0 to $a3 and $ra What is the solution? 31

Nested Procedures (1/2) 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 32

Nested Procedures (2/2) 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., 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 33

Optimized Function Convention To reduce expensive loads and stores from spilling and restoring registers, MIPS divides registers into two categories: 1. Preserved across function call Caller can rely on values being unchanged $sp, $gp, $fp, saved registers $s0- $s7 2. 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,$ra 34