MIPS Instruction Set James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy
MIPS Registers MIPS architecture has a 32 32-bit register file (e.g. it has 32 32-bit registers) Registers are numbered 0 to 31 Registers can be referenced in assembly code using various names Register Number Register Name Use 0 $zero Constant value 0 1-7 Coming soon Coming soon 8-15 $t0 - $t7 Temporary values 16-23 $s0 - $s7 Saved temporary values 24-25 $t8 - $t9 Temporary values 26-31 Coming soon Coming soon 2
Representing Instructions All instructions are encoded in binary Binary values represent type of instruction, source and destination registers, etc. Sequence of binary instructions is referred to as machine code Machine code is the output of an assembler Contents of executable application files is machine code Loaded into TEXT section of memory when application runs Interpreted by CPU as program executes 3
MIPS Instruction Formats MIPS instructions are encoded as 32-bit words Three basic MIPS instruction formats MIPS instructions includes multiple fields : A 6-bit operation or OPCODE Specifies which operation to execute (fewer than 64) Up to three 5-bit OPERAND fields Each a register (one of 32) as source/destination May contain embedded constants or immediate values 5-bits, 16-bits, or 26-bits long 3 register operands (2 source, 1 dest.) R-Type opcode rs rt rd shamt funct 6-bits 5-bits 5-bits 5-bits 5-bits 6-bits 2 register operands, 16-bit constant I-Type opcode rs rt 16-bit constant/offset 6-bits 5-bits 5-bits 16-bits No register operands, 26-bit address constant J-Type opcode 26-bit address constant 6-bits 26-bits Can be treated as signed or unsigned 4
MIPS R-Type Instructions Used for operations that require two source operands and shift operations add, addu, sub, subu, div, divu, mult, multu, and, or, nor, xor, etc. sll, srl, sra Instruction fields op: operation code (opcode) 3 register operands (2 source, 1 dest.) R-Type opcode rs rt rd shamt funct 6-bits 5-bits 5-bits 5-bits 5-bits 6-bits rs: first source register number rt: second source register number rd: destination register number shamt: shift amount funct: function code (extends opcode) 5
MIPS R-Type Instruction Example An example add instruction: op = 0 ten R-Type ALU instruction rs = 17 ten 1 st source reg $s1 rt = 18 ten 2 nd source reg $s2 rd = 8 ten destination reg $t0 shamt = 0 ten shift amount unused here funct = 32 ten in conjunction with opcode, add R-Type 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 = 0x02324020 opcode rs rt rd shamt funct 6-bits 5-bits 5-bits 5-bits 5-bits 6-bits add $8, $17, $18 NOTE: the ordering of registers is different between the machine add $t0, $s1, $s2 code and the assembly 6
MIPS I-Type Instructions Used for immediate arithmetic, load/store, comparisons, branching instructions addi, addiu, andi, ori, etc. lw, sw, etc. Instruction fields op: operation code (opcode) 2 register operands, 16-bit constant/offset I-Type opcode rs rt 16-bit constant/offset 6-bits 5-bits 5-bits 16-bits rs: base address for load/store operations rt: destination or source register number 16-bit constant/offset: 2 15 to +2 15 1 Constant for instructions that use immediate values Offset for load and store operations (added to base address) Offset for branching operations 7
MIPS I-Type Instruction Example (immediate) An example addi instruction: op = 8 ten addi instruction rs = 11 ten source reg $t3 rt = 9 ten destination reg $t1 constant = 3 ten value to add to rs I-Type 0 0 1 0 0 0 0 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 = 0x2169FFFD opcode rs rt 16-bit constant/offset 6-bits 5-bits 5-bits 16-bits addi $9, $11, -3 NOTE: the 16-bit constant is sign- addi $t1, $t3, -3 extended prior to summing with rs. 8
MIPS I-Type Instruction Example (load) An example lw instruction: op = 35 ten lw instruction rs = 15 ten base reg $t7 rt = 23 ten destination reg $s7 constant = 4 ten offset to add to base register rs I-Type 1 0 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 = 0x8DF70004 opcode rs rt 16-bit constant/offset 6-bits 5-bits 5-bits 16-bits lw $23, 4($15) NOTE: the 16-bit constant is sign- lw $s7, 4($t7) extended prior to summing with rs. 9
MIPS I-Type Instruction Example (store) An example sw instruction: op = 43 ten sw instruction rs = 8 ten base reg $t7 rt = 21 ten source reg $s7 constant = 12 ten offset to add to base register rs I-Type 1 0 1 0 1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 = 0xAD15000C opcode rs rt 16-bit constant/offset 6-bits 5-bits 5-bits 16-bits sw $21, 12($8) NOTE: the 16-bit constant is sign- sw $s5, 12($t0) extended prior to summing with rs. 10
Logical Operations Instructions for bitwise manipulation Useful for extracting and inserting groups of bits in a word Operation C / C++ MIPS Shift Left << sll Shift Right >> srl, sra Bitwise AND & and, andi Bitwise OR or, ori Bitwise XOR ^ xor Bitwise NOT ~ nor int x = 3; int y = 4; int z; z = x << 1; // z is now 6 z = y >> 1; // z is now 2 z = x & y; // z is now 0 z = x y; // z is now 7 z = x ^ y; // z is now 7 z = ~x; // z is now 0xfffffffc either -4 or 4,294,967,292 11
Shift Operations Uses R-Type instruction shamt field how many positions to shift Shift left logical 3 register operands (2 source, 1 dest.) R-Type opcode rs rt rd shamt funct 6-bits 5-bits 5-bits 5-bits 5-bits 6-bits Shift left and fill with 0 bits sll by i bits multiplies by 2 i Shift right logical before Shift right and fill with 0 bits srl by i bits divides by 2 i (unsigned only) Shift right arithmetic Shift right and fill with copy of MSB Shift Left Logical: The empty position in the least significant bit is filled with a zero. Shift Right Arithmetic: The empty position in the most significant bit is filled with a copy of the original MSB. after 12
AND Operations Uses R-Type instruction (opcode = 0x00, funct = 0x24) Useful to mask bits in a word Select some bits, clear others to 0 and $t0, $t1, $t2 register $t1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 register $t2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 perform AND operation result of AND stored in register $t0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 13
OR Operations Uses R-Type instruction (opcode = 0x00, funct = 0x25) Useful to include bits in a word Set some bits to 1, leave others unchanged or $t0, $t1, $t2 register $t1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 register $t2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 perform OR operation result of OR stored in register $t0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 14
XOR Operations Uses R-Type instruction (opcode = 0x00, funct = 0x26) Useful to find bits that differ between words Set output bit to 1 if input bits differ, others 0 xor $t0, $t1, $t2 register $t1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 register $t2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 perform XOR operation result of XOR stored in register $t0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 15
NOT Operations MIPS has no NOT instruction Instead, uses a NOR instruction (R-Type) a NOR b == NOT(a OR b) // to get a NOT, just set b=0 nor $t0, $t1, $zero # negates $t1, stores result in $t0 Useful to invert bits in a word Changes 0 s to 1 s, and 1 s to 0 s register $t1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 1 perform NOR operation result of NOR stored in register $t0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 0 16