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

Similar documents
Lecture 7: Procedures

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

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

CS61C : Machine Structures

ECE 15B Computer Organization Spring 2010

CS 61c: Great Ideas in Computer Architecture

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

CS61C : Machine Structures

UCB CS61C : Machine Structures

CS61C : Machine Structures

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

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

CS 110 Computer Architecture MIPS Instruction Formats

Reduced Instruction Set Computer (RISC)

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

MIPS Functions and Instruction Formats

Lecture 5: Procedure Calls

Reduced Instruction Set Computer (RISC)

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

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Lecture 7: Procedures and Program Execution Preview

Lecture 5: Procedure Calls

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

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

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

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

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

Computer Architecture. The Language of the Machine

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

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

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

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

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

ECE232: Hardware Organization and Design

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

Lec 10: Assembler. Announcements

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

CS61C : Machine Structures

CS61C : Machine Structures

We will study the MIPS assembly language as an exemplar of the concept.

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

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

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

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

MIPS Instruction Set

ECE260: Fundamentals of Computer Engineering

Lecture 6: Assembly Programs

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

Control Instructions

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

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

COMPUTER ORGANIZATION AND DESIGN

CS 61c: Great Ideas in Computer Architecture

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

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

UCB CS61C : Machine Structures

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

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

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

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

CENG3420 Lecture 03 Review

Thomas Polzer Institut für Technische Informatik

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

ECE331: Hardware Organization and Design

CSE Lecture In Class Example Handout

UCB CS61C : Machine Structures

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

UCB CS61C : Machine Structures

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

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

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

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

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

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

Chapter 2A Instructions: Language of the Computer

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

CS61C : Machine Structures

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

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

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

MIPS Instruction Set Architecture (2)

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

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

ú There are CONVENTIONS when calling procedures! Stack main ()! { a(0);! }! void a (int m)! frame" Garcia, Spring 2013 UCB printf() (y==?

Instructions: Assembly Language

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

CSE Lecture In Class Example Handout

ECE260: Fundamentals of Computer Engineering

Chapter 2. Instructions:

Computer Architecture. MIPS Instruction Set Architecture

Chapter 2. Instructions: Language of the Computer

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

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Transcription:

6/29/2011 61: Great deas in omputer Architecture (achine tructures) ore achine Language nstructors: Randy H. Katz David A. atterson http://inst.eecs.berkeley.edu/~cs61c/sp11 pring 2011 -- Lecture #6 1 achine nterpretation Levels of Representation/nterpretation High Level Language rogram (e.g., ) ompiler Assembly Language rogram (e.g., ) Assembler achine Language rogram () Hardware Architecture Description (e.g., block diagrams) Architecture mplementation 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 ircuit Description 6/29/2011 (ircuit chematic Diagrams) pring 2011 -- Lecture #6 2 trings: vs. Java : each char is 8-bit A. Deal with 1 byte at a time. Java: 16-bit UNODE (a much larger character vocabulary). Deal with 2 bytes at a time. 6/29/2011 pring 2011 -- Lecture #6 3 6/29/2011 pring 2011 -- Lecture #5 4 upport for haracters and trings Load a word, use andi to isolate byte lw $s0,0($s1) andi $s0,$s0,255 # Zero everything but last 8 bits R Design rinciple: ake the ommon ase Fast any programs use text: has load byte instruction (lb) lb $s0,0($s1) Also store byte instruction (sb) Loading, toring bytes What do with other 24 bits in the 32 bit register? lb: sign extends to fill upper 24 bits xxxx xxxx xxxx xxxx xxxx xxxx xzzz zzzz byte is copied to sign-extend loaded This bit Normally don t want to sign extend chars instruction that doesn t sign extend when loading bytes: load byte unsigned (lbu) 6/29/2011 pring 2011 -- Lecture #5 5 1

upport for haracters and trings also provides fast support for Unicode: load halfword instruction (lh) lh $s0,0($s1) There s also load halfword unsigned (lhu) store halfword instruction (sh) sh $s0,0($s1) igned vs. Unsigned Three Different eanings! terms igned/unsigned overloaded : Do/Don't sign extend (lb, lbu, lh, lhu) Do/Don't overflow (add, addi, sub, mult, div) (addu, addiu, subu, multu, divu) Do signed/unsigned compare (slt, slti/sltu, sltiu) 6/29/2011 pring 2011 -- Lecture #5 7 What Do We Know Now? Arithmetic add, addi, addu, addiu, sub, subu, and, andi, or, ori, sll, slr, slt, slti, sltiu! Data Transfer lw, sw, lb, lbu, sb, lh, lhu, sh! Branches and Jumps bne, beq, j 6/29/2011 pring 2011 -- Lecture #6 9 6/29/2011 pring 2011 -- Lecture #6 10 Administrivia HW2 osted. Remember, it s big! roject 1 posted by tomorrow, due 7/10. No homework that week. Lab 4 is up. lease vote on times for idterm and Final, and tell us any conflicts you have. urvey linked on iazza, emailed out. 6/29/2011 pring 2011 -- Lecture #4 11 6/29/2011 pring 2011 -- Lecture #6 12 2

void foo() { int i,j; i = factorial(10);... j = factorial(25); Functions in int factorial(int n) { if (n<1) return 1; return n*factorial(n-1); Functions have their own local storage Functions can be called from multiple places. We pass arguments to functions, and functions return values Functions can call other functions, even themselves! 6/29/2011 pring 2011 -- Lecture #6 13 ix Fundamental teps in alling a Function 1. ut parameters in a place where function can access them 2. Transfer control to function 3. Acquire (local) storage resources needed for function 4. erform desired task of the function 5. ut 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 function can be called from several points in a program 6/29/2011 pring 2011 -- Lecture #6 14 Registers for Function alls Registers way faster than memory, so use registers $a0 $a3: four argument registers to pass parameters $v0 $v1: two value registers to return values $ra: one return address register that saves where a function is called from. ncluding $s and $t registers, we now know 26 of 32 registers! nstructions for Function alls nvoke function: jump and link instruction (jal) link means storing the location of the calling site. Jumps to label and simultaneously saves the location of following instruction in register $ra jal rocedureaddress Return from function: jump register instruction (jr) Unconditional jump to address specified in register jr $ra 6/29/2011 pring 2011 -- Lecture #6 15 6/29/2011 pring 2011 -- Lecture #6 16 Function all Example (1/4) address (shown in decimal) 1000 1004 1008 1012 1016 2000 2004 n, all instructions are 4 bytes, and stored in memory just like data. o here we show the addresses of where the programs are stored. Function all Example (2/4) f we didn t have jal 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 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction 3

Function all Example (3/4) Function all Example (4/4) address (shown in decimal) 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b 1008 jal sum # $ra = 1012, goto sum 1012 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction 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 Notes on Functions What f a Function alls another Function? alling program (caller) puts parameters into registers $a0-$a3 and uses jal X to invoke X (callee) ust have register in computer with address of currently executing instruction nstead of nstruction Address Register (better name), historically called rogram ounter () t s a program s counter, it doesn t count programs! Doesn t count as one of the 32 we ve mentioned. jal puts (+4) into $ra (then jumps to label) jr $ra puts address inside $ra into Would overwrite $ra ight also need to reuse other registesrs What is the solution? ave $ra (and anything else that might be needed later) on the stack! $sp register contains pointer to current bottom of stack. 6/29/2011 pring 2011 -- Lecture #6 21 6/29/2011 pring 2011 -- Lecture #6 22 Address-> $sp stack pointer 0 Recall: emory Layout tack Heap tatic ode pace for saved procedure information Explicitly created space, i.e., malloc() Variables declared once per program; e.g., globals rogram Example: sumquare int sumquare(int x, int y) { return mult(x,x)+ y; all to mult will overwrite $ra, must save it. Need to reuse $a1 to pass second argument to mult, but need current value (y) later. ust save $a1. To save something to stack, move $sp down required amount and fill the space it s created. 6/29/2011 pring 2011 -- Lecture #6 24 4

Example: sumquare int sumquare(int x, int y) { return mult(x,x)+ y; sumquare: addi $sp,$sp,-8 # make space on stack sw $ra, 4($sp) # save ret addr push sw $a1, 0($sp) # save y add $a1,$a0,$zero # set 2 nd mult arg jal mult # call mult lw $a1, 0($sp) # restore y add $v0,$v0,$a1 # ret val = mult(x,x)+y lw $ra, 4($sp) # get ret addr pop addi $sp,$sp,8 # restore stack jr $ra mult:... rologue func_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp) save other regs if need be Body Basic tructure of a Function Epilogue (call other functions )... restore other regs if need be lw $ra, framesize-4($sp) addi $sp,$sp, framesize jr $ra ra stack Local Variables and Arrays Any local variables the compiler cannot assign to registers will be allocated as part of the stack frame. Locally declared arrays and structs are also allocated as part of the stack frame. tack manipulation is same as before: move stack pointer down an extra amount, use the space it created as storage. tack Before, During, After all 6/29/2011 pring 2011 -- Lecture #6 27 6/29/2011 pring 2011 -- Lecture #6 28 6/29/2011 pring 2011 -- Lecture #6 29 6/29/2011 pring 2011 -- Lecture #6 30 5

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 aved $16-$23 $s0-$s7 ore Temporary $24-$25 $t8-$t9 Used by Kernel $26-27 $k0-$k1 Global ointer $28 $gp tack ointer $29 $sp Frame ointer $30 $fp Return Address $31 $ra Already discussed Not yet (From green sheet) Use names for registers -- code is clearer! Register onventions (1/4) aller: the calling function allee: the function being called When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged. Register onventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call (jal) and which may be changed. Register onventions (2/4) saved $0: No hange. Always 0. $s0-$s7: Restore if you change. Very important, that s why they re called saved registers. f the callee changes these in any way, it must restore the original values before returning. $sp: Restore if you change. The stack pointer must point to the same place before and after the jal call, or else the caller won t be able to restore values from the stack. HNT -- All saved registers start with! Register onventions (3/4) volatile $ra: an hange. The jal call itself will change this register. aller needs to save on stack if nested call. $v0-$v1: an hange. These will contain the new returned values. $a0-$a3: an change. These are volatile argument registers. aller needs to save if they are needed after the call. $t0-$t9: an change. That s why they re called temporary: any procedure may change them at any time. aller needs to save if they ll need them afterwards. Register onventions (4/4) What do these conventions mean? f 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 use before garbling up their values Remember: caller/callee need to save only temporary/saved registers they are using, not all registers. Where is the tack in emory? convention tack starts in high memory and grows down Hexadecimal : 7fff fffc hex programs (text segment) in low end 0040 0000 hex static data segment (constants and other static variables) above text for static variables convention global pointer ($gp) points to static Heap above static for data structures that grow and shrink ; grows up to high addresses 6/29/2011 pring 2011 -- Lecture #6 36 6

emory Allocation And in onclusion, instructions and conventions for implementing functions. Jump and link (jal) invokes, jump register (jr $ra) returns Registers $a0-$a3 for arguments, $v0-$v1 for return values Register conventions determine responsibilities of aller and allee for preserving values of registers. tack for spilling registers, saving return address, local variables 6/29/2011 pring 2011 -- Lecture #6 37 6/29/2011 pring 2011 -- Lecture #6 38 7