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

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

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

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

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

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

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

UCB CS61C : Machine Structures

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

Lecture 7: Procedures

CS61C : Machine Structures

CS 110 Computer Architecture MIPS Instruction Formats

MIPS Functions and Instruction Formats

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

ECE 15B Computer Organization Spring 2010

CS61C : Machine Structures

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

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

CS 61c: Great Ideas in Computer Architecture

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Reduced Instruction Set Computer (RISC)

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

Reduced Instruction Set Computer (RISC)

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

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

Lecture 7: Procedures and Program Execution Preview

CS 61C: Great Ideas in Computer Architecture. Machine Interpreta4on

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

CS61C : Machine Structures

CENG3420 Lecture 03 Review

COE608: Computer Organization and Architecture

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

Control Instructions

UCB CS61C : Machine Structures

CS61C : Machine Structures

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

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

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

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

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

UCB CS61C : Machine Structures

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

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

Lecture 5: Procedure Calls

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

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

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

Lecture 5: Procedure Calls

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

ECE331: Hardware Organization and Design

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Thomas Polzer Institut für Technische Informatik

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

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

ECE232: Hardware Organization and Design

More C functions and Big Picture [MIPSc Notes]

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

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

9/9/12. New- School Machine Structures (It s a bit more complicated!) CS 61C: Great Ideas in Computer Architecture IntroducMon to Machine Language

Computer Architecture

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

Chapter 2A Instructions: Language of the Computer

CSE Lecture In Class Example Handout

MIPS Functions and the Runtime Stack

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

Computer Architecture. The Language of the Machine

CS61CL : Machine Structures

CS61C : Machine Structures

ECE260: Fundamentals of Computer Engineering

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

Lecture 6: Assembly Programs

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

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to Machine Language

CENG3420 L03: Instruction Set Architecture

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

Final 9am instead of 9:30! Your grade will be ed by autograder! Jeremy Huddleston sb $t1 0($s2)!

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Instruction Set Design and Architecture

Course Administration

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

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

EE 361 University of Hawaii Fall

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.

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

ECE369. Chapter 2 ECE369

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

MIPS Instruction Set

CSE Lecture In Class Example Handout

COMPUTER ORGANIZATION AND DESIGN

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

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

CS153: Compilers Lecture 8: Compiling Calls

CMPE324 Computer Architecture Lecture 2

Instructions: Assembly Language

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

UCB CS61C : Machine Structures

Chapter 2: Instructions:

Transcription:

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro Instructors: Krste Asanovic & Vladimir Stojanovic h>p://inst.eecs.berkeley.edu/~cs61c/sp15 1

Machine Interpreta4on Levels of RepresentaIon/ InterpretaIon High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Assembler Machine Language Program (MIPS) Hardware Architecture DescripCon (e.g., block diagrams) Architecture Implementa4on 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 instrucions 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 DescripCon (Circuit SchemaCc Diagrams) 2

From last lecture Computer words and vocabulary are called instruc8ons and instruc8on set respecively MIPS is example RISC instrucion set used in CS61C Rigid format: 1 operaion, 2 source operands, 1 desinaion 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 arithmeic expressions, array access, in C to MIPS instrucions 3

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

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

How Program is Executed: InstrucIon Fetch Processor Control Datapath Program Counter Registers ArithmeIc & Logic Unit (ALU) Read InstrucIon Bits InstrucIon Address Memory Program Bytes Data The program counter (internal register inside processor) holds address of next instrucion to be executed 6

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

Types of Branches Branch change of control flow CondiConal Branch change control flow depending on outcome of comparison branch if equal (beq) or branch if not equal (bne) UncondiConal Branch always branch a MIPS instrucion for this: jump (j) 8

Example if Statement Assuming translaions 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 condiion 9

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

Inequalities in MIPS UnIl now, we ve only tested equaliies (== and!= in C). General programs need to test < and > as well. Introduce MIPS Inequality InstrucIon: 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. 11

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 # $t0 = 1 if g<h bne $t0,$zero,less # if $t0!=0 goto Less Register $zero always contains the value 0, so bne and beq ouen use it for comparison auer an slt instrucion sltu treats registers as unsigned 12

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)) 13

Clickers/Peer Instruction Label: sll $t1,$s3,2! addu $t1,$t1,$s5 lw $t1,0($t1)!!!! add $s1,$s1,$t1 addu $s3,$s3,$s4 bne $s3,$s2,label What is the code above? A: while loop B: do while loop C: for loop D: Not a loop E: Dunno 14

Clickers/Peer Instruction 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

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

MIPS FuncIon Call ConvenIons Registers faster than memory, so use them $a0 $a3: four argument registers to pass parameters $v0 $v1: two value registers to return values $ra: one return address register to return to the point of origin 17

InstrucIon Support for FuncIons (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.

InstrucIon Support for FuncIons (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 instruction"

InstrucIon Support for FuncIons (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 instruction"

InstrucIon Support for FuncIons (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: funcion calls very common. Don t have to know where code is in memory with jal!

MIPS FuncIon Call InstrucIons Invoke funcion: jump and link instrucion (jal) (really should be laj link and jump ) link means form an address or link that points to calling site to allow funcion to return to proper address Jumps to address and simultaneously saves the address of the following instrucion in register $ra jal FunctionLabel Return from funcion: jump register instrucion (jr) UncondiIonal jump to address specified in register jr $ra 22

Notes on FuncIons Calling program (caller) puts parameters into registers $a0-$a3 and uses jal X to invoke (callee) at address X Must have register in computer with address of currently execuing instrucion Instead of Instruc8on Address Register (be>er 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 23

Where Are Old Register Values Saved to Restore Them Auer FuncIon Call? Need a place to save old values before call funcion, 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 ConvenIon is grow from high to low addresses Push decrements $sp, Pop increments $sp 24

Example 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 Assume need one temporary register $t0 25

Stack Before, During, Auer FuncIon Need to save old values of $s0 and $t0 Contents of $t0 Contents of $s0 26

MIPS Code for leaf_example Leaf_example addi $sp,$sp,-8 # adjust stack for 2 items sw $t0, 4($sp) # save $t0 for use auerwards sw $s0, 0($sp) # save $s0 for use auerwards! 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 rouine 27

What If a FuncIon Calls a FuncIon? Recursive FuncIon Calls? Would clobber values in $a0 to $a3 and $ra What is the soluion? 28

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 overwri>en by the call to mult. Need to save sumsquare return address before call to mult.

Nested Procedures (2/2) In general, may need to save some other info in addiion to $ra. When a C program is run, there are 3 important memory areas allocated: StaIc: Variables declared once per program, cease to exist only auer execuion completes - e.g., C globals Heap: Variables declared dynamically via malloc Stack: Space to be used by procedure during execuion; this is where we can save register values

OpImized FuncIon ConvenIon To reduce expensive loads and stores from spilling and restoring registers, MIPS divides registers into two categories: 1. Preserved across funcion call Caller can rely on values being unchanged $ra, $sp, $gp, $fp, saved registers $s0- $s7 2. Not preserved across funcion call Caller cannot rely on values being unchanged Return value registers $v0,$v1, Argument registers $a0- $a3, temporary registers $t0- $t9 31

Clickers/Peer InstrucIon Which statement is FALSE? A: MIPS uses jal to invoke a funcion and jr to return from a funcion B: jal saves PC+1 in $ra! C: The callee can use temporary registers ($ti) without saving and restoring them D: The caller can rely on the saved registers ($si) without fear of callee changing them 32

Clickers/Peer InstrucIon Which statement is FALSE? A: MIPS uses jal to invoke a funcion and jr to return from a funcion B: jal saves PC+1 in $ra" C: The callee can use temporary registers ($ti) without saving and restoring them D: The caller can rely on the saved registers ($si) without fear of callee changing them 33

Administrivia Hopefully everyone turned- in HW0 HW1 due 11:59:59pm Sunday 2/8 34

In the News MIPS for hobbyists MIPS Creator CI20 dev board now available A lot like Raspberry Pi but with MIPS CPU Supports Linux and Android 1.2GHz 32- bit MIPS with integrated graphics http://liliputing.com/2015/01/mips-creator-ci20-dev-boardnow-available-for-65.html" 35

AllocaIng Space on Stack C has two storage classes: automaic and staic Automa8c variables are local to funcion and discarded when funcion exits Sta8c variables exist across exits from and entries to procedures Use stack for automaic (local) variables that don t fit in registers Procedure frame or ac8va8on 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 36

Stack Before, During, Auer Call 37

Using the Stack (1/2) 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 amount 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 (2/2) Hand-compile sumsquare: push pop mult:... " int sumsquare(int x, int y) { "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

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

Where is the Stack in Memory? MIPS convenion Stack starts in high memory and grows down Hexadecimal (base 16) : 7fff fffc hex MIPS programs (text segment) in low end 0040 0000 hex sta8c data segment (constants and other staic variables) above text for staic variables MIPS convenion global pointer ($gp) points to staic Heap above staic for data structures that grow and shrink ; grows up to high addresses 41

MIPS Memory AllocaIon 42

Register AllocaIon and Numbering 43

And in Conclusion FuncIons 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! InstrucIons we know so far ArithmeIc: add, addi, sub, addu, addiu, subu" Memory: lw, sw, lb, sb" Decision: beq, bne, slt, slti, sltu, sltiu" UncondiIonal Branches (Jumps): j, jal, jr" Registers we know so far All of them! $a0- $a3 for funcion arguments, $v0- $v1 for return values $sp, stack pointer, $fp frame pointer, $ra return address

Bonus Slides 45

Recursive FuncIon Factorial int fact (int n) { if (n < 1) return (1); else return (n * fact(n-1)); } 46

Recursive FuncIon Factorial Fact: L1: # adjust stack for 2 items # Else part (n >= 1) addi $sp,$sp,-8 # arg. gets (n 1) # save return address addi $a0,$a0,-1 sw $ra, 4($sp) # call fact with (n 1) # save argument n jal fact sw $a0, 0($sp) # return from jal: restore n # test for n < 1 lw $a0, 0($sp) slti $t0,$a0,1 # restore return address # if n >= 1, go to L1 lw $ra, 4($sp) beq $t0,$zero,l1 # adjust sp to pop 2 items # Then part (n==1) return 1 addi $sp, $sp,8 addi $v0,$zero,1 # return n * fact (n 1) # pop 2 items off stack mul $v0,$a0,$v0 addi $sp,$sp,8 # return to the caller # return to caller jr $ra jr $ra mul is a pseudo instruc8on 47