MIPS Instruction Set Architecture

The MIPS (Microprocessor without Interlocked Pipeline Stages) Instruction Set Architecture (ISA) is one of the most widely studied and implemented ISAs in the field of computer science and engineering. Developed in the early 1980s at Stanford University by a team led by John L. Hennessy, the MIPS architecture is renowned for its simplicity, efficiency, and scalability.

What is an ISA?

An Instruction Set Architecture (ISA) is a crucial interface between hardware and software in a computer system. It defines the set of instructions that a processor can execute, including arithmetic operations, data manipulation, control flow instructions, and memory access commands. The ISA serves as a blueprint for designing both hardware (CPU) and software (compilers, operating systems).

Why MIPS?

The MIPS architecture is particularly favored in educational settings for several reasons:

  1. Simplicity: MIPS has a small, regular instruction set, making it easier to learn and understand.

  2. RISC Principles: MIPS is a classic example of a Reduced Instruction Set Computer (RISC) architecture, which uses a small number of simple instructions for efficient performance.

  3. Scalability: MIPS architecture is scalable, allowing it to be used in a wide range of applications, from embedded systems to high-performance computing.

  4. Educational Tools: A variety of simulation tools and resources are available for learning and experimenting with MIPS, including the SPIM simulator and MARS (MIPS Assembler and Runtime Simulator).

Key Features of MIPS ISA

  • Load/Store Architecture: MIPS uses a load/store architecture where operations are performed on registers, and only load and store instructions access memory.

  • Fixed Instruction Length: All MIPS instructions are 32 bits long, simplifying instruction decoding.

  • Three-Operand Format: Most arithmetic instructions use a three-operand format (e.g., ADD R1, R2, R3), enhancing readability and reducing the need for complex instruction decoding.

  • Pipeline-Friendly: The design of the MIPS instruction set facilitates pipelining, a technique used to increase the throughput of a processor.

MIPS Instruction Categories

MIPS instructions are categorized into three main types:

  1. R-type (Register): Instructions that perform operations on registers. Example: ADD R1, R2, R3.

  2. I-type (Immediate): Instructions that use an immediate value. Example: ADDI R1, R2, 10.

  3. J-type (Jump): Instructions for control flow changes. Example: J label.

Understanding MIPS Registers

In the MIPS Instruction Set Architecture (ISA), registers play a crucial role in the execution of instructions. Registers are small, fast storage locations within the CPU that hold data to be processed. MIPS has 32 general-purpose registers, each 32 bits wide, as well as a few special-purpose registers.

General-Purpose Registers

MIPS registers are conventionally named and numbered for ease of use. Here's a detailed breakdown of these registers:

Register NameRegister NumberAliasDescription
$zero0-Constant value 0
$at1-Assembler temporary
$v0-$v12-3-Function return values
$a0-$a34-7-Function arguments
$t0-$t78-15-Temporary registers, not preserved across calls
$s0-$s716-23-Saved registers, preserved across calls
$t8-$t924-25-Additional temporary registers
$k0-$k126-27-Reserved for kernel use
$gp28-Global pointer
$sp29-Stack pointer
$fp30-Frame pointer
$ra31-Return address

Special-Purpose Registers

In addition to the general-purpose registers, MIPS includes several special-purpose registers:

Register NameDescription
HIHigh result of multiplication and division operations
LOLow result of multiplication and division operations
PCProgram Counter, holds the address of the next instruction to be executed

Detailed Descriptions

General-Purpose Registers

  1. $zero (Register 0): Always contains the constant value 0. Any attempt to write to this register is ignored.

  2. $at (Register 1): Reserved for use by the assembler.

  3. $v0-$v1 (Registers 2-3): Used to hold the return values of functions.

  4. $a0-$a3 (Registers 4-7): Used to pass the first four arguments to functions.

  5. $t0-$t7 (Registers 8-15): Temporary registers that are not preserved across function calls. Can be used freely within a function.

  6. $s0-$s7 (Registers 16-23): Saved registers that must be preserved across function calls. If a function uses these registers, it must save their values and restore them before returning.

  7. $t8-$t9 (Registers 24-25): Additional temporary registers, similar to $t0-$t7.

  8. $k0-$k1 (Registers 26-27): Reserved for operating system kernel use.

  9. $gp (Register 28): Global pointer, used to access static data.

  10. $sp (Register 29): Stack pointer, points to the top of the stack.

  11. $fp (Register 30): Frame pointer, used to reference the base of the current stack frame.

  12. $ra (Register 31): Return address, holds the return address for function calls.

Special-Purpose Registers

  1. HI and LO Registers: These are used to store the results of multiplication and division operations. For example, after a multiplication operation, the higher-order 32 bits of the result are stored in HI and the lower-order 32 bits are stored in LO.

  2. Program Counter (PC): This register holds the address of the next instruction to be executed. It is automatically updated by the CPU as instructions are executed.

MIPS Instruction Categories

MIPS instructions are grouped into several categories based on their functionality. Here's a summary of the main instruction categories in the MIPS ISA:

Instruction CategoryDescription
Arithmetic InstructionsPerform arithmetic operations like addition, subtraction, multiplication, and division.
Logical InstructionsPerform bitwise logical operations like AND, OR, XOR, and NOT.
Data Transfer InstructionsMove data between registers and memory (load and store operations).
Control Flow InstructionsChange the sequence of instruction execution (branching, jumping).
Comparison InstructionsCompare values and set flags or produce results based on the comparison.
Shift InstructionsPerform bit shifting operations (left shift, right shift).
Immediate InstructionsUse immediate (constant) values as operands.
Special InstructionsInclude miscellaneous operations like NOP (no operation) and system calls.

Arithmetic Instructions

NameAssembler FormatOperationTypeOpcodeFunction Code
ADDADD rd, rs, rtrd = rs + rtR000000100000
ADDIADDI rt, rs, immrt = rs + immI001000-
ADDUADDU rd, rs, rtrd = rs + rtR000000100001
ADDIUADDIU rt, rs, immrt = rs + immI001001-
SUBSUB rd, rs, rtrd = rs - rtR000000100010
SUBUSUBU rd, rs, rtrd = rs - rtR000000100011
MULTMULT rs, rtHI:LO = rs * rtR000000011000
MULTUMULTU rs, rtHI:LO = rs * rt (unsigned)R000000011001
DIVDIV rs, rtLO = rs / rt, HI = rs % rtR000000011010
DIVUDIVU rs, rtLO = rs / rt, HI = rs % rt (unsigned)R000000011011

Logical Instructions

NameAssembler FormatOperationTypeOpcodeFunction Code
ANDAND rd, rs, rtrd = rs AND rtR000000100100
ANDIANDI rt, rs, immrt = rs AND immI001100-
OROR rd, rs, rtrd = rs OR rtR000000100101
ORIORI rt, rs, immrt = rs OR immI001101-
XORXOR rd, rs, rtrd = rs XOR rtR000000100110
XORIXORI rt, rs, immrt = rs XOR immI001110-
NORNOR rd, rs, rtrd = ~(rs OR rt)RRR

Data Transfer Instructions

NameAssembler FormatOperationTypeOpcodeFunction Code
LWLW rt, offset(rs)rt = Memory[rs + offset]I100011-
SWSW rt, offset(rs)Memory[rs + offset] = rtI101011-
LBLB rt, offset(rs)rt = Memory[rs + offset] (byte)I100000-
SBSB rt, offset(rs)Memory[rs + offset] = rt (byte)I101000-
LUILUI rt, immrt = imm << 16I001111-

Control Flow Instructions

NameAssembler FormatOperationTypeOpcodeFunction Code
JJ targetPC = PC[31:28] then append (target « 2)J000010
JALJAL targetra = PC + 4, PC = PC[31:28] then append (target « 2)J000011
JRJR rsPC = rsR000000001000
BEQBEQ rs, rt, offsetif (rs == rt) PC = PC + 4 + (offset << 2)I000100-
BNEBNE rs, rt, offsetif (rs != rt) PC = PC + 4 + (offset << 2)I000101-
BGEZBGEZ rs, offsetif (rs >= 0) PC = PC + 4 + (offset << 2)I000001-

Comparison Instructions

NameAssembler FormatOperationTypeOpcodeFunction Code
SLTSLT rd, rs, rtrd = (rs < rt)R000000101010
SLTISLTI rt, rs, immrt = (rs < imm)I001010-
SLTUSLTU rd, rs, rtrd = (rs < rt) (unsigned)R000000101011
SLTIUSLTIU rt, rs, immrt = (rs < imm) (unsigned)I001011-

Shift Instructions

NameAssembler FormatOperationTypeOpcodeFunction Code
SLLSLL rd, rt, sard = rt << saR000000000000
SRLSRL rd, rt, sard = rt >> saR000000000010
SRASRA rd, rt, sard = rt >>> saR000000000011
SLLVSLLV rd, rt, rsrd = rt << rsR000000000100
SRLVSRLV rd, rt, rsrd = rt >> rsR000000000110
SRAVSRAV rd, rt, rsrd = rt >>> rsR000000000111

Special Instructions

NameAssembler FormatOperationTypeOpcodeFunction Code
NOPSLL $0, $0, 0No operationR000000000000
SYSCALLSYSCALLSystem callSpecial000000001100
BREAKBREAKBreakpointSpecial000000001101

Legend

  • rs, rt, rd: Source and destination registers.

  • immediate: Immediate value (a constant).

  • sa: Shift amount.

  • HI, LO: Special registers used for multiplication and division results.

  • PC: Program Counter, the address of the next instruction.

  • [address]: Memory address.

  • [rs]: Address contained in register rs.

  • [x:y]: Slice of bits from x to y (inclusive).

  • <<: Left shift operator.

  • >>: Logical right shift operator.

  • >>>: Arithmetic right shift operator (preserves the sign bit).

  • ^: Bitwise XOR operator.

  • &: Bitwise AND operator.

  • \|: Bitwise OR operator.

  • ~: Bitwise NOT operator.

  • R-type: Register type instructions.

  • I-type: Immediate type instructions.

  • J-type: Jump type instructions.

  • Special: Instructions that do not fit into the R, I, or J types.

MIPS Instruction Encodings

In the MIPS Instruction Set Architecture, different types of instructions have specific binary encodings. Each instruction type is encoded using a fixed format that specifies the opcode, registers, immediate values, and other relevant information.

R-Type Instructions

R-type instructions are used for arithmetic and logical operations, and they have the following encoding format:

31-2625-2120-1615-1110-65-0
opcodersrtrdshamtfunct
  • opcode: 6 bits (always 000000 for R-type instructions)

  • rs: Source register (5 bits)

  • rt: Second source register (5 bits)

  • rd: Destination register (5 bits)

  • shamt: Shift amount (5 bits, used only for shift instructions)

  • funct: Function code (6 bits, specifies the exact operation)

I-Type Instructions

I-type instructions are used for immediate arithmetic, logical operations, and data transfer. They have the following encoding format:

31-2625-2120-1615-0
opcodersrtimmediate
  • opcode: 6 bits (specifies the operation)

  • rs: Source register (5 bits)

  • rt: Destination register or source/destination register (5 bits)

  • immediate: Immediate value or address offset (16 bits)

J-Type Instructions

J-type instructions are used for jump operations. They have the following encoding format:

31-2625-0
opcodeaddress
  • opcode: 6 bits (specifies the operation)

  • address: Target address (26 bits)

Special Instructions

Special instructions have unique formats and are used for operations like system calls and breakpoints. Here are the typical formats:

SYSCALL Instruction

The SYSCALL instruction has the following format:

31-2625-65-0
000000sys_code001100
  • opcode: 6 bits (always 000000 for SYSCALL)

  • sys_code: System call code (20 bits, usually zero for standard syscall)

  • funct: Function code (6 bits, always 001100 for SYSCALL)

BREAK Instruction

The BREAK instruction has the following format:

31-2625-65-0
000000break_code001101
  • opcode: 6 bits (always 000000 for BREAK)

  • break_code: Breakpoint code (20 bits)

  • funct: Function code (6 bits, always 001101 for BREAK)

Encoding Summary

  • R-Type: Encodes the operation with a function code and uses three registers (rs, rt, rd) and an optional shift amount.

  • I-Type: Encodes the operation with an opcode and uses two registers (rs, rt) and an immediate value or address offset.

  • J-Type: Encodes the operation with an opcode and a large address field for jumps.

  • Special: Unique formats for system calls and breakpoints with specific function codes.