Computer Organization MIPS ISA

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

Instructions: Language of the Computer

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Course Administration

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

CENG3420 Lecture 03 Review

Chapter 2A Instructions: Language of the Computer

Computer Architecture

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.

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

CS3350B Computer Architecture MIPS Instruction Representation

CENG3420 L03: Instruction Set Architecture

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

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

Instructions: Language of the Computer

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

Instructions: Language of the Computer

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

CS3350B Computer Architecture

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

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

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

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

Control Instructions

CS3350B Computer Architecture MIPS Introduction

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

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

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

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

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

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

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

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

Chapter 2: Instructions:

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

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

CS222: MIPS Instruction Set

MIPS (SPIM) Assembler Syntax

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

COMPUTER ORGANIZATION AND DESIGN

All instructions have 3 operands Operand order is fixed (destination first)

Reduced Instruction Set Computer (RISC)

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

Reduced Instruction Set Computer (RISC)

Rechnerstrukturen. Chapter 2. Instructions: Language of the Computer

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

Instruction Set Architecture. "Speaking with the computer"

Chapter 2. Instructions: Language of the Computer

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

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

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

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

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

Chapter 2. Instruction Set Architecture (ISA)

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

EEC 581 Computer Architecture Lecture 1 Review MIPS

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Topic Notes: MIPS Instruction Set Architecture

Lecture 4: MIPS Instruction Set

MIPS Instruction Set Architecture (2)

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.

Computer Architecture

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

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Computer Architecture. The Language of the Machine

CS/COE1541: Introduction to Computer Architecture

Course Administration

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

MIPS%Assembly% E155%

CS222: Dr. A. Sahu. Indian Institute of Technology Guwahati

Unsigned Binary Integers

Unsigned Binary Integers

Computer Architecture. MIPS Instruction Set Architecture

Thomas Polzer Institut für Technische Informatik

Systems Architecture I

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

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

ECE331: Hardware Organization and Design

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

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

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

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

CS 61c: Great Ideas in Computer Architecture

Chapter 2. Instructions:

Instruction Set Architecture

Lecture 3: The Instruction Set Architecture (cont.)

Lecture 3: The Instruction Set Architecture (cont.)

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

Levels of Programming. Registers

Review of instruction set architectures

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

Machine Instructions - II. Hwansoo Han

Lectures 3-4: MIPS instructions

Chapter 2. Baback Izadi Division of Engineering Programs

Transcription:

CPE 335 Computer Organization MIPS ISA Dr. Iyad Jafar Adapted from Dr. Gheith Abandah Slides http://www.abandah.com/gheith/courses/cpe335_s08/index.html CPE 232 MIPS ISA 1

(vonneumann) Processor Organization Control needs to 1. input instructions from Memory 2. issue signals to control the information flow between the Datapath components and to control what operations they perform 3. control instruction sequencing CPU Control Datapath Memory Fetch Devices Input Output Datapath needs to have the Exec components the functional units and storage (e.g., register file) needed to execute instructions Decode interconnects - components connected so that the instructions can be accomplished and so that data can be loaded from and stored to Memory CPE 232 MIPS ISA 2

Computer Language In order to command the computer, we must speak its language, i.e. we must know its instructions and programming model (Instruction Set Architecture) Computer instructions are represented as binary electric signals at the hardware level. Programming by storing instructions in their binary format (Machine Language) is tedious and time consuming. Assembly language came into action and represented machine codes/instructions as words. Assembling was done initially by hand. Later, assemblers were introduced. In assembly, programmers have to write one line of code for each instruction. The programmers have to think like a machine. High-level programming languages and compilers CPE 232 MIPS ISA 3

Computer Language CPE 232 MIPS ISA 4

RISC - Reduced Instruction Set Computer RISC philosophy fixed instruction lengths load-store instruction sets limited addressing modes limited operations MIPS, Sun SPARC, HP PA-RISC, IBM PowerPC, HP (Compaq) Alpha, Instruction sets are measured by how well compilers use them as opposed to how well assembly language programmers use them Design goals: speed, cost (design, fabrication, test, packaging), size, power consumption, reliability, memory space (embedded systems) stems) CPE 232 MIPS ISA 5

MIPS R3000 Instruction Set Architecture (ISA) Instruction Categories Computational Load/Store Jump and Branch Floating Point - coprocessor Memory Management Special Registers R0 - R31 PC HI LO 3 Instruction Formats: all 32 bits wide OP rs rt rd sa funct R format OP rs rt immediate I format OP jump target J format CPE 232 MIPS ISA 6

Review: Unsigned Binary Representation Hex Binary Decimal 0x00000000 0 0000 0 0x00000001 0 0001 1 0x00000002 0 0010 2 0x00000003 0 00110011 3 0x00000004 0 0100 4 0x00000005 0 0101 5 0x00000006 0 0110 6 0x00000007 0 0111 7 0x00000008 0 1000 8 2 31 2 30 2 29... 2 3 2 2 2 1 2 0 bit weight 31 30 29... 3 2 1 0 bit position 1 1 1... 1 1 1 1 bit 1 0 0 0... 0 0 0 0-1 0x00000009 0 1001 9 2 32-1 0xFFFFFFFC 1 1100 2 32-4 0xFFFFFFFD 1 1101 2 32-3 0xFFFFFFFE 1 1110 2 32-2 0xFFFFFFFF 1 1111 2 32-1 CPE 232 MIPS ISA 7

Aside: Beyond Numbers American Std Code for Info Interchange (ASCII): 8-bit bytes representing characters ASCII Char ASCII Char ASCII Char ASCII Char ASCII Char ASCII Char 0 Null 32 space 48 0 64 @ 96 ` 112 p 1 33! 49 1 65 A 97 a 113 q 2 34 50 2 66 B 98 b 114 r 3 35 # 51 3 67 C 99 c 115 s 4 EOT 36 $ 52 4 68 D 100 d 116 t 5 37 % 53 5 69 E 101 e 117 u 6 ACK 38 & 54 6 70 F 102 f 118 v 7 39 55 7 71 G 103 g 119 w 8 bksp 40 ( 56 8 72 H 104 h 120 x 9 tab 41 ) 57 9 73 I 105 i 121 y 10 LF 42 * 58 : 74 J 106 j 122 z 11 43 + 59 ; 75 K 107 k 123 { 12 FF 44, 60 < 76 L 108 l 124 15 47 / 63? 79 O 111 o 127 DEL CPE 232 MIPS ISA 8

MIPS Register File Holds thirty-two 32-bit registers Two read ports and One write port Registers are Faster than main memory src1 addr src2 addr dst addr write data 5 5 5 32 Register File 32 bits 32 locations - But register files with more locations are slower (e.g., a 64 word file could write control be as much as 50% slower than a 32 word file) - Read/write port increase impacts speed quadratically Easier for a compiler to use - e.g., (A*B) (C*D) (E*F) can do multiplies in any order vs. stack Can hold variables so that - code density improves (since register are named with fewer bits than a memory location) 32 32 src1 data src2 data CPE 232 MIPS ISA 9

Aside: MIPS Register Convention Name Register Number Usage Preserve on call? $zero 0 constant 0 (hardware) n.a. $at 1 reserved for assembler n.a. $v0 - $v1 2-3 returned values no $a0 - $a3 4-7 arguments yes $t0 - $t7 8-15 temporaries no $s0 - $s7 16-23 saved values yes $t8 - $t9 24-25 temporaries no $gp 28 global pointer yes $sp 29 stack pointer yes $fp 30 frame pointer yes $ra 31 return addr (hardware) yes CPE 232 MIPS ISA 10

MIPS Arithmetic Instructions MIPS assembly language arithmetic statement add $t0, $s1, $s2 sub $t0, $s1, $s2 Each arithmetic instruction performs only one operation Each arithmetic instruction fits in 32 bits and specifies exactly three operands destination source1 op Operand order is fixed (destination first) source2 Those operands are all contained in the datapath s register file ($t0,$s1,$s2) $ 1 $ indicated by $ CPE 232 MIPS ISA 11

Assembly Example 1 Given the following piece of C code, find the equivalent assembly code using the basic MIPS ISA given so far. a = b c d=3* a Solution: assume that the variables a, b, c, and d are associated with registers $s0, $s1, $s2, and $s3, respectively, by the compiler. sub $s0, $s1, $s2 # $s0 contains b c add $s3, $s0,$s0 # $s3 contains 2*a add $s3, $s3, $s0 # $s3 contains 3*a CPE 232 MIPS ISA 12

Assembly Example 2 Given the following piece of C code, find the equivalent assembly code using the basic MIPS ISA given so far. f = (g + h) (i + j) Solution: assume that the variables f, g, h, i, and j are associated with registers $s0, $s1, $s2, $s3, and $s4, respectively, by the compiler. add $t0, $s1, $s2 # $t0 contains g + h add $t1, $s3,$s4 # $t1 contains i + j sub $s0, $t0, $t1 # $s0 contains (g+h) (i+j) CPE 232 MIPS ISA 13

Machine Language - Add Instruction Instructions are 32 bits long Arithmetic Instruction Format (R format): add $t0, $s1, $s2 op rs rt rd shamt funct op 6-bits opcode that specifies the operation rs 5-bits register file address of the first source operand rt 5-bits register file address of the second source operand rd 5-bits register file address of the result s destination shamt 5-bits shift amount (for shift instructions) funct 6-bits function code augmenting the opcode CPE 232 MIPS ISA 14

Machine language Arithmetic Example 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Op code rs rt rd shamt funct Machine code for add $s0, $t0, $s0 in hexadecimal op = 0x00 rs = $0 $t0 = 0x0808 rt = $s0 = 0x10 rd = $s0 = 0x10 shamt = 0x00 funct = 0x20 000000 01000 10000 10000 00000 100000 Machine code for sub b$ $s0, $t0, $s0in hexadecimal op = 0x0 rs = $t0 = 0x08 rt = $s0 = 0x10 rd = $s0 = 0x10 shamt = 0x00 funct = 0x22 000000 01000 10000 10000 00000 100010 CPE 232 MIPS ISA 15

MIPS Logical Instructions Similar to the arithmetic instructions, MIPS assembly language supports some logical instructions. and or $s1, $s2, $s3 # $s1 = $s2 & $s3 bitwise $s1, $s2, $s3 # $s1 = $s2 $s3 bitwise nor $s1, $s2, $s3 The instruction format is the R-type. 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Op code rs rt rd shamt funct Machine code for and $s1, $t2, $s2 in hexadecimal op = 0x00 rs = $t2 = 0x0A 0A rt = $s2= 0x12 rd = $s1 = 0x11 shamt = 0x00 funct = 0x24 000000 01010 10010 10001 00000 100100 CPE 232 MIPS ISA 16

MIPS Memory Access Instructions MIPS has two basic data transfer instructions for accessing memory lw $t0, 4($s3) #load word from memory sw $t0, 8($s3) #store word to memory The data is loaded into (lw) or stored from (sw) a register in the register file a 5 bit address The memory address a 32 bit address is formed by adding the contents t of the base address register (32 bits) to the offset value (16 bits) A16-bit field meaning access is limited to memory locations within a region of ±2 13 or 8,192 words (±2 15 or 32,768 bytes) of the address in the base register Note that the offset can be positive or negative CPE 232 MIPS ISA 17

Machine Language - Load Instruction Load/Store Instruction Format (I format): lw $t0, 24($s2) op rs rt 16 bit offset 24 10 + $s2 = 10 Memory 0xf f f f f f f f... 0001 1000 $t0 0x120040ac +... 1001 0100 $s2 0x12004094... 1010 1100 = 0x120040ac data 0x0000000c 0x00000008 0x00000004 0x00000000 word address (hex) CPE 232 MIPS ISA 18

Byte Addresses Since 8-bit bytes are so useful, most architectures address individual bytes in memory The memory address of a word must be a multiple of 4 (alignment restriction) Big Endian: leftmost t byte is word address Little Endian: IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA rightmost byte is word address Intel 80x86, DEC Vax, DEC Alpha (Windows NT) msb little endian byte 0 3 2 1 0 0 1 2 3 big endian byte 0 lsb CPE 232 MIPS ISA 19

Aside: Loading and Storing Bytes MIPS provides special instructions to move bytes lb $t0, 1($s3) #load byte from memory sb $t0, 6($s3) #store byte to memory 6 bits 5 bits 5 bits 16 bits Opcode rs rt Offset What 8 bits get loaded and stored? load byte places the byte from memory in the rightmost 8 bits of the destination register - what happens to the other bits in the register? store byte takes the byte from the rightmost 8 bits of a register and writes it to a byte in memory - what happens to the other bits in the memory word? CPE 232 MIPS ISA 20

Assembly Example 3 Assume that A is an array of 32-bit numbers whose base address is stored in $s0. Convert the following C code into MIPS assembly language. G = 2 * F A[17] = A[16] + G Solution: assume that the variables F and G are associated with registers $s4 and $s5, respectively. add $s5, $s4, $s4 # $s5 contains 2*F lw $t0, 64($s0) # $t0 contains A[16] (note the offset is 64) add $t0, $t0, $s5 # $t0 contains A[16] + G sw $t0, 68($s0) # store $t0 in A[17] (note the offset is 68) CPE 232 MIPS ISA 21

Machine language Load/Store 6 bits 5 bits 5 bits 16 bits Opcode rs rt Offset Machine code for lw $s5, 4($s1) in hexadecimal op = 0x23 rs = $s1 = 0x11 rt = $s5 = 0x15 offset = 0x0004 100011 10001 10101 0000 0000 0000 0100 Machine code for sw $s0, 16($s4) in hexadecimal op = 0x2b rs = $s4 = 0x14 rt = $s0 = 0x10 offset = 0x0010 101011 10100 10000 0000 0000 0001 0000 CPE 232 MIPS ISA 22

MIPS Control Flow Instructions MIPS conditional branch instructions: ti bne $s0, $s1, Lbl #go to Lbl if $s0 $s1 beq $s0, $s1, Lbl #go to Lbl if $s0=$s1 Ex: if (i==j) h = i + j; bne $s0, $s1, Lbl1 add $s3, $s0, $s1 Lbl1:... Instruction Format (I format): 6 bits 5 bits 5 bits 16 bits Opcode rs rt Offset beq opcode is 0x04 and bne opcode is 0x05 How is the branch destination address specified? CPE 232 MIPS ISA 23

Specifying Branch Destinations Use a register (like in lw and sw) added to the 16-bit offset which register? Instruction Address Register (the PC) - its use is automatically ti implied by instruction ti - PC gets updated (PC+4) during the fetch cycle so that it holds the address of the next instruction limits the branch distance to -2 15 to +2 15-1 instructions from the (instruction after the) branch instruction, but most branches are local anyway from the low order 16 bits of the branch instruction sign-extend PC 16 offset 00 32 32 Add 32 Add 32 32 32 4 32? branch dst address CPE 232 MIPS ISA 24

Machine language Control 6 bits 5 bits 5 bits 16 bits Opcode rs rt Offset Machine code for beq $s5, $s1, 20 in hexadecimal. Assume that PC = 100. op = 0x04 rs = $s5 = 0x15 rt = $s1 = 0x11 offset = 0x0014 000100 10101 10001 0000 0000 0001 0100 Machine code for bne $s0, $s1, 13 in hexadecimal op = 0x05 rs = $s0 = 0x10 rt = $s1 = 0x11 offset = 0x000d 000101 10000 10001 0000 0000 0000 1101 CPE 232 MIPS ISA 25

More Branch Instructions We have beq, bne, but what about other kinds of brances (e.g., branch-if-less-than)? For this, we need yet another instruction, slt Set on less than instruction: slt $t0, $s0, $s1 # if $s0 < $s1 then # $t0 = 1 else # $t0 = 0 Instruction format (R format): 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Op code rs rt rd shamt funct Opcode for slt is 0x00 funct is 0x2a CPE 232 MIPS ISA 26 2

More Branch Instructions, Con t Can use slt, beq, bne, and the fixed value of 0 in register $zero to create other conditions less than blt $s1, $s2, Label slt $at, $s1, $s2 #$at set to 1 if bne $at, $zero, Label # $s1 < $s2 less than or equal to greater than great than or equal to ble $s1, $s2, Label bgt $s1, $s2, Label bge $s1, $s2, Label Such branches are included in the instruction set as pseudo instructions ti - recognized (and expanded) d) by the assembler Its why the assembler needs a reserved register ($at) CPE 232 MIPS ISA 27

Other Control Flow Instructions MIPS also has an unconditional branch instruction ti or jump instruction: j label Instruction Format (J Format): #go to label 6 bits 26 bits Opcode Address Field from the low order 26 bits of the jump instruction 26 00 Opcode is 0x02 4 32 PC 32 CPE 232 MIPS ISA 28

Aside: Branching Far Away What if the branch destination is further away than can be captured in 16 bits? The assembler comes to the rescue it inserts an unconditional jump to the branch target and inverts the condition beq $s0, $s1, L1 becomes L2: bne j $s0, $s1, L2 L1 CPE 232 MIPS ISA 29

Instructions for Accessing Procedures MIPS procedure call instruction: ti jal ProcedureAddress e dd ess #jump and link Saves PC+4 in register $ra to have a link to the next instruction for the procedure return Machine format (J format): 6 bits 26 bits Opcode Address Field Then can do procedure return with a jr $ra #return Instruction format (R format): 6bit bits 5bit bits 5bit bits 5bit bits 5bit bits 6bit bits Op code rs CPE 232 MIPS ISA 30

Aside: Spilling Registers What if the callee needs more registers? What if the procedure is recursive? uses a stack a last-in-first-out queue in memory for passing additional values or saving (recursive) return address(es) high addr One of the general registers, $sp, is used to address the stack (which grows from high address to low address) top of stack $sp add data onto the stack push $sp = $sp 4 data on stack at new $sp low addr remove data from the stack pop data from stack at $sp $sp = $sp + 4 CPE 232 MIPS ISA 31

MIPS Immediate Instructions Small constants t are used often in typical code Possible approaches? put typical constants in memory and load them create hard-wired registers (like $zero) for constants like 1 have special instructions that contain constants! addi $sp, $sp, 4 #$sp = $sp + 4 slti $t0, $s2, 15 #$t0 = 1 if $s2<15 Machine format (I format): 6 bits 5 bits 5 bits 16 bits Opcode rs rt Offset The constant is kept inside the instruction itself! Immediate format limits values to the range +2 15 1 to -2 15 CPE 232 MIPS ISA 32

Aside: How About Larger Constants? We'd also like to be able to load a 32 bit constant into a register, for this we must use two instructions a new "load upper immediate" instruction lui $t0, 1010101010101010 16 0 8 1010101010101010 Then must get the lower order bits right, use ori $t0, $t0, 1010101010101010 1010101010101010 0000000000000000 0000000000000000 1010101010101010 1010101010101010 1010101010101010 CPE 232 MIPS ISA 33

MIPS Organization So Far src1 addr src2 addr dst addr write data Processor Register File 5 5 5 32 32 registers ($zero - $ra) 32 bits 32 32 src1 data src2 data read/write addr 32 Memory 1 11001100 2 30 words Exec branch offset 32 Add 32 PC 32 Add 32 32 4 Fetch 32 PC = PC+4 Decode 32 32 ALU 32 read data 32 write data 32 0 1100 0 1000 6 7 0 0100 0 00000000 32 bits word address (binary) 4 5 0 1 2 3 byte address (big Endian) CPE 232 MIPS ISA 34

Review of MIPS Operand Addressing Modes Register addressing operand is in a register op rs rt rd funct Register word operand Base (displacement) addressing operand is at the memory location whose address is the sum of a register and a 16-bit constant contained within the instruction op rs rt offset M base register Register relative (indirect) with 0($a0) Pseudo-direct with addr($zero) Memory word or byte operand Immediate addressing operand is a 16-bit constant contained within the instruction op rs rt operand CPE 232 MIPS ISA 35

Review of MIPS Instruction Addressing Modes PC-relative addressing instruction address is the sum of the PC and a 16-bit constant contained within the instruction op rs rt offset Program Counter (PC) Memory branch destination instruction ti Pseudo-direct addressing instruction address is the 26- bit constant contained within the instruction concatenated with the upper 4 bits of the PC op jump address Program Counter (PC) Memory jump destination instruction CPE 232 MIPS ISA 36

MIPS (RISC) Design Principles Simplicity favors regularity fixed size instructions 32-bits small number of instruction formats opcode always the first 6 bits Good design demands good compromises three instruction formats Smaller is faster limited instruction set limited number of registers in register file limited number of addressing modes Make the common case fast arithmetic operands from the register file (load-store machine) allow instructions to contain immediate operands CPE 232 MIPS ISA 37

MIPS ISA So Far Category Instr Op Code Example Meaning Arithmetic add 0 and 32 add $s1, $s2, $s3 $s1 = $s2 + $s3 (R & I subtract 0 and 34 sub $s1, $s2, $s3 $s1 = $s2 - $s3 format) add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6 or immediate 13 ori $s1, $s2, 6 $s1 = $s2 v 6 Data load word 35 lw $s1, 24($s2) $s1 = Memory($s2+24) Transfer store word 43 sw $s1, 24($s2) Memory($s2+24) = $s1 (I format) load byte 32 lb $s1, 25($s2) $s1 = Memory($s2+25) store byte 40 sb $s1, 25($s2) Memory($s2+25) = $s1 load upper imm 15 lui $s1, 6 $s1 = 6 * 2 16 Cond. br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L Branch br on not equal 5 bne $s1, $s2, L if ($s1!=$s2) go to L (I & R format) set on less than 0 and 42 slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else $s1=0 set on less than 10 slti $s1, $s2, 6 if ($s2<6) $s1=1 1 else immediate $s1=0 Uncond. jump 2 j 2500 go to 10000 Jump jump register 0 and 8 jr $t1 go to $t1 (J & R format) jump and link 3 jal 2500 go to 10000; $ra=pc+4 CPE 232 MIPS ISA 38

MIPS ISA so far Name Register Number Usage Preserve on call? $zero 0 constant 0 (hardware) n.a. $at 1 reserved for assembler n.a. $v0 - $v1 2-3 returned values no $a0 - $a3 4-7 arguments yes $t0 - $t7 8-15 temporaries no $s0 - $s7 16-23 saved values yes $t8 - $t9 24-25 temporaries no $gp 28 global pointer yes $sp 29 stack pointer yes $fp 30 frame pointer yes $ra 31 return addr (hardware) yes CPE 232 MIPS ISA 39

Examples on MIPS programming Example 1: Convert the following high-level code into MIPS assembly language. Make your own association for registers. A = 5 ; B = 2 * A ; if B < array[12] then else end array[12] = 0 array[12] = 5 CPE 232 MIPS ISA 40

Examples on MIPS programming Example 2: Assume that the following assembly code is loaded in the memory. Draw the memory and show its contents assuming that the program counter starts at 0x00000400. add $s0, $s1, $s2 beq $s0, $s3, Next lw $s0, 0($s5) Next lw $s0, 0($s6) CPE 232 MIPS ISA 41

Examples on MIPS programming Example 3: what is the assembly language statements corresponding to the following machine instructions 0x00af8020 0x0c001101 CPE 232 MIPS ISA 42

Examples on MIPS programming Example 4: if the address of the following instructions is 0x00000008 What is the address of the instruction to be executed after the following instruction j 256 What is the content of the $ra register after executing jal 256 What is the address of the instruction if the condition checked by the following instruction is true beq $s1,$s2, 25 CPE 232 MIPS ISA 43

Examples on MIPS Programming Example 5: compile the following function into MIPS Assembly language. void strcpy (char x[], char y[]) { } int i ; i = 0 ; while ((x[i] = y[i])!= \0 ) i += 1 ; CPE 232 MIPS ISA 44