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

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

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

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Lecture 5: Procedure Calls

ECE232: Hardware Organization and Design

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

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

MIPS Functions and Instruction Formats

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

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 (Machine Structures) Introduc)on to Machine Language

CS 110 Computer Architecture MIPS Instruction Formats

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

ECE331: Hardware Organization and Design

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

Lecture 6: Assembly Programs

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

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

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

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

ECE260: Fundamentals of Computer Engineering

CS 61c: Great Ideas in Computer Architecture

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

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

Control Instructions

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

Thomas Polzer Institut für Technische Informatik

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

Course Administration

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

ECE260: Fundamentals of Computer Engineering

COMPUTER ORGANIZATION AND DESIGN

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

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

ECE260: Fundamentals of Computer Engineering

Chapter 2A Instructions: Language of the Computer

Lecture 5: Procedure Calls

Lecture 5. Announcements: Today: Finish up functions in MIPS

CSE Lecture In Class Example Handout

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

CENG3420 Lecture 03 Review

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CS 316: Procedure Calls/Pipelining

Functions in MIPS. Functions in MIPS 1

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Machine Instructions - II. Hwansoo Han

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

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

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

COE608: Computer Organization and Architecture

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

2012 David Black- Schaffer 1

CS 61C: Great Ideas in Computer Architecture. MIPS Instruc,on Representa,on II. Dan Garcia

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

CSE Lecture In Class Example Handout

Chapter 2. Instructions: Language of the Computer

MIPS Functions and the Runtime Stack

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

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

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

More C functions and Big Picture [MIPSc Notes]

Procedure Call Instructions

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

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

Computer Architecture

MIPS Instruction Set Architecture (2)

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

EE 361 University of Hawaii Fall

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Today. Putting it all together

MIPS function continued

CS153: Compilers Lecture 8: Compiling Calls

ECE 30 Introduction to Computer Engineering

CS3350B Computer Architecture MIPS Procedures and Compilation

Instructions: Assembly Language

Procedure Call and Return Procedure call

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MIPS Assembly (Functions)

Computer Organization and Design

Stacks and Procedures

CS61C : Machine Structures

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.

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

Lecture 7: Examples, MARS, Arithmetic

COMP2611: Computer Organization MIPS function and recursion

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

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

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

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

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

Lecture 7: Procedures

Lecture 7: Procedures and Program Execution Preview

Compilers and computer architecture: A realistic compiler to MIPS

All instructions have 3 operands Operand order is fixed (destination first)

MIPS Assembly (FuncDons)

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

CA Compiler Construction

Transcription:

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons Instructor: Krste Asanovic, Randy H. Katz hdp://inst.eecs.berkeley.edu/~cs61c/sp12 Fall 2012 - - Lecture #7 1 New- School Machine Structures (It s a bit more complicated!) Parallel Requests Assigned to computer e.g., Search Katz Parallel Threads Assigned to core e.g., Lookup, Ads So0ware Parallel Instruc\ons >1 instruc\on @ one \me e.g., 5 pipelined instruc\ons Parallel Data >1 data item @ one \me e.g., Add of 4 pairs of words Hardware descrip\ons All gates @ one \me Programming Languages Harness Parallelism & Achieve High Performance Hardware Warehouse Scale Computer Core Memory Input/Output Instruc\on Unit(s) Cache Memory Core Smart Phone Today s Logic Gates Lecture Fall 2012 - - Lecture #7 2 Computer (Cache) Core Func\onal Unit(s) A 0 +B 0 A 1 +B 1 A 2 +B 2 A 3 +B 3 Big Idea #1: Levels of Representa\on/ Interpreta\on High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Machine Interpreta4on 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 instruc\ons 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) Fall 2012 - - Lecture #7 3 Review Strings in C and MIPS Administrivia Func\ons And in Conclusion, Agenda Fall 2012 - - Lecture #7 4 Strings: C vs. Java Recall: a string is just a long sequence of characters (i.e., array of chars) C: 8- bit ASCII, define strings with end of string character NUL (0 in ASCII) Java: 16- bit Unicode, first entry gives length of string Strings Cal in ASCII in C; How many bytes? Using 1 integer per byte, what does it look like? Fall 2012 - - Lecture #7 5 Fall 2011 - - Lecture #7 6 1

Strings Cal in Unicode in Java; How many bytes? Using 1 integer per byte, what does it look like? (For La\n alphabet, 1 st byte is 0, 2 nd byte is ASCII) Fall 2011 - - Lecture #7 7 Pointers and Strings in C char *p; # p is a pointer to a character char x[] = Randy Katz ; # x points to a literal string p = x; # p points to the same place as x p = &x[0]; # same as p = x Cannot write x = Randy Katz ; Strings are not a primi.ve type in C (but character arrays are) Element/character at a.me processing possible, but whole string processing requires special rou.nes P X R A N D Y sp K A T Z 0! 0 1 2 3 4 5 6 7 8 9 10! Fall 2012 - - Lecture #7 8 Support for Characters and Strings Load a word, use andi to isolate byte lw $s0,0($s1) andi $s0,$s0,255 # Zero everything but last 8 bits RISC Design Principle: Make the Common Case Fast Many programs use text: MIPS has load byte instruc\on (lb) lb $s0,0($s1) Also store byte instruc\on (sb) Support for Characters and Strings Load a word, use andi to isolate half of word lw $s0,0($s1) andi $s0,$s0,65535 # Zero everything but last 16 bits RISC Design Principle #3: Make the Common Case Fast Many programs use text, MIPS has load halfword instruc\on (lh) lh $s0,0($s1) Also store halfword instruc\on (sh) Fall 2012 - - Lecture #7 9 Fall 2012 - - Lecture #7 10 Fast String Copy Code in C Copy x[] to y[] char *p, *q; p = &x[0]; /* p = x */ /* set p to address of 1 st char of x */ q = &y[0]; /* q = y also OK */ /* set q to address of 1 st char of y */ while((*q++ = *p++)!= \0 ) ; Fall 2012 - - Lecture #7 11 Fast String Copy in MIPS Assembly Get addresses of x and y into $s1, $s2 p and q are assigned to these registers # $t1 = &p (BA), q @ &p + 4 # $s1 = p # $s2 = q Loop: # $t2 = *p # *q = $t2 # p = p + 1 # q = q + 1 BA+4 BA # if *p == 0, go to Exit j Loop # go to Loop Exit: # N characters => N*6 + 3 instruc\ons Fall 2012 - - Lecture #7 Q P 12 2

Fast String Copy in MIPS Assembly Get addresses of x and y into $s1, $s2 p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 addi $s1,$s1,1 # p = p + 1 BA P addi $s2,$s2,1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop Exit: # N characters => N*6 + 3 instruc\ons Fall 2012 - - Lecture #7 13 Fast String Copy in MIPS Assembly Get addresses of x and y into $s1, $s2 p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 addi $s1,$s1,1 # p = p + 1 BA P addi $s2,$s2,1 # q = q + 1 beq $t2,$zero,exit # if *p == 0, go to Exit j Loop # go to Loop Exit: # N characters => N*6 + 3 instruc\ons Fall 2012 - - Lecture #7 14 Which statement is TRUE? char *p, *q; lw $t1, Base Address p = &x[0]; q = &y[0]; lw $s1,0($t1) while((*q++ = *p++)!= \0 ) ; lw $s2,4($t1) $t1 corresponds to p! Loop: lb $t2,0($s1) # sb $t2,0($s2) addi $s1,$s1,1 # $s1 corresponds to p! addi $s2,$s2,1 # beq $t2,$zero,exit # $s1 corresponds to q! j Loop # Exit: Review Strings in C and MIPS Administrivia Func\ons And in Conclusion, Agenda 15 Fall 2012 - - Lecture #7 16 Administrivia Map- Reduce Project #1 Two Parts, first part due Sunday Write- up posted last night (thanks Alan!) Lab #3 Hands on EC2, needed for Project #1, Part 2 HW #3 C prac\ce/numbers and strings Midterm is coming in < one month! Let us know special accommoda\on now CS 188 students, ask your instructors to return my emails! Fall 2012 - - Lecture #7 17 Review Strings in C and MIPS Administrivia Func\ons And in Conclusion, Agenda Fall 2012 - - Lecture #7 18 3

Six Fundamental Steps in Calling a Func\on 1. Put parameters in a place where func\on can access them 2. Transfer control to func\on 3. Acquire (local) storage resources needed for func\on 4. Perform desired task of the func\on 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 func\on can be called from several points in a program MIPS Func\on Call Conven\ons 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 (7 + $zero +$at of 32, 23 le{!) Fall 2012 - - Lecture #7 19 Fall 2012 - - Lecture #7 20 MIPS Registers Assembly Language Conven\ons $t0- $t9: 10 x temporaries (intermediates) $s0- $s7: 8 x saved temporaries (program variables) 18 registers 32 (18 + 9) = 5 le{ MIPS Func\on Call Instruc\ons Invoke func\on: jump and link instruc\on (jal) link means form an address or link that points to calling site to allow func\on to return to proper address Jumps to address and simultaneously saves the address of following instruc\on in register $ra jal ProcedureAddress Return from func\on: jump register instruc\on (jr) Uncondi\onal jump to address specified in register jr $ra Fall 2012 - - Lecture #7 21 Fall 2012 - - Lecture #7 22 Notes on Func\ons Calling program (caller) puts parameters into registers $a0-$a3 and uses jal X to invoke X (callee) Must have register in computer with address of currently execu\ng instruc\on Instead of Instruc\on Address Register (beder name), historically called Program Counter (PC) It s a program s counter; it doesn t count programs! jr $ra puts address inside $ra into PC What value does jal X place into $ra????? Fall 2012 - - Lecture #7 23 Where Are Old Register Values Saved to Restore Them A{er Func\on Call Need a place to save old values before call func\on, 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 Conven\on is grow from high to low addresses Push decrements $sp, Pop increments $sp (28 out of 32, 4 le{!) Fall 2012 - - Lecture #7 25 4

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 Stack Before, During, A{er Func\on Need to save old values of $s0 and $t0 Contents of $t0 Contents of $s0 Fall 2012 - - Lecture #7 26 Fall 2012 - - Lecture #7 27 MIPS Code for leaf_example leaf_example: # adjust stack for 2 int items # save $t0 for use a{erwards # save $s0 for use a{erwards # f = g + h # t0 = i + j # return value (g + h) (i + j) # restore $s0 for caller # restore $t0 for caller # delete 2 items from stack # jump back to calling rou\ne Fall 2012 - - Lecture #7 28 What will the prin output? Print - 4 Print 4 a.out will crash! static int *p; int leaf (int g, int h, int i, int j) { int f; p = &f; f = (g + h) (i + j); return f; int main(void) { int x; x = leaf(1,2,3,4); x = leaf(3,4,1,2); printf( %d\n,*p); 29 What If a Func\on Calls a Func\on? Recursive Func\on Calls? Would clobber values in $a0 to $a3 and $ra What is the solu\on? Alloca\ng Space on Stack C has two storage classes: automa\c and sta\c Automa.c variables are local to func\on and discarded when func\on exits Sta.c variables exist across exits from and entries to procedures Use stack for automa\c (local) variables that don t fit in registers Procedure frame or ac.va.on 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 (29 of 32, 3 le{!) Fall 2012 - - Lecture #7 30 Fall 2012 - - Lecture #7 31 5

Stack Before, During, A{er Call Recursive Func\on Factorial int fact (int n) { if (n < 1) return (1); else return (n * fact(n-1)); Fall 2012 - - Lecture #7 32 Fall 2012 - - Lecture #7 33 Fact: Recursive Func\on Factorial # adjust stack for 2 items addi $sp,$sp,-8 # save return address sw $ra, 4($sp) # save argument n sw $a0, 0($sp) # test for n < 1 slti $t0,$a0,1 # if n >= 1, go to L1 beq $t0,$zero,l1 # Then part (n==1) return 1 addi $v0,$zero,1 # pop 2 items off stack addi $sp,$sp,8 # return to caller jr $ra L1: # Else part (n >= 1) # arg. gets (n 1) addi $a0,$a0,-1 # call fact with (n 1) jal fact # return from jal: restore n lw $a0, 0($sp) # restore return address lw $ra, 4($sp) # adjust sp to pop 2 items addi $sp, $sp,8 # return n * fact (n 1) mul $v0,$a0,$v0 # return to the caller jr $ra mul is a pseudo instruc.on Fall 2012 - - Lecture #7 34 Op\mized Func\on Conven\on To reduce expensive loads and stores from spilling and restoring registers, MIPS divides registers into two categories: 1. Preserved across func\on call Caller can rely on values being unchanged $ra, $sp, $gp, $fp, saved registers $s0- $s7 2. Not preserved across func\on call Caller cannot rely on values being unchanged Return value registers $v0,$v1, Argument registers $a0- $a3, temporary registers $t0- $t9 Fall 2012 - - Lecture #7 35 Where is the Stack in Memory? MIPS Memory Alloca\on MIPS conven\on Stack starts in high memory and grows down Hexadecimal (base 16) : 7fff fffc hex MIPS programs (text segment) in low end 0040 0000 hex sta.c data segment (constants and other sta\c variables) above text for sta\c variables MIPS conven\on global pointer ($gp) points to sta\c (30 of 32, 2 le{! will see when talk about OS) Heap above sta\c for data structures that grow and shrink ; grows up to high addresses Fall 2012 - - Lecture #7 36 Fall 2012 - - Lecture #7 37 6

Register Alloca\on and Numbering Which statement is FALSE? MIPS uses jal to invoke a func\on and jr to return from a func\on jal saves PC+1 in %ra! The callee can use temporary registers (%ti) without saving and restoring them! Fall 2012 - - Lecture #7 38 39 And in Conclusion, C strings are char arrays, byte per character, null terminated Dis\nguish pointers and the memory they point to * for dereference, & for address C is func\on oriented; code reuse via func\ons Jump and link (jal) invokes, jump register (jr $ra) returns Registers $a0-$a3 for arguments, $v0-$v1 for return values Stack for spilling registers, nested func\on calls, C local (automa\c) variables Fall 2012 - - Lecture #7 40 7