Review of the Machine Cycle

Similar documents
The MIPS Instruction Set Architecture

Computer Architecture. The Language of the Machine

CS 4200/5200 Computer Architecture I

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

CS3350B Computer Architecture MIPS Introduction

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

MIPS Memory Access Instructions

Computer Architecture

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Lab 3 (Sections 300, 301 and 302) Prelab: MIPS-Control Instructions

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Chapter 2. Instruction Set Architecture (ISA)

Instructions: Language of the Computer

MIPS Instruction Reference

CS3350B Computer Architecture MIPS Instruction Representation

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Chapter 2: Instructions:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Lab 3 (All Sections) Prelab: MIPS-Control Instructions

CS3350B Computer Architecture

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

SPIM Instruction Set

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

F. Appendix 6 MIPS Instruction Reference

MIPS Reference Guide

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Chapter 2A Instructions: Language of the Computer

MIPS Instruction Format

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

MIPS Instruction Set

Project Part A: Single Cycle Processor

Computer Architecture. MIPS Instruction Set Architecture

Examples of branch instructions

Chapter 3. Instructions:

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

CS222: MIPS Instruction Set

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Mips Code Examples Peter Rounce

Flow of Control -- Conditional branch instructions

Programmable Machines

Programmable Machines

Course Administration

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

MIPS PROJECT INSTRUCTION SET and FORMAT

Reduced Instruction Set Computer (RISC)

CS/COE1541: Introduction to Computer Architecture

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

CENG3420 Lecture 03 Review

MIPS Assembly Language. Today s Lecture

Instruction Set Architecture. "Speaking with the computer"

EEC 581 Computer Architecture Lecture 1 Review MIPS

Processor. Han Wang CS3410, Spring 2012 Computer Science Cornell University. See P&H Chapter , 4.1 4

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Reduced Instruction Set Computer (RISC)

Chapter 4. The Processor. Computer Architecture and IC Design Lab

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Chapter 2. Instructions:

RTL Model of a Two-Stage MIPS Processor

EE 361 University of Hawaii Fall

Computer Organization MIPS ISA

CSSE 232 Computer Architecture I. I/O and Addressing

CS31001 COMPUTER ORGANIZATION AND ARCHITECTURE. Debdeep Mukhopadhyay, CSE, IIT Kharagpur. Instructions and Addressing

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

A Processor. Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University. See: P&H Chapter , 4.1-3

Final Project: MIPS-like Microprocessor

Topic Notes: MIPS Instruction Set Architecture

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

EE108B Lecture 3. MIPS Assembly Language II

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

Instruction Set Design and Architecture

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

bits 5..0 the sub-function of opcode 0, 32 for the add instruction

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

ECE 2300 Digital Logic & Computer Organization. More Single Cycle Microprocessor

Thomas Polzer Institut für Technische Informatik

ICS 233 COMPUTER ARCHITECTURE. MIPS Processor Design Multicycle Implementation

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Anne Bracy CS 3410 Computer Science Cornell University. See P&H Chapter: , , Appendix B

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Lecture 4: MIPS Instruction Set

The Program Counter. QtSPIM Register Display (Fixed-Point Registers) Erik Jonsson School of Engineering and Computer Science

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

Transcription:

MIPS Branch and Jump Instructions Cptr280 Dr Curtis Nelson Review of the Machine Cycle When a program is executing, its instructions are located in main memory. The address of an instruction is the address of the first (the lowest addressed) byte of the fourbyte instruction. Each machine cycle executes one machine instruction. At the t of the machine cycle, the PC (program counter) contains the address of an instruction to fetch from memory. The instruction is fetched into the processor and is prepared for execution. In the middle of the machine cycle the PC is incremented by four so that it points to the instruction that follows the one just fetched. Then the fetched instruction is executed and the cycle repeats. The machine cycle automatically executes instructions in sequence. When a jump instruction executes (in the last step of the machine cycle), it puts a new address into the PC. Now the fetch at the t of the next machine cycle fetches the instruction at that new address. Instead of executing the instruction that follows the jump instruction in memory, the processor "jumps" to an instruction somewhere else in memory. Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction 1

Review of the Machine Cycle However, it takes an extra machine cycle before the change in the PC takes effect. Before the PC changes, the instruction that follows the jump instruction in memory is fetched and executed. After that instruction executes, the next instruction to execute is the one that was jumped to. The instruction that follows a jump instruction in memory is said to be in the branch delay slot. The reason for this delay is that MIPS is pipelined. Normally instructions are executed in sequence. In order to gain speed, the processor cleverly fetches several sequential instructions and starts working on them all. When the machine cycle calls for one of these instructions to be executed, most of the work has already been done. These instructions are in an instruction pipe. This means that the instruction after a jump instruction has mostly been completed when the jump is executed. Rather than waste this effort, the instruction is allowed to finish. Only then is the PC changed by the jump instruction. The instruction that follows a jump instruction in memory (in the branch delay slot) is always executed. After it executes, the next instruction to execute is the one that was jumped to. Often the branch delay slot is filled with a n instruction. The QTSpim simulator allows you to turn the pipeline feature off, but this is not an tion with actual R2000 hardware. Altering the PC Shown below is a sequence of instructions. The "load" and "add" represent typical instructions. The "jump" instruction shows the address we wish to put into the PC. (The actual MIPS instruction for this involves details that we are skipping for the moment.) The last instruction, a n, fills the branch delay slot to give the PC time to change. Once started, the four instructions execute in an unending lo. Address Instruction PC just after this (details omitted) instruction has executed (at the bottom of the cycle) 00400000 00400000 load 00400004 00400004 add 00400008 00400008 jump 0x00400000 0040000C 0040000C n 00400000 -- effect of jump A lo structure is created with the jump instruction. The intent of the jump instruction is to put the address 0x00400000 into the PC. However, this effect is not seen until after the instruction in the branch delay slot has executed. 2

Review of MIPS Instruction Formats Instruction Categories Computational Load/Store Jump and Branch Floating Point Crocessor Memory Management Special Registers R0 - R31 3 Instruction Formats - all 32 bits wide rs rt rd sa funct rs rt immediate jump target R format I format J format Addresses in Jump Instructions PC-relative addressing is used for jump instructions: J 26 bit address The MIPS jump instruction (j)replaces the lower 28 bits of the PC with A00 where A is the 26 bit address; it never changes the upper 4 bits: Example : if PC = 1011X (where X = 28 bits), it is replaced with 1011A00 There are 16 (=2 4 ) partitions of the 2 32 size address space, each partition of size 256 MB (=2 28 ), such that, in each partition the upper 4 bits of the address is the same. If a program crosses an address partition, then a jump instruction that attempts to jump to a different partition has to be replaced by (jr)with a full 32-bit address first loaded into the jump register. 3

Unconditional Branch (Jump) MIPS unconditional branch instructions: j Label Example : if (i!=j) beq $s4, $s5, Lab1 h=i+j; add $s3, $s4, $s5 else j Lab2 h=i-j; Lab1: sub $s3, $s4, $s5 Lab2:... 000010 00000000000000000000011001 6 bits 26 bits 26 bit number Addresses in Branch Instructions: bne $t4,$t5,label Next instruction is at Label if $t4!= $t5 beq $t4,$t5,label Next instruction is at Label if $t4 = $t5 Format: rs rt 16 bit offset 16 bits is too small to reach a 2 32 address space. Solution: specify a register (as for lw and sw) and add it to offset: Use the Program Counter as the register. This type of addressing is called PC-relative addressing, based on: Principle of locality: most branches are to instructions near current instruction (e.g., los and if statements). 4

Addresses in Branch We can further extend the reach of branch instructions by observing that all MIPS instructions are a word (= 4 bytes), therefore we use word-relative addressing. MIPS branch destination address = (PC + 4) + (4 * offset) Because hardware typically increments PC early in execute cycle to point to next instruction So offset = (branch destination address PC 4)/4 But QTSpim does offset = (branch destination address PC)/4 if pipelining is not enabled so. Be careful, best to stick to branching to labels, rather than calculating the offset by hand. MIPS Compare and Branch Instructions Compare and Branch beq rs, rt, offset if R[rs] == R[rt] then PC-relative branch bne rs, rt, offset <> Compare to Zero and Branch blez rs, offset if R[rs] <= 0 then PC-relative branch bgtz rs, offset > blt rs, offset < bgez rs, offset >= bltzal rs, offset if R[rs] < 0 then branch and link (into R31) bgezal rs, offset >= Why would you guess that only compares with zero are supported and not general compares? 5

MIPS Compare and Jump Instructions Instruction Example Meaning set on less than slt $1,$2,$3 if ($2 < $3) $1=1; else $1=0 Compare less than; 2 s complement set less than imm. slti $1,$2,100 if ($2 < 100) $1=1; else $1=0 Compare < constant; 2 s complement set less than uns. sltu $1,$2,$3 if ($2 < $3) $1=1; else $1=0 Compare less than; unsigned numbers set l. t. imm. uns. sltiu $1,$2,100 if ($2 < 100) $1=1; else $1=0 Compare < constant; unsigned numbers jump j 10000 go to (PC[31,28], 40000) Jump to target address jump and link jal 10000 $31 = PC + 4; go to (PC[31,28], 40000) For procedure/subroutine call jump register jr $31 go to $31 For switch, procedure/subroutine return Signed vs. Unsigned Comparison R1= 0 00 0000 0000 0000 0001 2 = 1 10 R2= 0 00 0000 0000 0000 0010 2 = 2 10 R3= 1 11 1111 1111 1111 1111 2 = 2 32-1 or 1 10 After executing these instructions: slt r4,r2,r1 ; if (r2 < r1) r4=1; else r4=0 slt r5,r3,r1 ; if (r3 < r1) r5=1; else r5=0 sltu r6,r2,r1 ; if (r2 < r1) r6=1; else r6=0 sltu r7,r3,r1 ; if (r3 < r1) r7=1; else r7=0 What are values of registers r4 - r7? r4 = ; r5 = ; r6 = ; r7 = ; 6

MIPS Code for If-then-else Structure Conditional statements allow us to make decisions. Replace the C code for if (i == j), f = g + h; else f = g - h; by equivalent MIPS instructions. Assume variables f through j correspond to registers $s0 through $s4. Instruction Comment bne $s3, $s4, Else if (i!= j) goto Else add $s0, $s1, $s2 f = g + h j Exit go to Exit Else: sub $s0, $s1, $s2 f = g - h Exit: Pseudo-Instructions The pseudo-instruction blt $s0, $s1, Else is implemented as slt $at, $s0, $s1 bne $at, $zero, Else Check the course web page for other branch instructions and branching pseudo-instructions. Note that when some pseudo-instructions are expanded into actual MIPS instructions, $R1 is often used to store temporary values. 7

Example: BranchJump.asm # Nonsense program to show address calculations for branch and jump instructions.text.globl main # text section # call main by SPIM # Nonsense code - load in SPIM to see the address calculations main: j label beq $8, $9, label label: Conclusions Program flow is controlled by branch and jump instructions; Decisions are made by comparing the contents of two registers or the contents of one register with an immediate value; Jump instructions use word-relative addressing and are limited to 256Mbyte partitions; Branch instructions use offsets and are limited to a 16 bit address space. 8