Cs 61C L6 Instruction, Proc II.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Functions, Part II Lecture 6 February 5, 1999 Dave Patterson.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Lecture 5: MIPS Instruction Set
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 9: MIPS Instruction Set
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
COMP3221 lec18-pointers.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I Internet2, a non-profit advanced.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
CWRU EECS 3221 Language of the Machine EECS 322 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CWRU EECS 3141 Language of the Machine EECS 314 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Cs 61C L4 Functions.1 Patterson Spring 99 ©UCB CS61C Functions, Procedures in C/Assembly Language Lecture 4 January 29, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I Apple took a bold step recently.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Cs 61C L5 Machine Lang.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Machine Language Lecture 5 February 3, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
CDA 3101 Spring 2016 Introduction to Computer Organization
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
April 2006 Saeid Nooshabadi
Computer Architecture
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

cs 61C L6 Instruction, Proc II.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Functions, Part II Lecture 6 February 5, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html

cs 61C L6 Instruction, Proc II.2 Patterson Spring 99 ©UCB Review 1/3 °MIPS assembly language instructions mapped to numbers in 3 formats Op field determines format °Operands Registers: $0 to $31 mapped onto $zero ; $at; $v0, $v1, $a0.., $s0.., $t0.., $gp, $sp, $fp, $ra Memory : Memory[0], Memory[4], Memory[8],,..., Memory[ ] -Index is the address of the word 6 bits5 bits 6 bits oprsrtrdfunctshamt oprsrtimmediate opaddress R I J

cs 61C L6 Instruction, Proc II.3 Patterson Spring 99 ©UCB Review 2/3 °Machine Language: what HW understands Assembly Language: can extend machine language to simplify for programmer Can encode instructions as numbers °Big Idea: Stored Program Concept From C to binary, and vice versa Everything has an address Pointer in C is HLL version of address Binary SW distribution  Binary compatibility  Instruction set “lock in”

cs 61C L6 Instruction, Proc II.4 Patterson Spring 99 ©UCB Instructions/Formats/“opcodes”, Regs Instr.Formatopfunct add Register032 sub Register 034 slt Register 042 jr Register 08 lw Immediate35 sw Immediate 43 addi Immediate 8 beq Immediate 4 bne Immediate 5 slti Immediate 10 jJump2 jalJump3 °Name Number $zero$0 $at$1 $v0,$v1$2-$3 $a0-$a3$4-$7 $t0-$t7$8-$15 $s0-$s7 $16-$22 $t8-$t9$23-$24 $k0,$k1$26,$27 $gp$28 $sp$29 $fp($t10)$30 $ra $31

cs 61C L6 Instruction, Proc II.5 Patterson Spring 99 ©UCB Overview °Decoding Instructions from numbers °Why MIPS? °C Function Memory Allocation review °Administrivia,“Cool Technology” °4 Versions of C/Asm. sumarray function °Pointers and memory allocation in C °Conclusion

cs 61C L6 Instruction, Proc II.6 Patterson Spring 99 ©UCB Decoding example °Binary  Decimal  Assembly  C? °Start at program at address 4,194,304 (2 22 ) °What are instruction formats of these 7 instructions?

cs 61C L6 Instruction, Proc II.7 Patterson Spring 99 ©UCB Decoding example: Binary=>Decimal °Binary  Decimal  Assembly  C? R R I R I R I R R I R I R I

cs 61C L6 Instruction, Proc II.8 Patterson Spring 99 ©UCB Decoding example: Decimal  Assembly °Decimal  Assembly? (Slide 4) R R I R I R I add$2, $0, $ slt$9, $0, $ beq$9, $0, add$2, $2, $ addi$5, $5, slt$9, $0, $ bne$9, $0, -4

cs 61C L6 Instruction, Proc II.9 Patterson Spring 99 ©UCB Decoding example: Symbolic Assembly °Registers, see Slide add$2, $0, $ slt$9, $0, $ beq$9, $0, add$2, $2, $ addi$5, $5, slt$9, $0, $ bne$9, $0, -4 add$v0,$zero,$zero slt$t1,$zero,$a1 beq$t1,$zero, Exit Loop:add$v0,$v0,$a0 addi$a1,$a1, -1 slt$t1,$zero,$a1 bne$t1,$zero,Loop Exit:

cs 61C L6 Instruction, Proc II.10 Patterson Spring 99 ©UCB Decoding example: Assembly  C °Binary  Decimal  Assembly  C? add$v0,$zero,$zero slt$t1,$zero,$a1 beq$t1,$zero, Exit Loop:add$v0,$v0,$a0 addi$a1,$a1, -1 slt$t1,$zero,$a1 bne$t1,$zero,Loop Exit: ° Mapping product:v0, mcand:a1, mlier:a1; product = 0; while (0 < mlier) { product = product + mcand; mlier = mlier - 1; } C MIPSMIPS

cs 61C L6 Instruction, Proc II.11 Patterson Spring 99 ©UCB Why MIPS? °Example of modern design: RISC, or Reduced Instruction Set Computer Style of computer, e.g., sports car RISC developed at IBM, Berkeley, Stanford °RISCs much easier to understand v. x86 ° bit Embedded Processors 83MMotorola 680x0 50MMIPS 48MARM (Acorn RISC Machine) 26MHitachi SuperH (RISC) 12Mx86 (5% of market)

cs 61C L6 Instruction, Proc II.12 Patterson Spring 99 ©UCB Review: C memory allocation 0  Address Code Program Static Variables declared once per program Heap Explicitly created space, e.g., malloc(); C pointers Stack Space for saved procedure information $sp stack pointer global pointer $gp

cs 61C L6 Instruction, Proc II.13 Patterson Spring 99 ©UCB Review: Memory Allocation on Call C Procedure Call Frame °Pass arguments (4 regs) °Save caller-saved regs °jal °space on stack ($sp-n) °save $fp & set $fp 1st word of frame ($sp+n-4) °Save callee-saved regs °If may call functions, save arguments and $ra Saved Registers Argument 5 Argument 6... $fp low high Address stack grows Local Variables $sp

cs 61C L6 Instruction, Proc II.14 Patterson Spring 99 ©UCB Review: Memory Deallocation on Return °Move return value into $v0 °Restore callee-saved regs from the stack °If saved $ra, restore it °Restore $fp from stack °Pop stack ($sp+n) word of frame °jr $ra °Restore caller-saved regs... $fp low high Address stack grows $sp

cs 61C L6 Instruction, Proc II.15 Patterson Spring 99 ©UCB Argument Passing Options °2 choices “Call by Value”: pass a copy of the item to the function/procedure “Call by Reference”: pass a pointer to the item to the function/procedure °Single word variables passed by value °Passing an array? e.g., a[100] Pascal--call by value--copies 100 words of a[] onto the stack C--call by reference--passes a pointer (1 word) to the array a[] in a register

cs 61C L6 Instruction, Proc II.16 Patterson Spring 99 ©UCB Administrivia °Readings: 3.7,4.1,4.2,4.3 °3rd homework: Due Wed 2/10 7PM Exercises 3.7, 3.8, 3.10 °2nd project: MIPS Disassembler Due Wed. 2/17 7PM °Midterm, Final: 5-8PM (3/17, 5/12) Conflicts: °HKN (hkn.eecs.berkeley.edu) Free Tutoring M-F Cory, 345 Soda Course surveys, online and paper exams

cs 61C L6 Instruction, Proc II.17 Patterson Spring 99 ©UCB Cool Technology: DIMM-PC/486 ELAN410 66MHz (486) CPU, 16 MB of DRAM, 16 MB of flash; all standard PC interfaces, such as 2 serial ports, printer, floppy and a hard disk interface; Ethernet option, Redhat 5.2 Linix: 1.6” x 2.7” x 0.25”, 0.8 w standby, $419, 486 PC smaller than a credit card

cs 61C L6 Instruction, Proc II.18 Patterson Spring 99 ©UCB Arrays, Pointers, Functions in C °4 versions of array function that adds two arrays and puts sum in a third array ( sumarray ) Third array is passed to function Using a local array (on stack) for result and passing a pointer to it Third array is allocated on heap Third array is declared static °Purpose of example is to show interaction of C statements, pointers, and memory allocation

cs 61C L6 Instruction, Proc II.19 Patterson Spring 99 ©UCB Calling sumarray, Version 1 int x[100], y[100], z[100]; sumarray(x, y, z); °C calling convention means above the same as sumarray(&x[0], &y[0], &z[0]); °Really passing pointers to arrays addi $a0,$gp,0 # x[0] starts at $gp addi $a1,$gp,400 # y[0] above x[100] addi $a2,$gp,800 # z[0] above y[100] jal sumarray

cs 61C L6 Instruction, Proc II.20 Patterson Spring 99 ©UCB Version 1: Optimized Compiled Code void sumarray(int a[],int b[],int c[]) { int i; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; } addi$t0,$a0,400 # beyond end of a[] Loop:beq$a0,$t0,Exit lw$t1, 0($a0) # $t1=a[i] lw$t2, 0($a1) # $t2=b[i] add$t1,$t1,$t2 # $t1=a[i] + b[i] sw$t1, 0($a2)# c[i]=a[i] + b[i] addi$a0,$a0,4# $a0++ addi$a1,$a1,4# $a1++ addi$a2,$a2,4# $a2++ jLoop Exit:jr $ra

cs 61C L6 Instruction, Proc II.21 Patterson Spring 99 ©UCB Version 1: Before optimizing compilers void sumarray(int a[],int b[],int c[]) { int *p = &a[100]; while (a != p) { *c = *a + *b; a++; b++; c++;} } addi$t0,$a0,400 # beyond end of a[] Loop:beq$a0,$t0,Exit lw$t1, 0($a0) # $t1=a[i] lw$t2, 0($a1) # $t2=b[i] add$t1,$t1,$t2 # $t1=a[i] + b[i] sw$t1, 0($a2)# c[i]=a[i] + b[i] addi$a0,$a0,4# $a0++ addi$a1,$a1,4# $a1++ addi$a2,$a2,4# $a2++ jLoop Exit:jr $ra

cs 61C L6 Instruction, Proc II.22 Patterson Spring 99 ©UCB Version 2 to Fix Weakness of Version 1 °Would like recursion to work int sumarray(int a[],int b[]); /* adds 2 arrays and returns sum */ sumarray(x, sumarray(y,z)); °Cannot do this with Version 1 style solution: what about this int * sumarray(int a[],int b[]) { int i, c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; }

cs 61C L6 Instruction, Proc II.23 Patterson Spring 99 ©UCB Version 2: Revised Compiled Code for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c;} addi$t0,$a0,400 # beyond end of a[] addi $sp,$sp,-400# space for c addi $t3,$sp,0# ptr for c addi$v0,$t3,0# $v0 = &c[0] Loop:beq$a0,$t0,Exit lw$t1, 0($a0) # $t1=a[i] lw$t2, 0($a1) # $t2=b[i] add$t1,$t1,$t2 # $t1=a[i] + b[i] sw$t1, 0($t3)# c[i]=a[i] + b[i] addi$a0,$a0,4# $a0++ addi$a1,$a1,4# $a1++ addi$t3,$t3,4# $t3++ jLoop Exit:addi $sp,$sp, 400# pop stack jr $ra

cs 61C L6 Instruction, Proc II.24 Patterson Spring 99 ©UCB Weakness of Version 2 °Legal Syntax; What’s Wrong? °Will work until call another function that uses stack °Won’t be reused instantly (e.g, add a printf ) °Stack allocated + unrestricted pointer is problem c[100] $sp low high Address stack grows

cs 61C L6 Instruction, Proc II.25 Patterson Spring 99 ©UCB Version 3 to Fix Weakness of Version 2 °Solution: allocate c[] on heap int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } CodeStatic Heap Stack c[100] °Not reused unless freed Can lead to memory leaks Java, Scheme have garbage collectors to reclaim free space

cs 61C L6 Instruction, Proc II.26 Patterson Spring 99 ©UCB Version 3: Revised Compiled Code addi$t0,$a0,400 # beyond end of a[] addi $sp,$sp,-12 # space for regs sw$ra, 0($sp) # save $ra sw$a0, 4($sp) # save 1st arg. sw$a1, 8($sp) # save 2nd arg. addi$a0,$zero,400 # jalmalloc addi $t3,$v0,0# ptr for c lw$a0, 4($sp) # restore 1st arg. lw$a1, 8($sp) # restore 2nd arg. Loop:beq$a0,$t0,Exit... (loop as before on prior slide ) jLoop Exit:lw$ra, 0($sp) # restore $ra addi $sp,$sp, 12# pop stack jr $ra

cs 61C L6 Instruction, Proc II.27 Patterson Spring 99 ©UCB Version 4 : Alternative to Version 3 °Static declaration int * sumarray(int a[],int b[]) { int i; static int c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } Code Static Heap Stack c[100] °Compiler allocates once for function, space is reused Will be changed next time sumarray invoked Why describe? used in C libraries

cs 61C L6 Instruction, Proc II.28 Patterson Spring 99 ©UCB What about Structures? °Scalars passed by value °Arrays passed by reference (pointers) °Structures by value too °Can think of C passing everything by value, just that arrays are simply a notation for pointers and the pointer is passed by value

cs 61C L6 Instruction, Proc II.29 Patterson Spring 99 ©UCB “And in Conclusion …” 1/2 °MIPS assembly language instructions mapped to numbers in 3 formats Op field determines format °Binary  Decimal  Assembly  Symbolic Assembly  C Reverse Engineering or Disassembly Its hard to do, therefore people like shipping binary machine language more than assembly or C 6 bits5 bits 6 bits oprsrtrdfunctshamt oprsrtimmediate opaddress R I J

cs 61C L6 Instruction, Proc II.30 Patterson Spring 99 ©UCB “And in Conclusion …” 2/2 °Programming language model of memory allocation and pointers Allocate in stack vs. heap vs. global areas Arguments passed call by value vs. call by reference Pointer in C is HLL version of machine address °Next : character, strings, other numbers