DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.

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.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
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.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
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.
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.
MIPS Instruction Set Advantages
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.
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
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.
Computer Organization Rabie A. Ramadan Lecture 3.
CENG 311 Instruction Representation
EE472 – Spring 2007P. Chiang, with Slide Help from C. Kozyrakis (Stanford) ECE472 Computer Architecture Lecture #3—Oct. 2, 2007 Patrick Chiang TA: Kang-Min.
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
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
CS 230: Computer Organization and Assembly Language
MIPS Instruction Set Advantages
CS2100 Computer Organisation
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
Decision Making.
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
ECE232: Hardware Organization and Design
Instruction encoding The ISA defines Format = Encoding
Chapter 2 Instructions: Language of the Computer part 2
The University of Adelaide, School of Computer Science
/ Computer Architecture and Design
3.
MIPS Coding.
COMS 361 Computer Organization
MIPS Assembly.
Computer Architecture
CSE 410, Spring 2008 Computer Systems
9/27: Lecture Topics Memory Data transfer instructions
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations

Chapter 2 — Instructions: Language of the Computer — 2 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 §2.7 Instructions for Making Decisions

Chapter 2 — Instructions: Language of the Computer — 3 Compiling If Statements C code: if (i==j) f = g+h; else f = g-h;  f, g, h, i, j in $s0, $s1, $s2, $s3, $s4 Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: … Assembler calculates addresses

Chapter 2 — Instructions: Language of the Computer — 4 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 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: … #$t1=4*i #$t1=&save[i] #$t0=save[i] #check save[i]==k #i++

Chapter 2 — Instructions: Language of the Computer — 5 Basic Blocks A basic block is a sequence of instructions with  No embedded branches (except at end)  No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks

MIPS Control Flow Instructions MIPS conditional branch instructions  Ex:  Instruction Format (I format): bne $s0, $s1, L1 #go to L1 if $s0≠$s1 beq $s0, $s1, L1 #go to L1 if $s0=$s1 If (i==j) h=i+j bne $s0, $s1, L1 add $s3, $s0, $s1 L1: … 0x bit offset

Specifying Branch Destinations Use a register (like in lw and sw) added to the 16-bit offset  Which register? Instruction Address Register (the PC)  Its use is automatically implied by instruction  PC gets updated (PC+4) during the fetch cycle so that it holds the address of the next instruction  Limits the branch distance to to (word) instructions from the (instruction after the) branch instruction. But most branches are local anyway  to (word) => to (byte)

Chapter 2 — Instructions: Language of the Computer — 8 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

More Branch Instructions Can use slt, beq, bne and the fixed value of 0 in regitster $zero to create other conditions  Less than  Less than or equal to  Greater than  Greater than or equal to Such branches are included in the instruction set as pseudo-instructions. These are recognized and expanded) by the assembler  Its why the assembler needs a reserved register ($at) blt $s1, $s2, Label slt $at, $s1, $s2 #$at set to 1 if bne $at, $zero, Label #$s1<$s2 ble $s1, $s2, Label bgt $s1, $s2, Label bge $s1, $s2, Label

Chapter 2 — Instructions: Language of the Computer — 10 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

Chapter 2 — Instructions: Language of the Computer — 11 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

Bounds Check Shortcut Treating signed numbers as if they were unsigned gives a low cost way of checking if 0≤x<y (index out of bounds for arrays) The key is that negative integers in 2’s complement look like large numbers in unsigned notation. Thus, an unsigned comparison of x<y also checks if x is negative as well as if x is less than y. sltu $t0, $s1, $s2 #$t0 set to 1 if beq $t0, $zero, IOOB #go to IOOB if $t0=0 #$s1 > $s2 (max) #or $s1 <0 (min)