Lecture 2 CS154B Review of MIPS ISA and Performance.

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

CEG3420 Lec2.1 ©UCB Fall 1997 ISA Review CEG3420 Computer Design Lecture 2.
1 Lecture 3: Instruction Set Architecture ISA types, register usage, memory addressing, endian and alignment, quantitative evaluation.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Assembly Language (cont)
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
1  1998 Morgan Kaufmann Publishers and UCB CEG3420 Computer Design Lecture 4 MIPS Instruction Set Philip Leong.
ECE 232 L3 InstructionSet.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 3.
EECC550 - Shaaban #1 Lec # 2 Winter Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of instructions.
Reduced Instruction Set Computer (RISC)
CS152 / Kubiatowicz Lec2.1 8/31/01©UCB Fall 2001 August 31, 2001 John Kubiatowicz (http.cs.berkeley.edu/~kubitron) lecture slides:
361 Lec4.1 ECE 361 Computer Architecture Lecture 4: MIPS Instruction Set Architecture.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
331 W02.1Spring 05 Announcements  HW1 is due on this Friday  Appendix A (on CD) is very helpful to HW1.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
ECE C61 Computer Architecture Lecture 3 – Instruction Set Architecture
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
EECC550 - Shaaban #1 Lec # 2 Winter Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of instructions.
EECC550 - Shaaban #1 Lec # 2 Winter (Chapter 2) Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of.
CPE442 Lec 3 ISA.1 Intro to Computer Architectures Instruction Set Design instruction set software hardware.
Overview of Today’s Lecture
Some material taken from Assembly Language for x86 Processors by Kip Irvine © Pearson Education, 2010 Slides revised 2/2/2014 by Patrick Kelley.
83 Assembly Language Readings: Chapter 2 ( , 2.8, 2.9, 2.13, 2.15), Appendix A.10 Assembly language Simple, regular instructions – building blocks.
CPE442 Lec 3 ISA.1 Intro to Computer Architectures Instruction Set Design instruction set software hardware.
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
CPE 442 Lec4.1 Intro Computer Architectures CpE 442 Computer Architecture and Engineering MIPS Instruction Set Architecture.
1  Modified from  1998 Morgan Kaufmann Publishers Chapter 2: Instructions: Language of the Machine citation and following credit line is included: 'Copyright.
Lecture 4: MIPS Subroutines and x86 Architecture Professor Mike Schulte Computer Architecture ECE 201.
EECC250 - Shaaban #1 lec #21 Winter Complex Instruction Set Computer (CISC) Emphasizes doing more with each instruction. Motivated by the high.
1 IKI20210 Pengantar Organisasi Komputer Kuliah Minggu ke-4b: Bahasa Rakitan MIPS I diadaptasikan dari materi kuliah CS61C/2000 & CS152/1997  2000/1997.
1 Computer Architecture COSC 3430 Lecture 3: Instructions.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
CS152 Fall 2002 Lec 2.1 Computer Organization Lecture 2 Review of MIPS ISA and Performance Modified From the Lectures of Randy H. Katz EECS UC Berkeley.
Lecture 11: 10/1/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Intro & ISA.1 1/14 Instruction Set Architecture (ISA) (Ch: , th ed) Instructions: Language of the Computer Dr. Khaled El-Ayat.
Csci 136 Computer Architecture II – Summary of MIPS ISA Xiuzhen Cheng
©UCB Fall CS/EE 362 Hardware Fundamentals Lecture 11 (Chapter 3: Hennessy and Patterson) Winter Quarter 1998 Chris Myers.
MIPS Instructions Instructions: Language of the Machine
Computer Organization and Design Jair Gonzalez Enero 2004 MIPS ISA.
MIPS Instructions Instructions: Language of the Machine
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
EEL5708/Bölöni Lec 3.1 Fall 2006 Sept 1, 2006 Lotzi Bölöni EEL 5708 High Performance Computer Architecture Lecture 3 Review: Instruction Sets.
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
Lecture 2: Instruction Set Architecture part 1 (Introduction) Mehran Rezaei.
EEL5708/Bölöni Lec 3.1 Fall 2004 Sept 1, 2004 Lotzi Bölöni Fall 2004 EEL 5708 High Performance Computer Architecture Lecture 3 Review: Instruction Sets.
Pirouz Bazargan SabetDecember 2003 Effective Implementation of a 32-bit RISC Processor Pirouz Bazargan Sabet University of Paris 6 - LIP6 - ASIM
Lecture 12: 10/3/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Instructor: Prof. Hany H Ammar, LCSEE, WVU
Instructions: Language of the Computer
Computer Organization and Design Instruction Sets - 2
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
32-bit MIPS ISA.
ENGR 3410 – Computer Architecture Mark L. Chang Fall 2006
Computer Organization and Design Instruction Sets
Instructions - Type and Format
Appendix A Classifying Instruction Set Architecture
Lecture 4: MIPS Instruction Set
ECE232: Hardware Organization and Design
Today’s Lecture Quick Review of Last Lecture
John Kubiatowicz (http.cs.berkeley.edu/~kubitron)
Computer Instructions
Computer Architecture
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
CS352H Computer Systems Architecture
Reduced Instruction Set Computer (RISC)
MIPS Arithmetic and Logic Instructions
Presentation transcript:

Lecture 2 CS154B Review of MIPS ISA and Performance

Review: Organization  All computers consist of five components Processor: (1) datapath and (2) control (3) Memory I/O: (4) Input devices and (5) Output devices  Not all “memory” is created equally Cache: fast (expensive) memory are placed closer to the processor Main memory: less expensive memory--we can have more  Input and output (I/O) devices have the messiest organization Wide range of speed: graphics vs. keyboard Wide range of requirements: speed, standard, cost... Least amount of research (so far)

Summary: Computer System Components Proc Caches Busses Memory I/O Devices: Controllers adapters Disks Displays Keyboards Networks  All have interfaces & organizations

Review: Instruction Set Design instruction set software hardware Which is easier to change/design???

Instruction Set Architecture: What Must be Specified? Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction  Instruction Format or Encoding how is it decoded?  Location of operands and result where other than memory? how many explicit operands? how are memory operands located? which can or cannot be in memory?  Data type and Size  Operations what are supported  Successor instruction jumps, conditions, branches fetch-decode-execute is implicit!

Basic ISA Classes Accumulator (1 register): 1 address add A acc  acc + mem[A] 1+x addressaddx Aacc  acc + mem[A + x] Stack: 0 addressaddtos  tos + next General Purpose Register (can be memory/memory): 2 addressadd A BEA[A]  EA[A] + EA[B] 3 addressadd A B CEA[A]  [B] + EA[C] Load/Store: 3 addressadd Ra Rb RcRa  Rb + Rc load Ra RbRa  mem[Rb] store Ra Rbmem[Rb]  Ra Comparison: Bytes per instruction? Number of Instructions? Cycles per instruction? Most real machines are hybrids of these:

Comparing Number of Instructions Code sequence for (C = A + B) for four classes of instruction sets: Stack Accumulator Register (load-store) Push ALoad ALoad R1,A Push BAdd BLoad R2,B AddStore C Register (register-memory) Load R1,A Add R1,B Store C, R1 Add R3,R1,R2 Pop CStore C,R3

General Purpose Registers Dominate ° all machines use general purpose registers °Advantages of registers registers are faster than memory registers are easier for a compiler to use - e.g., (A*B) – (C*D) – (E*F) can do multiplies in any order vs. stack registers can hold variables -memory traffic is reduced, so program is sped up (since registers are faster than memory) - code density improves (since register named with fewer bits than memory location)

MIPS I Registers  Programmable storage 2^32 x bytes of memory 31 x 32-bit GPRs (R0 = 0) 32 x 32-bit FP regs (paired DP) HI, LO, PC

Memory Addressing °Since 1980 almost every machine uses addresses to level of 8-bits (byte) °2 questions for design of ISA: Since could read a 32-bit word as four loads of bytes from sequential byte addresses or as one load word from a single byte address, How do byte addresses map onto words? Can a word be placed on any byte boundary?

Addressing Objects: Endianess and Alignment  Big Endian: address of most significant byte = word address (xx00 = Big End of word) IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA  Little Endian:address of least significant byte = word address (xx00 = Little End of word) Intel 80x86, DEC Vax, DEC Alpha (Windows NT) msb lsb little endian byte big endian byte 0 Alignment: require that objects fall on address that is multiple of their size Aligned Not Aligned

Addressing Modes Addressing modeExample Meaning RegisterAdd R4,R3 R4  R4+R3 ImmediateAdd R4,#3 R4  R4+3 DisplacementAdd R4,100(R1) R4  R4+Mem[100+R1] Register indirectAdd R4,(R1) R4  R4+Mem[R1] Indexed / BaseAdd R3,(R1+R2) R3  R3+Mem[R1+R2] Direct or absoluteAdd R1,(1001) R1  R1+Mem[1001] Memory indirectAdd R1  R1+Mem[Mem[R3]] Post-incrementAdd R1,(R2)+ R1  R1+Mem[R2]; R2  R2+d Pre-decrementAdd R1,–(R2) R2  R2–d; R1  R1+Mem[R2] Scaled Add R1,100(R2)[R3] R1  R1+Mem[100+R2+R3*d]

Addressing Mode Usage? (ignore register mode) 3 programs measured on machine with all address modes (VAX) --- Displacement:42% avg, 32% to 55% 75% --- Immediate: 33% avg, 17% to 43% 85% --- Register deferred (indirect): 13% avg, 3% to 24% --- Scaled: 7% avg, 0% to 16% --- Memory indirect: 3% avg, 1% to 6% --- Misc:2% avg, 0% to 3% 75% displacement & immediate 85% displacement, immediate & register indirect

Generic Examples of Instruction Format Widths Variable: Fixed: Hybrid: … …

Instruction Formats If code size is most important, use variable length instructions If performance is most important, use fixed length instructions Recent embedded machines (ARM, MIPS) added optional mode to execute subset of 16-bit wide instructions (Thumb, MIPS16); per procedure decide performance or density Some architectures actually exploring on-the-fly decompression for more density.

MIPS Addressing Modes/Instruction Formats oprsrtrd immed register Register (direct) oprsrt register Base+index + Memory immed oprsrt Immediate immed oprsrt PC PC-relative + Memory All instructions 32 bits wide Register Indirect?

Typical Operations (little change since 1960) Data Movement Load (from memory) Store (to memory) memory-to-memory move register-to-register move input (from I/O device) output (to I/O device) push, pop (to/from stack) Arithmetic integer (binary + decimal) or FP Add, Subtract, Multiply, Divide Logical not, and, or, set, clear Shift shift left/right, rotate left/right Control (Jump/Branch) unconditional, conditional Subroutine Linkage call, return Interrupt trap, return Synchronization test & set (atomic r-m-w) String search, translate Graphics (MMX) parallel subword ops (4 16bit add)

Top 10 80x86 Instructions

Use of Registers  Ex: a = ( b + c) - ( d + e) ;// C statement -add $t0, $s1, $s2 # $s0 - $s4 : a - e -add $t1, $s3, 4s4 -sub $s0, $t0, $t1 a = b + A[4];// add an array element to a var // $s3 has address A -lw$t0, 16($s3) -add $s1, $s2, $t0

Operation Summary Support these simple instructions, since they will dominate the number of instructions executed: load, store, add, subtract, move register-register, and, shift, compare equal, compare not equal, branch, jump, call, return;

Use of Registers : load and store  Ex A[8]= a + A[6] ;// A is in $s3, a is in $s2 lw$t0, 24($s3) # $t0 gets A[6] contents add $t0, $s2, $s0# $t0 gets the sum sw $t0, 32($s3)# sum is put in A[8]

load and store Ex: a = b + A[i]; // A is in $s3, a,b, i in // $s1, $s2, $s4 add $t1, $t1, $s3# $t1 = 2 * i add $t1, $t1, $t1# $t1 = 4 * i add $t1, $t1, $s3 # $t1 =addr. of A[i] ($s3+(4*i)) lw$t0, 0($t1)# $t0 = A[i] add$s1, $s2, $t0# a = b + A[i]

Making Decisions  Ex if ( a==b) goto L1// x,y,z,a,b map to $s0-$s4 x = y + z; L1 : x = x – a; beq$s3, $s4, L1# goto L1 if a=b add $s0, $s1, $s2 # x = y + z (ignore ifa=b) L1:sub $s0, $s0, $s3# x = x – a (always ex)

if-then-else  Ex: if ( a==b) x = y + z; else x = y – z ; bne$s3, $s4, Else# goto Else if a!=b add $s0, $s1, $s2# x = y + z jExit# goto Exit Else : sub $s0,$s1,$s2 # x = y – z Exit :

Loops  Ex: while ( A[i] == k )// i,j,k in $s3. $s4, $s5 i = i + j;// A is in $s6 Loop : add $t1, $s3, $s3# $t1 = 2 * i add $t1, $t1, $t1# $t1 = 4 * I add $t1, $t1, $s6# $t1 = addr. Of A[i] lw $t0, 0($t1)# $t0 = A[i] bne $t0, $s5, Exit# goto Exit if A[i]!=k add $s3, $s3, $s4# i = i + j j Loop# goto Loop Exit :

Compiling switch switch(a) { case 0 : f = I + j;break; case 1 : f = g + h; break; case 2 : f = g – h; break; case 3 : f = i – j;break; } slt$t3, $s5, $zero# is k < 0 bne$t3, $zero, Exit# if k < 0, goto Exit slt $t3, $s5, $t2# is k < 4 beq$t3, $zero, Exit# if k >=4 goto Exit

Switch cont. add$t1, $s5, $s5# $t1 = 2 * i add $t1, $t1, $t1# $t1 = 4 * i add$t1, $t1, $t4# $t1 = addr. Of table[k] lw$t0, 0($t1)# $t0 = table[k] jr$t0# jump to addr. In $t0 L0 : add$s0, $s3, $s4# f = i + j j Exit L1 :add$s0, $s1, $s2# f = g + h j Exit L2 :sub$s0, $s1, $s2# f = g – h jExit L3 :sub$s0, $s1, $s2# f = i – j Exit :

Calling conventions intfunc(int g, int h, int i, int j) {int f; f = ( g + h ) – ( i + j ) ; return ( f ); }// g,h,i,j - $a0,$a1,$a2,$a3, f in $s0 func : sub$sp, $sp, 12# make room in stack for 3 words sw$t1, 8($sp)# save the regs we want to use sw$t0, 4($sp) sw$s0, 0($sp) add$t0, $a0, $a1# $t0 = g + h add$t1, $a2, $a3# $t1 = i + j sub$s0, $t0, $t1# $s0 has the result add$v0, $s0, $zero# return reg $v0 has f

Calling (cont.) lw$s0, 0($sp)# restore $s0 lw$t0, 4($sp)# restore $t0 lw$t1, 8($sp)# restore $t1 add$sp, $sp, 12# restore sp we did not have to restore $t0-$t9 (caller does not expect) we do need to restore $s0-$s7 (must be preserved)

Compiling a String Copy Proc. voidstrcpy ( char x[ ], y[ ]) {int i=0; while ( (x[ i ] = y [ i ] != 0) i ++ ; }// x and y base addr. are in $a0 and $a1 strcpy : sub$sp, $sp, 4# reserve 1 word space in stack sw$s0, 0($sp)# save $s0 add$s0, $zer0, $zer0# i = 0 L1 : add$t1, $a1, $s0# addr. of y[ i ] in $t1 lb$t2, 0($t1)# $t2 = y[ i ] add$t3, $a0, $s0# addr. Of x[ i ] in $t3 sb$t2, 0($t3)# x[ i ] = y [ i ] beq $t2, $zero, L2# if y [ i ] = 0 goto L1

string copy (cont.) add$s0, $s0, 1# i ++ jL1# go to L1 L2 :lw$s0, 0($sp)# restore $s0 add $sp, $sp, 4# restore $sp jr$ra# return  it may be handier to use immediate operands sometimes To add an integer in location 2000 to $sp lw$t0, 2000($zero)# $t0 = (2000) Add$sp, $sp, $t0# $sp = $sp + (2000) Use : addi$sp, $sp, 4# same effect # avoids memory access

Array vs. Pointers Set1 ( int array [ ], int size) {int i ; for ( i = 0; i < size; i ++) array [ i ] = i; } Set2 ( int *array, int size) {int *p, i=0 ; for ( p = &array[0]; p < &array[size]; p ++) *p = i ++; }

Arrays vs. Pointers : MIPS for array version // $a0 = addr. of array, $a1 has size, $t0 = i move$t0, $zero# i = 0 Loop1:add$t1, $t0, $t0# $t1 = 2 * i add $t1, $t1, $t1# $t1 = 4 * i add$t2, $a0, $t1# $t2 = addr. of array sw$t0, 0($t2)# array[i] = i addi$t0, $t0,1# i ++ slt$t3, $t0, $a1# check end of loop (i <size) bne$t3, $zero, Loop1# if i < size goto Loop1

Arrays vs. Pointers : MIPS for pointer version // $a0 = addr. of array, $a1 has size, $t0 = p move$t0, $a0# p = addr. of array[0] move$t4, $zero# $t4 = i add$t1, $a1, $a1# $t1 = 2 * size add$t1, $t1, $t1# $t1 = 4 * size add$t2, $a0, $t1# $t2 = addr. of array[size] Loop2:sw$t4, 0($t0)# store i in *p addi $t0, $t0, 4# p = p + 4 addi$t4, $t4,1# i ++ slt$t3, $t0, $t2# $t3=(p<&array[size]) bne$t3, $zero, Loop2# if p < last addr. Go Loop1

MIPS I Operation Overview  Arithmetic Logical: Add, AddU, Sub, SubU, And, Or, Xor, Nor, SLT, SLTU AddI, AddIU, SLTI, SLTIU, AndI, OrI, XorI, LUI SLL, SRL, SRA, SLLV, SRLV, SRAV  Memory Access: LB, LBU, LH, LHU, LW, LWL,LWR SB, SH, SW, SWL, SWR

Multiply / Divide  Start multiply, divide MULT rs, rt MULTU rs, rt DIV rs, rt DIVU rs, rt  Move result from multiply, divide MFHI rd MFLO rd  Move to HI or LO MTHI rd MTLO rd Registers HILO

Data Types Bit: 0, 1 Bit String: sequence of bits of a particular length 4 bits is a nibble 8 bits is a byte 16 bits is a half-word 32 bits is a word 64 bits is a double-word Character: ASCII 7 bit code UNICODE 16 bit code Decimal: digits 0-9 encoded as 0000b thru 1001b two decimal digits packed per 8 bit byte Integers: 2's Complement Floating Point: Single Precision Double Precision Extended Precision M x R E How many +/- #'s? Where is decimal pt? How are +/- exponents represented? exponent base mantissa

Operand Size Usage Support for these data sizes and types: 8-bit, 16-bit, 32-bit integers and 32-bit and 64-bit IEEE 754 floating point numbers

MIPS arithmetic instructions InstructionExampleMeaningComments add add $1,$2,$3$1 = $2 + $33 operands; exception possible subtractsub $1,$2,$3$1 = $2 – $33 operands; exception possible add immediateaddi $1,$2,100$1 = $ constant; exception possible add unsignedaddu $1,$2,$3$1 = $2 + $33 operands; no exceptions subtract unsignedsubu $1,$2,$3$1 = $2 – $33 operands; no exceptions add imm. unsign.addiu $1,$2,100$1 = $ constant; no exceptions multiply mult $2,$3Hi, Lo = $2 x $364-bit signed product multiply unsignedmultu$2,$3Hi, Lo = $2 x $364-bit unsigned product divide div $2,$3Lo = $2 ÷ $3,Lo = quotient, Hi = remainder Hi = $2 mod $3 divide unsigned divu $2,$3Lo = $2 ÷ $3,Unsigned quotient & remainder Hi = $2 mod $3 Move from Himfhi $1$1 = HiUsed to get copy of Hi Move from Lomflo $1$1 = LoUsed to get copy of Lo

MIPS logical instructions InstructionExampleMeaningComment and and $1,$2,$3$1 = $2 & $33 reg. operands; Logical AND oror $1,$2,$3$1 = $2 | $33 reg. operands; Logical OR xorxor $1,$2,$3$1 = $2  $33 reg. operands; Logical XOR nornor $1,$2,$3$1 = ~($2 |$3)3 reg. operands; Logical NOR and immediateandi $1,$2,10$1 = $2 & 10Logical AND reg, constant or immediateori $1,$2,10$1 = $2 | 10Logical OR reg, constant xor immediate xori $1, $2,10 $1 = ~$2 &~10Logical XOR reg, constant shift left logicalsll $1,$2,10$1 = $2 << 10Shift left by constant shift right logicalsrl $1,$2,10$1 = $2 >> 10Shift right by constant shift right arithm.sra $1,$2,10$1 = $2 >> 10Shift right (sign extend) shift left logicalsllv $1,$2,$3$1 = $2 << $3 Shift left by variable shift right logicalsrlv $1,$2, $3 $1 = $2 >> $3 Shift right by variable shift right arithm.srav $1,$2, $3 $1 = $2 >> $3 Shift right arith. by variable

MIPS data transfer instructions InstructionComment SW 500(R4), R3Store word SH 502(R2), R3Store half SB 41(R3), R2Store byte LW R1, 30(R2)Load word LH R1, 40(R3)Load halfword LHU R1, 40(R3)Load halfword unsigned LB R1, 40(R3)Load byte LBU R1, 40(R3)Load byte unsigned LUI R1, 40Load Upper Immediate (16 bits shifted left by 16) 0000 … 0000 LUI R5 R5

When does MIPS sign extend?  When value is sign extended, copy upper bit to full value: Examples of sign extending 8 bits to 16 bits:    When is an immediate value sign extended? Arithmetic instructions (add, sub, etc.) sign extend immediates even for the unsigned versions of the instructions! Logical instructions do not sign extend  Load/Store half or byte do sign extend, but unsigned versions do not.

Methods of Testing Condition  Condition Codes Processor status bits are set as a side-effect of arithmetic instructions (possibly on Moves) or explicitly by compare or test instructions. ex:add r1, r2, r3 bz label  Condition Register Ex:cmp r1, r2, r3 bgt r1, label  Compare and Branch Ex:bgt r1, r2, label

Conditional Branch Distance 25% of integer branches are 2 to 4 instructions

Conditional Branch Addressing  PC-relative since most branches are relatively close to the current PC  At least 8 bits suggested (  128 instructions)  Compare Equal/Not Equal most important for integer programs (86%)

MIPS Compare and Branch  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 < BGEZ >= BLTZAL rs, offset if R[rs] < 0 then branch and link (into R 31) BGEZAL >=!  Remaining set of compare and branch ops take two instructions  Almost all comparisons are against zero!

MIPS jump, branch, compare instructions InstructionExampleMeaning branch on equalbeq $1,$2,100if ($1 == $2) go to PC Equal test; PC relative branch branch on not eq.bne $1,$2,100if ($1!= $2) go to PC Not equal test; PC relative set on less thanslt $1,$2,$3if ($2 < $3) $1=1; else $1=0 Compare less than; 2’s comp. set less than imm.slti $1,$2,100if ($2 < 100) $1=1; else $1=0 Compare < constant; 2’s comp. set less than uns.sltu $1,$2,$3if ($2 < $3) $1=1; else $1=0 Compare less than; natural numbers set l. t. imm. uns.sltiu $1,$2,100if ($2 < 100) $1=1; else $1=0 Compare < constant; natural numbers jumpj 10000go to Jump to target address jump registerjr $31go to $31 For switch, procedure return jump and linkjal 10000$31 = PC + 4; go to For procedure call

Signed vs. Unsigned Comparison R1= 0… R2= 0… R3= 1…  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? Why? r4 = ; r5 = ; r6 = ; r7 = ; two

Calls: Why Are Stacks So Great? Stacking of Subroutine Calls & Returns and Environments: A: CALL B CALL C C: RET B: A AB ABC AB A Some machines provide a memory stack as part of the architecture (e.g., VAX) Sometimes stacks are implemented via software convention (e.g., MIPS)

Memory Stacks Useful for stacked environments/subroutine call & return even if operand stack not part of architecture Stacks that Grow Up vs. Stacks that Grow Down: a b c 0 Little inf. Big 0 Little inf. Big Memory Addresses SP Next Empty? Last Full? How is empty stack represented? Little --> Big/Last Full POP: Read from Mem(SP) Decrement SP PUSH: Increment SP Write to Mem(SP) grows up grows down Little --> Big/Next Empty POP: Decrement SP Read from Mem(SP) PUSH: Write to Mem(SP) Increment SP

Call-Return Linkage: Stack Frames FP ARGS Callee Save Registers Local Variables SP Reference args and local variables at fixed (positive) offset from FP Grows and shrinks during expression evaluation (old FP, RA)  Many variations on stacks possible (up/down, last pushed / next )  Compilers normally keep scalar variables in registers, not memory! High Mem Low Mem

0zero constant 0 1atreserved for assembler 2v0expression evaluation & 3v1function results 4a0arguments 5a1 6a2 7a3 8t0temporary: caller saves...(callee can clobber) 15t7 MIPS: Software conventions for Registers 16s0callee saves... (callee must save) 23s7 24t8 temporary (cont’d) 25t9 26k0reserved for OS kernel 27k1 28gpPointer to global area 29spStack pointer 30fpframe pointer 31raReturn Address (HW)

Details of the MIPS instruction set  Register zero always has the value zero (even if you try to write it)  Branch/jump and link put the return addr. PC+4 or 8 into the link register (R31) (depends on logical vs physical architecture)  All instructions change all 32 bits of the destination register (including lui, lb, lh) and all read all 32 bits of sources (add, sub, and, or, …)  Immediate arithmetic and logical instructions are extended as follows: logical immediates ops are zero extended to 32 bits arithmetic immediates ops are sign extended to 32 bits (including addu)  The data loaded by the instructions lb and lh are extended as follows: lbu, lhu are zero extended lb, lh are sign extended  Overflow can occur in these arithmetic and logical instructions: add, sub, addi it cannot occur in addu, subu, addiu, and, or, xor, nor, shifts, mult, multu, div, divu

Miscellaneous MIPS I instructions  breakA breakpoint trap occurs, transfers control to exception handler  syscallA system trap occurs, transfers control to exception handler  coprocessor instrs.Support for floating point  TLB instructionsSupport for virtual memory: discussed later  restore from exceptionRestores previous interrupt mask & kernel/user mode bits into status register  load word left/rightSupports misaligned word loads  store word left/rightSupports misaligned word stores

Summary: Salient features of MIPS I 32-bit fixed format inst (3 formats) bit GPR (R0 contains zero) and 32 FP registers (and HI LO) – partitioned by software convention 3-address, reg-reg arithmetic instr. Single address mode for load/store: base+displacement – no indirection, scaled 16-bit immediate plus LUI Simple branch conditions – compare against zero or two registers for =,  – no integer condition codes Delayed branch – execute instruction after a branch (or jump) even if the branch is taken (Compiler can fill a delayed branch with useful work about 50% of the time)

Summary: Instruction set design (MIPS)  Use general purpose registers with a load-store architecture: YES  Provide at least 16 general purpose registers plus separate floating- point registers: 31 GPR & 32 FPR  Support basic addressing modes: displacement (with an address offset size of 12 to 16 bits), immediate (size 8 to 16 bits), and register deferred; : YES: 16 bits for immediate, displacement (disp=0 => register deferred)  All addressing modes apply to all data transfer instructions : YES  Use fixed instruction encoding if interested in performance and use variable instruction encoding if interested in code size : Fixed  Support these data sizes and types: 8-bit, 16-bit, 32-bit integers and 32-bit and 64-bit IEEE 754 floating point numbers: YES  Support these simple instructions, since they will dominate the number of instructions executed: load, store, add, subtract, move register-register, and, shift, compare equal, compare not equal, branch (with a PC-relative address at least 8-bits long), jump, call, and return: YES, 16b  Aim for a minimalist instruction set: YES