Mark Redekopp, All rights reserved. EE 352 Unit 4. Assembly and the MARS Simulator Control Flow (Branch Instructions)

Similar documents
EE 109 Unit 10 Assembler Directives and Control Flow

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

EE 109 Unit 10 MIPS Instruction Set

EE 109 Unit 10 MIPS Instruction Set. MIPS Processor and Bus Interface. Instruction Set Architecture (ISA) MIPS INSTRUCTION OVERVIEW

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

Computer Organization MIPS ISA

Mips Code Examples Peter Rounce

MIPS Instruction Format

Mark Redekopp, All rights reserved. EE 357 Unit 5. Assembler Directives and Programming

SPIM Instruction Set

Examples of branch instructions

Flow of Control -- Conditional branch instructions

MIPS Assembly Language Programming

MIPS Instruction Set Architecture (2)

Introduction to Digital Logic

Assembler Directives and Programming

MIPS Assembly Language Programming

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

CSc 256 Midterm (green) Fall 2018

Course Administration

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

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Computer Architecture

MIPS Reference Guide

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

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

Instructions: Language of the Computer

The MIPS Instruction Set Architecture

CS 4200/5200 Computer Architecture I

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

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

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

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

We will study the MIPS assembly language as an exemplar of the concept.

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

EE 352 Homework 1 Redekopp Name:

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

CENG3420 Lecture 03 Review

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

Control Instructions

Grading: 3 pts each part. If answer is correct but uses more instructions, 1 pt off. Wrong answer 3pts off.

Chapter 2A Instructions: Language of the Computer

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

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

Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field

MIPS Memory Access Instructions

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

MIPS (SPIM) Assembler Syntax

COMPUTER ORGANIZATION AND DESIGN

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

Topic Notes: MIPS Instruction Set Architecture

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

COMP MIPS instructions 2 Feb. 8, f = g + h i;

MIPS Assembly Language. Today s Lecture

Machine Instructions - II. Hwansoo Han

MIPS Functions and the Runtime Stack

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

Compiling Techniques

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

Thomas Polzer Institut für Technische Informatik

CS 61c: Great Ideas in Computer Architecture

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

CSc 256 Midterm 2 Fall 2011

Computer Architecture. The Language of the Machine

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

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

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

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

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

CS3350B Computer Architecture MIPS Instruction Representation

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

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

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

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

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

CSc 256 Midterm 2 Spring 2012

MIPS Assembly Language

EE 361 University of Hawaii Fall

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

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

Reduced Instruction Set Computer (RISC)

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

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

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

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

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

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

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

CSc 256 Midterm 1 Fall 2011

MACHINE LANGUAGE. To work with the machine, we need a translator.

Programmable Machines

MIPS Coding Continued

Quick Review. lw $t0, 4($a0) Registers x Memory. $a0 is simply another name for register 4 $t0 is another name for register (green sheet)

Programmable Machines

Reduced Instruction Set Computer (RISC)

CENG3420 L03: Instruction Set Architecture

Transcription:

EE 352 Unit 4 Assembly and the MARS Simulator Control Flow (Branch Instructions)

Directives Pseudo-instructions ASSEMBLERS

Assembler Syntax In MARS and most assemblers each line of the assembly program may be one of three possible options Comment Instruction / Pseudo-instruction Assembler Directive

Comments In MARS an entire line can be marked as a comment by starting it with a pound (#) character: Example: # This line will be ignored by the assembler LW $2,8($3) ADDI $2,$2,1...

Instructions In MARS each instruction is written on a separate line and has the following syntax: (Label:) Instruc. Op. Operands Comment Example: START: ADD $2,$3,$4 # R[2]=R[3] + R[4] Notes: Label is optional and is a text identifier for the address where the instruction is placed in memory. (These are normally used to identify the target of a branch or jump instruction.) In MARS, a comment can be inserted after an instruction by using a # sign A label can be on a line by itself in which case it refers to the address of the first instruction listed after it

Labels and Instructions The optional label in front of an instruction evaluates to the address where the instruction starts in memory and can be used in other instructions.text START: LW $4,8($10) L1: ADDI $4,$4,-1 BNE $4,$0,L1 J START Assembly Source File LW ADDI BNE J 0x400000 = START 0x400004 = L1 0x400008 0x40000C Note: The BNE instruc. causes the program to branch (jump) to the instruction at the specified address if the two operands are Not Equal. The J(ump) instruction causes program execution to jump to the specified label (address). Mark Redekopp, All rights reserved Assembler finds what address each instruction starts at.text LW $4,8($10) ADDI $4,$4,-1 BNE $4,$0,0x400004 J 0x400000 and replaces the labels with their corresponding address

Assembler Directives Similar to pre-processor statements and global variable declarations in C/C++ Text and data segments Reserving & initializing global variables and constants Compiler and linker status Direct the assembler in how to assemble the actual instructions and how to initialize memory when the program is loaded

Text and Static Data Segments.text directive indicates the following instructions should be placed in the program area of memory.data directive indicates the following data declarations will be placed in the data memory segment I/O Space Stack Dynamic Data Segment Static Data Segment Text Segment Unused 0xFFFF_FFFC 0x8000_0000 0x7FFF_FFFC 0x1000_8000 0x1000_0000 0x0040_0000 0x0000_0000

Static Data Directives Fills memory with specified data when program is loaded Format: (Label:).type_id val_0,val_1,,val_n type_id = {.byte,.half,.word,.float,.double} Each value in the comma separated list will be stored using the indicated size Mark Redekopp, All rights reserved Example: myval:.word 1, 2, 0x0003 Each value 1, 2, 3 is stored as a word (i.e. 32-bits) Label myval evaluates to the start address of the first word (i.e. of the value 1)

More Static Data Directives Can be used to initialize ASCII strings Format: (Label:).ascii string (Label:).asciiz string.asciiz adds a null-termination character (0) at the end of the string while.ascii does not Example: string:.asciiz Hello world\n Each character stored as a byte (including \n = Line Feed and \0 = Null (0) character) Label myval evaluates to the start address of the first byte of the string

Reserving Memory Reserves space in memory but leaves the contents unchanged Format: (Label:).space num_bytes.data dat1:.word 0x12345678 array:.space 4 dat2:.word 0xFEDCBA98 Skipped 00 00 00 00 0x1000000C FE DC BA 98 00 00 00 00 0x10000008 = dat2 0x10000004 = array 12 34 56 78 0x10000000 = dat1

Alignment Directive Used to skip to the next, correctly-aligned address for the given data size Format:.align 0,1,2, or 3 0 = byte-, 1 = half-, 2 = word-, 3 = doublealignment.data dat1:.byte 1, 2, 3.align 1 dat2:.half 0x4567.align 2 dat3:.word 0x89ABCDEF Note: The number after.align is not how many bytes to skip, it indicates what type of data will come next and thus the size to be aligned Mark Redekopp, All rights reserved Skipped Skipped 00 00 00 00 0x1000000C 89 AB CD EF 00 00 45 67 0x10000008 = dat3 0x10000004 = dat2 00 03 02 01 0x10000000 = dat1

Linker Directives.globl label Allows the following label and data to be referenced from other compilation units (files).extern label size Defines an externally allocated (in another file) static data storage with size at address label main:.data.extern dat1 4.text.globl main la $1,dat1... dat1 is defined in another file. Main is visible to other files dat1:.data.word 0x12345678 file1.s file2.s

.data example Examples.data C1:.byte 0xFE,0x05 MSG:.asciiz SC\n DAT:.half 1,2.align 2 VAR:.word 0x12345678 Skipped because a word must begin on a 4-byte boundary 12 34 56 78 00 00 00 02 00 01 00 0A 43 53 05 FE 0x1001000C 0x10010008 0x10010004 0x10010000 C1 evaluates to 0x10001000 MSG evaluates to 0x10001002 (Note: \n = Line Feed char. = 0x0A) DAT evaluates to 0x10001006 VAR evaluates to 0x1000100C

C/C++ and Directives Directives are used to initialize or reserve space for global variables in C short int count = 7; char message[16]; int table[8] = {0,1,2,3,4,5,6,7}; void main() {... }.data count:.half 7 message:.space 16.align 2 table:.word 0,1,2,3,4,5,6,7.text.globl main main:... C/C++ style global declarations Assembly equivalent

Summary & Notes Assembler Directives: Tell the assembler how to build the program memory image Where instructions and data should be placed in memory when the program is loaded How to initialize certain global variables Recall, a compiler/assembler simply outputs a memory IMAGE of the program. It must then be loaded into memory by the OS to be executed. Key: Directives are NOT instructions! They are used by the assembler to create the memory image and then removed The MIPS processor never sees these directives!

Directives in the Software Flow PC Program Executing SLT BNE Mark Redekopp, All rights reserved High Level Language Description int n = 0xC259; void main(){ if (x > 0) x = x + y - z; a = b*x;.c/.cpp files Compiler the processor NEVER sees/executes these directives Loader / OS.data MOVE.W X,D0 n:.word CMPI.W 0xC259 #0,D0.text BLE SKIP SLT ADD $4,$2,$0 Y,D0 BNE SUB SKIP Z,D0 SKIP MUL SKIP: MUL Assembly (.asm/.s files) 1110 0010 0101 1001 0110 1011 0000 1100 0100 1101 0111 1111 1010 1100 0010 1011 0001 0110 0011 1000 Executable Binary Image Assembler Directives are used to create the object code (executable) image Assembler 1110 1110 0010 0010 0101 0101 1001 1001 0110 0110 1011 1011 0000 0000 1100 1100 0100 0100 1101 1101 0111 0111 1111 1111 1010 1010 1100 1100 0010 0010 1011 1011 0001 0110 0011 1000 0001 0110 0011 1000 Object/Machine Code (.o files) Linker

Assembly Process The assembly procedure of a file requires two passes over the code 1 st Pass: Build a symbol table Maps labels to corresponding addresses 2 nd Pass: Substitute corresponding values for labels and symbols and then translate to machine code

1 st Pass 2 nd Pass Assembler (MARS) Assembly Process Diagram.data count:.word -1.text Main: la $2,count.s Assembly Input File Host System (PC) Symbol Table count = 0x10010000 MAIN = 0x400000 Target System (MIPS) MIPS Proc. 400000 PC.data count:.word -1.text Main: la $2,0x10010000 Data Structure (Part of Assembler Program) FF FF FF FF 3C 02 10 01 0x10010000 0x00400000 @0x10010000:0xFFFFFFFF @0x400000:0x3c021001 Executable File

Pseudo-instructions Macros translated by the assembler to instructions actually supported by the HW Simplifies writing code in assembly Example LI (Load-immediate) pseudoinstruction translated by assembler to 2 instruction sequence (LUI & ORI)... li... $2, 0x12345678... lui $2, 0x1234 ori $2, $2, 0x5678... With pseudo-instruction After assembler

Pseudo-instructions Pseudo-instruction NOT Rd,Rs NEG Rd,Rs Actual Assembly NOR Rd,Rs,$0 SUB Rd,$0,Rs LI Rt, immed. # Load Immediate LUI Rt, {immediate[31:16], 16 b0} ORI Rt, {16 b0, immediate[15:0]} LA Rt, label # Load Address LUI Rt, {immediate[31:16], 16 b0} ORI Rt, {16 b0, immediate[15:0]} BLT Rs,Rt,Label SLT $1,Rs,Rt BNE $1,$0,Label Note: Pseudoinstructions are assembler-dependent. See MARS Help for more details.

Support for Pseudo-instructions Pseudo-instructions often expand to several instructions and there is a need for usage of a temporary register Assembler reserves R[1] ($1) In the assembler, $1 = $at (assembler temp.) You can use $1 but it will be overwritten when you use certain pseudo-instructions

Branch Instructions Loops and Conditionals CONTROL FLOW

Branch Instructions Operation: PC = PC + displacement Branches allow us to jump backward or forward in our code label ---- ---- ---- ---- branch ---- ---- branch ---- ---- ---- label ----

Branch Instructions Conditional Branches Branches only if a particular condition is true Fundamental Instrucs.: BEQ (if equal), BNE (not equal) Syntax: BNE/BEQ Rs, Rt, label Compares Rs, Rt and if EQ/NE, branch to label, else continue Unconditional Branches Always branches to a new location in the code Instruction: BEQ $0,$0,label Pseudo-instruction: B label Mark Redekopp, All rights reserved ----!= beq $2,$3,label ---- ---- label: ---- = label: ---- ---- ---- b label ----

Single-Operand Compare & Branches For <, >, etc. comparison, actual MIPS conditional branch instructions can only compare one operand with zero Syntax: BccZ Rt, label cc = {LT, LE, GT, GE} Branch Instruc. Branch if BLTZ $2,label $2 < 0 BLEZ $2,label $2 0 BGTZ $2,label $2 > 0 BGEZ $2,label $2 0

Two-Operand Compare & Branches Two-operand comparison is accomplished using the SLT/SLTI/SLTU (Set If Lessthan) instruction Syntax: SLT Rd,Rs,Rt or SLT Rd,Rs,imm If Rs < Rt then Rd = 1, else Rd = 0 Use appropriate BNE/BEQ instruction to infer relationship Branch if SLT BNE/BEQ $2 < $3 SLT $1,$2,$3 BNE $1,$0,label $2 $3 SLT $1,$3,$2 BEQ $1,$0,label $2 > $3 SLT $1,$3,$2 BNE $1,$0,label Mark Redekopp, All rights reserved $2 $3 SLT $1,$2,$3 BEQ $1,$0,label

Branch Pseudo-Instructions Pseudo-instruction BLT Rt, Rs, label BLE Rt, Rs, label BGT Rt, Rs, label BGE Rt, Rs, label BLTU Rt, Rs, label BLT Rt, imm, label Description Branch if less-than Branch if less-than or equal Branch if greater-than Branch if greater-than of equal Branch if less-than (unsigned) Branch if less-than immediate Note: Pseudoinstructions are assembler-dependent. See MARS Help for more details.

Comparison with SLT Performing comparison with the SLT instruction is really accomplished by subtracting A-B and examining the sign of the result if A-B = 0, then A=B if A-B = negative #, then A<B If A-B = positive # and not 0, then A>B Determining if the result is positive or negative requires knowing what system is being used signed or unsigned? if overflow occurred when overflow occurs the sign of the result is incorrect (i.e. p+p = n or n+n = p)

SLT/SLTU Operation Use SLT for signed operand and SLTU for unsigned operands An SLT instruction subtracts A-B and examine sign of the result and the overflow test to determine if it should set the result Tests to determine less-than (negative) condition < Signed: (Neg. & No OV) OR (Pos. & OV) < Unsigned: (Unsigned OV) For unsigned subtraction, overflow is defined when the result is negative (i.e. Cout = 0) thus we use that test

Branch Example 1 C Code if A > B (&A in $t0) A = A + B (&B in $t1) else A = 1 MIPS Assembly.text LW LW SLT BEQ ADD B ELSE: ADDI NEXT: SW ---- $t2,0($t0) $t3,0($t1) $1,$t3,$t2 $1,$0,ELSE $t2,$t2,$t3 NEXT $t2,$0,1 $t2,0($t0) Could use pseudo-inst. BLE $4,$5,ELSE This branch skips over the else portion. This is a pseudo-instruction and is translated to BEQ $0,$0,next

Branch Example 2 C Code for(i=0;i < 10;i++) ($t0=i) j = j + i; ($t1=j) MIPS Assembly.text ADDI LOOP: SLTI BEQ ADD ADD B NEXT: ---- $t0,$0,$0 $1,$t0,10 $1,$0,NEXT $t1,$t1,$t0 $t0,$t0,1 LOOP Branches if i is not less than 10 Loops back to the comparison check

Another Branch Example C Code M68000 Assembly int dat[10]; for(i=0;i < 10;i++) (D0=i) data[i] = 5; (D1=j).data dat:.space 40.text la addi addi LOOP: sw addi addi bnez NEXT: ---- $t0,dat $t1,$zero,10 $t2,$zero,5 $t2,0($t0) $t0,$t0,4 $t1,$t1,-1 $t1,$zero,loop

A Final Example C Code M68000 Assembly Mark Redekopp, All rights reserved char A[] = hello world ; char B[50]; // strcpy(b,a); i=0; while(a[i]!= 0){ B[i] = A[i]; i++; } B[i] = 0;.data A:.asciiz hello world B:.space 50.text la la LOOP: lb beq sb addi addi b NEXT: sb $t0,a $t1,b $t2,0($t0) $t2,$zero,next $t2,0($t1) $t0,$t0,1 $t1,$t1,1 LOOP $t2,0($t1)

Branch Machine Code Format Branch instructions use the I-Type Format 6-bits 5-bits 5-bits 16-bits opcode rs (src1) rt (src2) Signed displacement Operation: PC = PC + {disp.,00} Displacement notes Displacement is the value that should be added to the PC so that it now points to the desired branch location Processor appends two 0 s to end of disp. since all instructions are 4-byte words Essentially, displacement is in units of words Effective range of displacement is an 18-bit signed value = ±128KB address space (i.e. can t branch anywhere in memory but long branches are rare and there is a mechanism Mark Redekopp, All rights reserved to handle them)

Branch Displacement To calculate displacement you must know where instructions are stored in memory (relative to each other) Mark Redekopp, All rights reserved Don t worry, assembler finds displacement for you you just use the label.text ADDI $8,$0,$0 ADDI $7,$0,10 LOOP: SLTI $1,$8,10 BEQ $1,$0,NEXT ADD $9,$9,$8 ADD $8,$8,1 BEQ $0,$0,LOOP NEXT: ---- MIPS Assembly A A + 0x4 A + 0x8 A + 0xC A + 0x10 A + 0x14 A + 0x18 A + 0x1C ADDI ADDI SLTI BEQ ADD ADD BEQ ---- 1 word for each instruction

Calculating Displacements Disp. = [(Addr. of Target) (Addr. of Branch + 4)] / 4 Constant 4 is due to the fact that by the time the branch executes the PC will be pointing at the instruction after it (i.e. plus 4 bytes) Following slides will show displacement calculation for BEQ $1,$0,NEXT.text ADDI $8,$0,$0 ADDI $7,$0,10 LOOP: SLTI $1,$8,10 BEQ $1,$0,NEXT ADD $9,$9,$8 ADD $8,$8,1 BEQ $0,$0,LOOP NEXT: ---- A A + 0x4 A + 0x8 A + 0xC A + 0x10 A + 0x14 A + 0x18 A + 0x1C ADDI ADDI SLTI BEQ ADD ADD BEQ ---- 1 word for each instruction Mark Redekopp, All rights reserved MIPS Assembly

Calculating Displacements Disp. = [(Addr. of Target) (Addr. of Branch + 4)] / 4 Disp. = (A+0x1C) (A+0x0C+ 4) = 0x1C 0x10 = 0x0C / 4 = 0x03.text ADDI $8,$0,$0 ADDI $7,$0,10 LOOP: SLTI $1,$8,10 BEQ $1,$0,NEXT ADD $9,$9,$8 ADD $8,$8,1 BEQ $0,$0,LOOP NEXT: ---- A A + 0x4 A + 0x8 A + 0xC A + 0x10 A + 0x14 A + 0x18 A + 0x1C ADDI ADDI SLTI BEQ ADD ADD BEQ ---- 1 word for each instruction MIPS Assembly

Calculating Displacements If the BEQ does in fact branch, it will add the displacement ({0x03, 00} = 0x000C) to the PC (A+0x10) and thus point to the MOVE instruction (A+0x1C).text ADDI $8,$0,$0 ADDI $7,$0,10 LOOP: SLTI $1,$8,10 BEQ $1,$0,NEXT ADD $9,$9,$8 ADD $8,$8,1 BEQ $0,$0,LOOP NEXT: ---- MIPS Assembly PC (after fetching BEQ) PC (after adding displacement) A + 0x10 + 000C A + 0x1C A A + 0x4 A + 0x8 A + 0xC A + 0x10 A + 0x14 A + 0x18 A + 0x1C ADDI ADDI SLTI BEQ ADD ADD BEQ ---- BEQ $1,$0,0x03 opcode rs 000100 00001 rt 00000 immediate 0000 0000 0000 0011 Mark Redekopp, All rights reserved

Another Example Disp. = [(Addr. of Label) (Addr. of Branch + 4)] / 4 Disp. = (A+0x04) (A+0x14 + 4) = 0x04 0x18 = 0xFFEC / 4 = 0xFFFB.text ADDI $8,$0,$0 LOOP: SLTI $1,$8,10 BEQ $1,$0,NEXT ADD $9,$9,$8 ADD $8,$8,1 BEQ $0,$0,LOOP NEXT: ---- A A + 0x4 A + 0x8 A + 0xC A + 0x10 A + 0x14 A + 0x18 ADDI SLTI BEQ ADD ADD BEQ ---- BEQ $0,$0,0xFFFB opcode rs 000100 00000 rt 00000 immediate 1111 1111 1111 1011 Mark Redekopp, All rights reserved

Jump Instructions Jumps provide method of branching beyond range of 16-bit displacement Syntax: J label/address Operation: PC = address Address is appended with two 0 s just like branch displacement yielding a 28-bit address with upper 4-bits of PC unaffected New instruction format: J-Type Mark Redekopp, All rights reserved 6-bits opcode Old PC [31:28] 26-bits Jump address Sample Jump instruction Old PC PC before execution of Jump 4-bits 26-bits 2-bits Jump address New PC after execution of Jump 00

Jump Example Take 28 LSB s of target address, remove LSB s (which are 0 s) and store 26-bits in the jump instruction.text ADDI $8,$0,$0 SLTI $1,$8,10 BEQ $1,$0,SKIP J L1 SKIP: ADD $8,$8,1... L1: SUB PC before exec. of jump: E0400010 PC after exec. of jump: EC800004 1110 1100 1000 0000 0000 0000 0000 01 00 0xE0400000 0xE0400004 0xE0400008 0xE040000C 0xE0400010 0xE0400014 ADDI SLTI BEQ BEQ J ADD ---- 0xEC800004 SUB Mark Redekopp, All rights reserved opcode 0000 10 Target Address / 4 11 0010 0000 0000 0000 0000 0001 0xC8000004 / 4

Jump Register jr instruction can be used if a full 32-bit jump is needed or variable jump address is needed Syntax: JR rs Mark Redekopp, All rights reserved Operation: PC = R[s] R-Type machine code format Usage: Can load rs with an immediate address Can calculate rs for a variable jump (class member functions, switch statements, etc.)