ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.

Slides:



Advertisements
Similar presentations
CENG 311 Decisions in C/Assembly Language
Advertisements

Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
CS/COE0447 Computer Organization & Assembly Language
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
CS3350B Computer Architecture Winter 2015 Lecture 4
Chapter 2 Instructions: Language of the Computer
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
The University of Adelaide, School of Computer Science
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 4: Arithmetic / Data Transfer Instructions Partially adapted from Computer Organization.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
CS 61C L07 MIPS Memory (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #7: MIPS Memory & Decisions.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Lecture 4 Sept 9 Goals: Amdahl’s law Chapter 2 MIPS assembly language instruction formats translating c into MIPS - examples.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
MIPS Instructions Instructions: Language of the Machine
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
CDA 3101 Spring 2016 Introduction to Computer Organization
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Chapter 2 Instructions: Language of the Computer.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Architecture & Operations I
CS2100 Computer Organisation
Morgan Kaufmann Publishers
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
The University of Adelaide, School of Computer Science
Instructions - Type and Format
The University of Adelaide, School of Computer Science
Systems Architecture Lecture 5: MIPS Instruction Set
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
/ Computer Architecture and Design
Computer Instructions
Computer Architecture
3.
COMS 361 Computer Organization
CSC3050 – Computer Architecture
MIPS Instruction Set Architecture
MIPS Assembly.
Computer Architecture
Reduced Instruction Set Computer (RISC)
9/27: Lecture Topics Memory Data transfer instructions
Presentation transcript:

ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy, and classes taught by Ryan Kastner at UCSB

Last Lecture: Load and Store Instructions ECE 15B Spring 2010

CPU overview ECE 15B Spring 2010

Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply arithmetic operations – Load values from memory into registers – Store result from register to memory Memory is byte addressed – Each address identifies an 8-bit byte Words are aligned in memory – Address must be a multiple of 4 MIPS is Big Endian – Most-significant byte at least address of a word – c.f. Little Endian: least-significant byte at least address ECE 15B Spring 2010

Data Transfer: Memory to Register MIPS load Instruction Syntax lw register#, offset(register#) (1) (2) (3) (4) Where 1) operation name 2) register that will receive value 3) numerical offset in bytes 4) register containing pointer to memory lw – meaning Load Word 32 bits or one word are loaded at a time ECE 15B Spring 2010

Data Transfer: Register to Memory MIPS store Instruction Syntax sw register#, offset(register#) (1) (2) (3) (4) Where 1) operation name 2) register that will be written in memory 3) numerical offset in bytes 4) register containing pointer to memory sw – meaning Store Word 32 bits or one word are stored at a time ECE 15B Spring 2010

Memory Operand Example 1 C code: g = h + A[8]; – g in $s1, h in $s2, base address of A in $s3 Compiled MIPS code: – Index 8 requires offset of 32 4 bytes per word lw $t0, 32($s3) # load word add $s1, $s2, $t0 offset base register ECE 15B Spring 2010

Memory Operand Example 2 C code: A[12] = h + A[8]; – h in $s2, base address of A in $s3 Compiled MIPS code: – Index 8 requires offset of 32 lw $t0, 32($s3) # load word add $t0, $s2, $t0 sw $t0, 48($s3) # store word ECE 15B Spring 2010

Registers vs. Memory Registers are faster to access than memory Operating on memory data requires loads and stores – More instructions to be executed Compiler must use registers for variables as much as possible – Only spill to memory for less frequently used variables – Register optimization is important! ECE 15B Spring 2010

Byte/Halfword Operations MIPS byte/halfword load/store – String processing is a common case lb rt, offset(rs) lh rt, offset(rs) – Sign extend to 32 bits in rt lbu rt, offset(rs) lhu rt, offset(rs) – Zero extend to 32 bits in rt sb rt, offset(rs) sh rt, offset(rs) – Store just rightmost byte/halfword Why do we need them?  characters and multimedia data are expressed by less than 32 bits; having dedicated 8 and 16 bits load and store instructions results in faster operation ECE 15B Spring 2010

Control Flow Instructions ECE 15B Spring 2010

Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue sequentially beq rs, rt, L1 – if (rs == rt) branch to instruction labeled L1; bne rs, rt, L1 – if (rs != rt) branch to instruction labeled L1; j L1 – unconditional jump to instruction labeled L1 ECE 15B Spring 2010

Compiling If Statements C code: if (i==j) f = g+h; else f = g-h; – f, g, … in $s0, $s1, … Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: … Assembler calculates addresses ECE 15B Spring 2010

Compiling Loop Statements C code: while (save[i] == k) i += 1; – i in $s3, k in $s5, address of save in $s6 Compiled MIPS code: Loop: sll $t1, $s3, 2 #t1 = i*4 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: … ECE 15B Spring 2010

More Conditional Operations Set result to 1 if a condition is true – Otherwise, set to 0 slt rd, rs, rt – if (rs < rt) rd = 1; else rd = 0; slti rt, rs, constant – if (rs < constant) rt = 1; else rt = 0; Use in combination with beq, bne slt $t0, $s1, $s2 # if ($s1 < $s2) bne $t0, $zero, L # branch to L ECE 15B Spring 2010

Branch Instruction Design Why not blt, bge, etc? Hardware for <, ≥, … slower than =, ≠ – Combining with branch involves more work per instruction, requiring a slower clock – All instructions penalized! beq and bne are the common case This is a good design compromise ECE 15B Spring 2010

More Examples: For Loop C code: for(i = 1; i <= 10; i++) A[i] = A[i] + 1; Okay assembly code: assume i: $s1, base of A: $s2 addi $s1, $0, 1# i = 1 LOOP:slti $t0, $s1, 11# LOOP: if(i < 11) $t0 = 1 else $t0 = 0 beq $t0, $0, END# if ($t0==0) goto END; else { sll $t0, $s1, 2# $t0 = i *4; add $t0, $s2, $t0 # $t0 = $t0 + addr A; lw $t1, 0($t0)# $t1 = A[i]; addi $t1, $t1, 1# $t1 = $t1 + 1; sw $t1, 0($t0)# A[i] = $t1; addi $s1, $s1, 1# i = i + 1; j LOOP# goto LOOP } END:# END: ECE 15B Spring control flow instructions + 7 other instructions in the loop

More Examples: For Loop C code: for(i = 1; i <= 10; i++) A[i] = A[i] + 1; Better assembly code: assume i:$s1, base of A:$s2 addi $s1, $0, 1# i = 1; addi $t0, $s2, 4# $t0 = addr A + 4; (* pointer to A[1] *) addi $t2, $0, 11# $t2 = 11; LOOP:lw $t1, 0($t0)# do { $t1 = A[i]; addi $t1, $t1, 1# $t1 = $t1 + 1; sw $t1, 0($t0)# A[i] = $t1; addi $s1, $s1, 1# i = i + 1; addi $t0, $t0, 4# $t0 = $t0 + 4; } bne $s1, $t2, LOOP# while (i != 11); ECE 15B Spring control flow instructions + 5 other instructions in the loop

More Examples: For loop C code: for(i = 1; i <= 10; i++) A[i] = A[i] + 1; Even better assembly code: assume i: $s1, base of A: $s2 addi $t0, $s2, 4# $t0 = addr A + 4; (* pointer to a[1] *) addi $t2, $t0, 40# $t2 = $t0 + 40; (* pointer to a[11] *) LOOP:lw $t1, 0($t0)# do { $t1 = A[i]; addi $t1, $t1, 1# $t1 = $t1 + 1; sw $t1, 0($t0)# A[i] = $t1; addi $t0, $t0, 4# $t0 = $t0 + 4; } bne $t0, $t2, LOOP# while ($t2 != $t0); addi $s1, $0, 11# i = 11 (note that in this case the variable i is not used at all. The last line is just to make C code functionally equivalent to assembly code, since in C variable i will be equal to 11 after the completion of the loop) ECE 15B Spring control flow instructions + 4 other instructions in the loop

Signed vs. Unsigned Signed comparison: slt, slti Unsigned comparison: sltu, sltui Example – $s0 = – $s1 = – slt $t0, $s0, $s1 # signed –1 < +1  $t0 = 1 – sltu $t0, $s0, $s1 # unsigned +4,294,967,295 > +1  $t0 = 0 ECE 15B Spring 2010

CPU overview ECE 15B Spring 2010

Review Instructions so far: add, addi, sub mult, div, mfhi, mflo, lw, sw, lb, lbu, lh, lhu beq, bne, j slt, slti, sltu, sltui Registers so far C variables: $s0 - $s7 Temporary variables: $t0 - $t9 Zero: $zero ECE 15B Spring 2010