Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.

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

CENG 311 Decisions in C/Assembly Language
Lecture 5: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
Lecture 4. MIPS Instructions #3 Branch Instructions Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced Computer Architecture.
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.
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos.
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Lecture 5 Sept 14 Goals: Chapter 2 continued 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.
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
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 4. Miscellaneous Addressing Mode, Memory Map, Pointer, and ASCII Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced.
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.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.
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.
CENG 311 Instruction Representation
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.
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.
Lecture 4. MIPS Instructions #3 Branch Instructions Prof. Taeweon Suh Computer Science & Engineering Korea University COSE222, COMP212 Computer Architecture.
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
MIPS Assembly.
MIPS Instruction Set Advantages
CS2100 Computer Organisation
Instructions: Language of the Computer
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
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,
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
Pick up the handout on your way in!!
How to represent signed integers
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
Flow of Control -- Conditional branch instructions
3.
COMS 361 Computer Organization
Flow of Control -- Conditional branch instructions
Computer Architecture
CSE 410, Spring 2008 Computer Systems
Conditional Branching (beq)
Control Flow and Arrays
Presentation transcript:

Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education & Research

Korea Univ Why Branch? 2 “if” statement if (i == j) f = g + h; else f = f – i; “while” statement // determines the power // of x such that 2 x = 128 int pow = 1; int x = 0; while (pow != 128) { pow = pow * 2; x = x + 1; } “for” statement // add the numbers from 0 to 9 int sum = 0; int i; for (i=0; i!=10; i = i+1) { sum = sum + i; } A computer performs different tasks depending on condition  Example: In high-level language, if/else, case, while and for loops statements all conditionally execute code

Korea Univ Why Branch? An advantage of a computer over a calculator is its ability to make decisions  A computer performs different tasks depending on condition Example: In high-level language, if/else, case, while and for loops statements all conditionally execute code To sequentially execute instructions, the pc (program counter) increments by 4 after each instruction in MIPS since the size of each instruction is 4-byte branch instructions modify the pc to skip over sections of code or to go back to repeat previous code  There are 2 kinds of branch instructions Conditional branch instructions perform a test and branch only if the test is true Unconditional branch instructions called jumps always branch 3

Korea Univ Branch Instructions in MIPS Conditional branch instructions  beq (branch if equal)  bne (branch if not equal) Unconditional branch instructions ( jumps )  j (jump)  jal (jump and link)  jr (jump register) 4

Korea Univ MIPS Conditional Branch Instructions Instruction format (I format) beq (bne) rs, rt, label Examples: bne $s0, $s1, Lbl// go to Lbl if $s0  $s1 beq $s0, $s1, Lbl// go to Lbl if $s0=$s1 How is the branch destination address specified? 5 High-level code if (i==j) h = i + j; MIPS assembly code // $s0 = i, $s1 = j bne $s0, $s1, Lbl1 add $s3, $s0, $s1 Lbl1:... compile opcodersrtimmediate 41617?

Korea Univ Branch Destination Address beq and bne instructions are I-type, which has the 16-bit offset (immediate) Offset is relative to the PC (Its use is automatically implied by instruction)  PC gets updated to “PC+4” during the fetch cycle so that it holds the address of the next instruction – Will cover this in chapter 4  It limits the branch distance to a range of to instructions from the instruction after the branch instruction  As a result, destination = (PC + 4) + (imm << 2) 6 Add PC Immediate of the branch instruction offset sign-extend Branch destination address

Korea Univ bne Example 7 High-level code if (i == j) f = g + h; f = f – i; MIPS assembly code # $s0 = f, $s1 = g, $s2 = h # $s3 = i, $s4 = j bne $s3, $s4, L1 add $s0, $s1, $s2 L1: sub $s0, $s0, $s3 Notice that the assembly tests for the opposite case ( i != j ) than the test in the high-level code ( i == j ). compile

Korea Univ In Support of Branch There are 4 instructions ( slt, sltu, slti, sltiu) that help you set the conditions slt, slti for signed numbers sltu, sltiu for unsigned numbers Instruction format slt rd, rs, rt // Set on less than (R format) sltu rd, rs, rt // Set on less than unsigned (R format) slti rt, rs, imm // Set on less than immediate (I format) sltiu rt, rs, imm // Set on less than unsigned immediate (I format) Examples: slt $t0, $s0, $s1 # if $s0 < $s1 then # $t0 = 1else # $t0 = 0 sltiu $t0, $s0, 25# if $s0 < 25 then $t0= opcodersrtimmediate NameRegister Number $zero0 $at1 $v0 - $v12-3 $a0 - $a34-7 $t0 - $t78-15 $s0 - $s $t8 - $t $gp28 $sp29 $fp30 $ra31

Korea Univ Branch Pseudo Instructions MIPS compilers use slt, slti, beq, bne and the fixed value of 0 (always available by reading register $zero ) to create all relative conditions (equal, not equal, less than, less than or equal, greater than, greater than or equal).  less than blt $s1, $s2, Label slt $at, $s1, $s2# $at set to 1 if $s1 < $s2 bne $at, $zero, Label  less than or equal to ble $s1, $s2, Label  greater than bgt $s1, $s2, Label  great than or equal to bge $s1, $s2, Label Such branches are included in the instruction set as pseudo instructions  Pseudo instructions are not supported by machine (CPU) itself  But, it is recognized and expanded by the assembler  The assembler uses a reserved register ( $at ) when expanding the pseudo instructions ( blt, ble, bgt, bge )  That’s why the assembler needs a reserved register ( $at ) 9

Korea Univ 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)  Example: int my_array[100] // $t2 = 100 // $s1 has a index to the array and changes dynamically while executing the program // $s1 and $t2 contain signed numbers, but the following code treats them as unsigned numbers sltu $t0, $s1, $t2 # $t0 = 0 if # $s1 > $t2 (max) # or $s1 < 0 (min) beq $t0, $zero, IOOB# go to IOOB if $t0 = 0 The key is that negative integers in two’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 10

Korea Univ MIPS Unconditional Branch Instructions MIPS also has an unconditional branch instruction or jump instruction  j, jr, jal Instruction format (J format) j target // jump jal target // jump and link jr rs // jump register Example j LLL ……. LLL: 11 opcodejump target 2? destination = {PC[31:28], jump target, 2’b00}

Korea Univ Branching Far Away What if the branch destination is further away than can be captured in 16 bits of beq ? The assembler comes to the rescue – it inserts an unconditional jump to the branch target and inverts the condition beq$s0, $s1, L1 becomes bne$s0, $s1, L2 jL1 L2: 12

Korea Univ C-to-Machine Code - While Loops 13 High-level code // determines the power // of x such that 2 x = 128 int pow = 1; int x = 0; while (pow != 128) { pow = pow * 2; x = x + 1; } MIPS assembly code # $s0 = pow, $s1 = x addi $s0, $0, 1 add $s1, $0, $0 addi $t0, $0, 128 while: beq $s0, $t0, done sll $s0, $s0, 1 addi $s1, $s1, 1 j while done: Notice that the assembly tests for the opposite case ( pow == 128 ) than the test in the high-level code ( pow != 128 ). compile

Korea Univ C-to-Machine Code - For Loops 14 High-level code // add the numbers from 0 to 9 int sum = 0; int i; for (i=0; i!=10; i = i+1) { sum = sum + i; } MIPS assembly code # $s0 = i, $s1 = sum addi $s1, $0, 0 add $s0, $0, $0 addi $t0, $0, 10 for: beq $s0, $t0, done add $s1, $s1, $s0 addi $s0, $s0, 1 j for done: Notice that the assembly tests for the opposite case ( i == 128 ) than the test in the high-level code ( i != 10 ). compile

Korea Univ C-to-Machine Code - Less Than Comparisons 15 High-level code // add the powers of 2 from 1 // to 100 int sum = 0; int i; for (i=1; i < 101; i = i*2) { sum = sum + i; } MIPS assembly code # $s0 = i, $s1 = sum addi $s1, $0, 0 addi $s0, $0, 1 addi $t0, $0, 101 loop: slt $t1, $s0, $t0 beq $t1, $0, done add $s1, $s1, $s0 sll $s0, $s0, 1 j loop done: $t1 = 1 if i < 101 compile