The University of Adelaide, School of Computer Science

Slides:



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

Goal: Write Programs in Assembly
Lecture 5: MIPS Instruction Set
Branches Two branch instructions:
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.
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.
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
CS2100 Computer Organisation MIPS Part III: Instruction Formats (AY2014/2015) Semester 2.
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.
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.
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.
Lecture 4: MIPS Instruction Set
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.
9/29: Lecture Topics Conditional branch instructions
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.
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
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.
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.
Lecture 17: 10/31/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Computer Architecture & Operations I
Computer Architecture Instruction Set Architecture
MIPS Instruction Set Advantages
CS2100 Computer Organisation
MIPS Coding Continued.
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,
Lecturer PSOE Dan Garcia
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
Pick up the handout on your way in!!
Lecture 4: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
How to represent signed integers
MIPS Instruction Encoding
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
MIPS Instruction Encoding
Instruction encoding The ISA defines Format = Encoding
MIPS coding.
Chapter 2 Instructions: Language of the Computer part 2
Flow of Control -- Conditional branch instructions
3.
COMS 361 Computer Organization
Flow of Control -- Conditional branch instructions
MIPS Coding Continued.
Computer Architecture
CS/COE0447 Computer Organization & Assembly Language
Control Flow and Arrays
Presentation transcript:

The University of Adelaide, School of Computer Science 14 January 2019 Lecture 2.5 Instructions: Branch and Jump Instructions Chapter 2 — Instructions: Language of the Computer

Learning Objectives Understand that instruction label cannot be presented in binary representation directly Understand that the offset (in branch) and the address (in jump) have the unit of word Use the address of the next instruction as the base address Covert branch and jump instructions between assembly statements and binary representations Use slt instruction with beq/bne to implement more complicated branch conditions Chapter 2 — Instructions: Language of the Computer — 2

Coverage Textbook Chapter 2.7 Chapter 2 — Instructions: Language of the Computer — 3

Control Flow Instructions The University of Adelaide, School of Computer Science 14 January 2019 Control Flow Instructions 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; Instruction Format (I format) §2.7 Instructions for Making Decisions op rs rt Offset 6 bits 5 bits 16 bits Chapter 2 — Instructions: Language of the Computer — 4 Chapter 2 — Instructions: Language of the Computer

Compiling If Statements The University of Adelaide, School of Computer Science 14 January 2019 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 — 5 Chapter 2 — Instructions: Language of the Computer

Compiling If Statements The University of Adelaide, School of Computer Science 14 January 2019 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: beq $s3, $s4, If sub $s0, $s1, $s2 j Exit If: add $s0, $s1, $s2 Exit: … Assembler calculates addresses Chapter 2 — Instructions: Language of the Computer — 6 Chapter 2 — Instructions: Language of the Computer

Control Flow Instructions The University of Adelaide, School of Computer Science 14 January 2019 Control Flow Instructions Branch to a labeled instruction if a condition is true Otherwise, continue sequentially beq $rs, $rt, L1 bne $rs, $rt, L1 Instruction Format (I format) How to specify the branch destination address? §2.7 Instructions for Making Decisions op rs rt Offset 6 bits 5 bits 16 bits Chapter 2 — Instructions: Language of the Computer — 7 Chapter 2 — Instructions: Language of the Computer

Specifying Branch Destinations Use a base address (like in lw) added to the 16-bit offset Which register? Instruction Address Register (Program Counter) Its use is automatically implied by instructions PC gets updated (PC+4) during the fetch stage so that it holds the address of the next instruction Instruction address is always the multiple of 4. The unit of the offset is word Limit the branch distance to -215to+215-1 (word) instructions from the instruction after the branch instruction Chapter 2 — Instructions: Language of the Computer — 8

Calculate Branch Destination Address Target address = Base address + Offset × 4 Offset = (Target address – Base address) / 4 = Address difference / 4 Target address: the address of the labelled instruction Base address: the address of the instruction following the branch instruction Chapter 2 — Instructions: Language of the Computer — 9

Compiling Loop Statements The University of Adelaide, School of Computer Science 14 January 2019 Compiling Loop Statements C code: while (save[i] == k) i += 1; i in $s3, k in $s5, address of save[] in $s6 All items in save[] are 32-bit words 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: … Chapter 2 — Instructions: Language of the Computer — 10 Chapter 2 — Instructions: Language of the Computer

Branch Instruction Design The University of Adelaide, School of Computer Science 14 January 2019 Branch Instruction Design Why not blt, bge, etc? Hardware for <, ≥, … slower than =, ≠ Combining comparisons such as “<” with branch involves more work for a branch instruction, requiring a slower clock All instructions are penalized! beq and bne are the common and fast cases This is a good design compromise Chapter 2 — Instructions: Language of the Computer — 11 Chapter 2 — Instructions: Language of the Computer

In Support of Branch Instructions We have beq, bne, but what about other kinds of branches (e.g., branch-if-less-than)? Set on less than instruction: slt $rd, $rs, $rt if (rs < rt) rd = 1; else rd = 0; Instruction format: R format Alternate versions of slt sltu $rd, $rs, $rt slti $rt, $rs, Imm # SignExt sltiu $rt, $rs, Imm # SignExt Chapter 2 — Instructions: Language of the Computer — 12

The University of Adelaide, School of Computer Science 14 January 2019 Signed vs. Unsigned Signed comparison: slt, slti Unsigned comparison: sltu, sltui Example $s0 = 1111 1111 1111 1111 1111 1111 1111 1111 $s1 = 0000 0000 0000 0000 0000 0000 0000 0001 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 — 13 Chapter 2 — Instructions: Language of the Computer

Pseudo Branch Instructions Can use slt, beq, bne, and the register $zero to create other conditions less than blt $s1, $s2, Label less than or equal to ble $s1, $s2, Label greater than bgt $s1, $s2, Label greater than or equal to bge $s1, $s2, Label Such branches are included in the instruction set as pseudo instructions - recognized and expanded by the assembler Assembler needs a reserved register ($at) slt $at, $s1, $s2 #$at set to 1 if bne $at, $zero, Label #$s1 < $s2 Chapter 2 — Instructions: Language of the Computer — 14

The University of Adelaide, School of Computer Science 14 January 2019 Unconditional Jump Jump (j) targets could be anywhere in text segment within 256 (i.e., 228) MB Encode full address in instruction Instruction format: J format op address 6 bits 26 bits (Pseudo)Direct jump addressing Target address = (PC+4)31…28:(address×4) address×4 = (Target address)27…0 PC is the address of the jump instruction Chapter 2 — Instructions: Language of the Computer — 15 Chapter 2 — Instructions: Language of the Computer

Form the target address PC+4 4 32 26 00 from the low order 26 bits of the jump instruction Where the 4 bits come from? The upper 4 bits of the address of the instruction following the Jump instruction Chapter 2 — Instructions: Language of the Computer — 16

Target Addressing Example The University of Adelaide, School of Computer Science 14 January 2019 Target Addressing Example Loop code from earlier example Assume Loop at location 80000 Loop: sll $t1, $s3, 2 80000 19 9 2 add $t1, $t1, $s6 80004 22 32 lw $t0, 0($t1) 80008 35 8 bne $t0, $s5, Exit 80012 5 21 addi $s3, $s3, 1 80016 1 j Loop 80020 Exit: … 80024 Chapter 2 — Instructions: Language of the Computer — 17 Chapter 2 — Instructions: Language of the Computer

Target Addressing Example The University of Adelaide, School of Computer Science 14 January 2019 Target Addressing Example Loop code from earlier example Assume Loop at location 80000 Loop: sll $t1, $s3, 2 80000 19 9 2 add $t1, $t1, $s6 80004 22 32 lw $t0, 0($t1) 80008 35 8 bne $t0, $s5, Exit 80012 5 21 addi $s3, $s3, 1 80016 1 j Loop 80020 20000 Exit: … 80024 Chapter 2 — Instructions: Language of the Computer — 18 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 14 January 2019 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites the code Example beq $s0,$s1, L1 L2: … ↓ bne $s0,$s1, L2 j L1 L2: … Chapter 2 — Instructions: Language of the Computer — 19 Chapter 2 — Instructions: Language of the Computer