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

Similar documents
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 61C: Great Ideas in Computer Architecture Func%ons and Numbers

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

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

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

ECE232: Hardware Organization and Design

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

CS 110 Computer Architecture MIPS Instruction Formats

MIPS Functions and Instruction Formats

Lecture 5: Procedure Calls

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. Lecture 6: More MIPS, MIPS Func.ons. Instructor: Sagar Karandikar

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

ECE331: Hardware Organization and Design

CS 61c: Great Ideas in Computer Architecture

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

Control Instructions

ECE260: Fundamentals of Computer Engineering

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

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

CENG3420 Lecture 03 Review

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

Lecture 6: Assembly Programs

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

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

Course Administration

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

Thomas Polzer Institut für Technische Informatik

CS 316: Procedure Calls/Pipelining

CSE Lecture In Class Example Handout

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

COE608: Computer Organization and Architecture

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

Lecture 5: Procedure Calls

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

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

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

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

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

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

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

Chapter 2A Instructions: Language of the Computer

Procedure Call and Return Procedure call

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

MIPS function continued

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

MIPS Functions and the Runtime Stack

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

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.

ECE 30 Introduction to Computer Engineering

MIPS Assembly (Functions)

Architecture II. Computer Systems Laboratory Sungkyunkwan University

COMPUTER ORGANIZATION AND DESIGN

EE 361 University of Hawaii Fall

Functions in MIPS. Functions in MIPS 1

Lecture 7: Procedures and Program Execution Preview

CS61C : Machine Structures

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

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

CS 110 Computer Architecture MIPS Instruction Formats

MIPS Assembly (FuncDons)

Lec 10: Assembler. Announcements

CSE Lecture In Class Example Handout

Machine Instructions - II. Hwansoo Han

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

Lecture 7: Procedures

Stacks and Procedures

CA Compiler Construction

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

Instructions: Assembly Language

CS3350B Computer Architecture MIPS Procedures and Compilation

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

More C functions and Big Picture [MIPSc Notes]

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

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

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

Lab 4 : MIPS Function Calls

Instruction Set Architectures (4)

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

CS153: Compilers Lecture 8: Compiling Calls

MIPS Instruction Set Architecture (2)

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

CS64 Week 5 Lecture 1. Kyle Dewey

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Chapter 2. Instructions: Language of the Computer

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

CENG3420 L03: Instruction Set Architecture

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

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

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

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

CS61C : Machine Structures

MIPS Procedure Calls. Lecture 6 CS301

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

Computer Organization and Design

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Today. Putting it all together

Transcription:

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language Instructors: Randy H. Katz David A. PaGerson hgp://inst.eecs.berkeley.edu/~cs61c/sp11 1 2 Machine Interpreta4on Levels of RepresentaRon/ InterpretaRon 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 instrucrons 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 2/4/11 (Circuit SchemaCc Diagrams) Spring 2011 - - Lecture #6 3 String Copy Example Administrivia FuncRons Technology Break Memory Heap Summary Agenda 4 String Copy Example Administrivia FuncRons Technology Break Memory Heap Summary Agenda 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 ) ; 5 6 1

# $t1 = &p (BA), q @ &p + 4 # $s1 = p # $s2 = q Loop: # $t2 = *p # *q = $t2 BA+4 BA Q P 7 # $s1 = p # $s2 = q Loop: # $t2 = *p # *q = $t2 BA+4 BA Q P 8 # $s2 = q Loop: # $t2 = *p # *q = $t2 BA+4 BA Q P 9 Loop: # $t2 = *p # *q = $t2 BA+4 BA Q P 10 Loop: lb $t2,0($s1) # $t2 = *p # *q = $t2 BA+4 BA Q P 11 Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 BA P 12 2

Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 addi $s1,$s1,1 BA P 13 Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 addi $s1,$s1,1 BA P addi $s2,$s2,1 14 Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 addi $s1,$s1,1 BA P addi $s2,$s2,1 beq $t2,$zero,exit 15 Assembler Pseudo- instrucrons See hgp://en.wikipedia.org/wiki/mips_architecture Load Address (asm temp regs): $at = Label Address la $at, LabelAddr Implemented as: lui $at, LabelAddr[31:16]; ori $at, $at, LabelAddr[15:0] 16 String Copy Example Administrivia FuncRons Technology Break Memory Heap Summary Agenda Administrivia This week in lab and homework: Lab #3 EC2 (TAs say it is more doable than last week) Note: labs graded on a scale of 0 to 2 HW #3 Posted Note: HWs graded on a scale from 0 to 3 Project #1 posted Note: intermediate checkpoint due Sunday 12 students have already checked it in! 2 students have already completed Project #1! CSUA announcement 17 18 3

CS61c In the News Intel Chip Bug: hgp://newsroom.intel.com/community/intel_newsroom/blog/2011/01/31/intel- idenrfies- chipset- design- error- implemenrng- soluron Spring 2011 - - Lecture #6 19 Agenda Spring 2011 - - Lecture #6 21 String Copy Example Administrivia FuncRons Technology Break Memory Heap Summary MIPS Registers Assembly Language ConvenRons Spring 2011 - - Lecture #6 22 Six Fundamental Steps in Calling a FuncRon $t0- $t9: 10 x temporaries (intermediates) $s0- $s7: 8 x saved temporaries (program variables) $at: 1 x assembler temporary (used by pseudo instrucrons) 19 registers what are the other 13 registers used for, by convenron? 1. Put parameters in a place where funcron can access them 2. Transfer control to funcron 3. Acquire (local) storage resources needed for funcron 4. Perform desired task of the funcron 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 funcron can be called from several points in a program Spring 2011 - - Lecture #6 23 Spring 2011 - - Lecture #6 24 4

MIPS FuncRon Call ConvenRons 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 to return to the point of origin (7 of 13, 6 let!) MIPS FuncRon Call InstrucRons Invoke funcron: jump and link instrucron (jal) link means form an address or link that points to calling site to allow funcron to return to proper address Jumps to address and simultaneously saves the address of following instrucron in register $ra jal ProcedureAddress Return from funcron: jump register instrucron (jr) UncondiRonal jump to address specified in register jr $ra 25 26 Notes on FuncRons 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 execurng instrucron Instead of InstrucRon Address Register (beger 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????? 27 Notes on FuncRons 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 execurng instrucron Instead of InstrucRon Address Register (beger 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? Next PC PC + 4 28 Where Save Old Registers Values to Restore Them Ater FuncRon Call Need a place to place old values before call funcron, 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 ConvenRon is grow from high to low addresses Push decrements $sp, Pop increments $sp (8 of 13, 5 let!) 29 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 30 5

Stack Before, During, Ater FuncRon Need to save old values of $s0 and $t0 Contents of $t0 Contents of $s0 31 # adjust stack for 2 int items # save $t0 for use aterwards # save $s0 for use aterwards # f = g + h # t0 = i + j # restore $s0 for caller # restore $t0 for caller # delete 2 items from stack 32 # save $t0 for use aterwards # save $s0 for use aterwards # f = g + h # $t0 = i + j 33 # save $s0 for use aterwards # f = g + h # $t0 = i + j 34 # f = g + h # $t0 = i + j 35 # $t0 = i + j 36 6

add $t0,$a2,$a3 # $t0 = i + j 37 add $t0,$a2,$a3 # $t0 = i + j sub $v0,$s0,$t0 38 add $t0,$a2,$a3 # t0 = i + j sub $v0,$s0,$t0 lw $s0, 0($sp) 39 add $t0,$a2,$a3 # $t0 = i + j sub $v0,$s0,$t0 lw $s0, 0($sp) lw $t0, 4($sp) 40 add $t0,$a2,$a3 # $t0 = i + j sub $v0,$s0,$t0 lw $s0, 0($sp) lw $t0, 4($sp) addi $sp,$sp,8 41 add $t0,$a2,$a3 # $t0 = i + j sub $v0,$s0,$t0 lw $s0, 0($sp) lw $t0, 4($sp) addi $sp,$sp,8 jr $ra 42 7

What If a FuncRon Calls a FuncRon? Recursive FuncRon Calls? Would clobber values in $a0 to $a1 and $ra What is the soluron? Recursive FuncRon Factorial int fact (int n) { if (n < 1) return (1); else return (n * fact(n-1)); 43 44 Fact: Recursive FuncRon 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 45 String Copy Example Administrivia FuncRons Technology Break Memory Heap Summary Agenda 46 String Copy Example Administrivia FuncRons Technology Break Memory Heap Summary Agenda 47 48 8

AllocaRng Space on Stack Stack Before, During, Ater Call C has two storage classes: automarc and starc Automa=c variables are local to funcron and discarded when funcron exits. Sta=c variables exist across exits from and entries to procedures Use stack for automarc (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 (9 of 13, 4 let!) 49 50 Peer InstrucRon QuesRon 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); What will a.out do? Red. Print - 4 Orange. Print 4 Green. a.out will crash Yellow. None of the above 51 Peer InstrucRon Answer 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); What will a.out do? Red. Print - 4 Orange. Print 4 Green. a.out will crash Yellow. None of the above 52 OpRmized FuncRon ConvenRon To reduce expensive loads and stores from spilling and restoring registers, MIPS divides registers into two categories: 1. Preserved across funcron call Caller can rely on values being unchanged $ra, $sp, $gp, $fp, saved registers $s0- $s7 2. Not preserved across funcron call Caller cannot rely on values being unchanged Return value registers $v0,$v1, Argument registers $a0- $a3, temporary registers $t0- $t9 53 Register Numbering 54 9

Where is the Stack in Memory? MIPS Memory AllocaRon MIPS convenron 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 starc variables) above text for starc variables MIPS convenron global pointer ($gp) points to starc (10 of 13, 3 let!) Heap above starc for data structures that grow and shrink ; grows up to high addresses 55 56 And in Conclusion, C is funcron oriented; code reuse via funcrons 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 funcron calls, C local (automarc) variables 57 10