User Avatar
Discussion

What are the three types of instructions in MIPS?

The Three Types of Instructions in MIPS Architecture

MIPS (Microprocessor without Interlocked Pipeline Stages) is a reduced instruction set computer (RISC) architecture that has been widely used in embedded systems, networking equipment, and educational settings. One of the key features of MIPS is its simplicity and regularity, which makes it an excellent platform for learning computer architecture. The MIPS instruction set is divided into three main types of instructions: R-type (Register-type), I-type (Immediate-type), and J-type (Jump-type). Each type serves a distinct purpose and has a specific format. In this article, we will explore these three types of instructions in detail, including their formats, uses, and examples.


1. R-Type Instructions (Register-Type)

R-type instructions are the most fundamental type of instructions in MIPS. They operate exclusively on registers and are used for arithmetic, logical, and data movement operations. The name "R-type" comes from the fact that these instructions use only registers as operands.

Format of R-Type Instructions

R-type instructions have a fixed 32-bit format, which is divided into six fields:

Opcode (6 bits) Rs (5 bits) Rt (5 bits) Rd (5 bits) Shamt (5 bits) Funct (6 bits)
000000 Source 1 Source 2 Destination Shift Amount Function Code
  • Opcode (6 bits): For R-type instructions, the opcode is always 000000, indicating that this is an R-type instruction.
  • Rs (5 bits): Specifies the first source register.
  • Rt (5 bits): Specifies the second source register.
  • Rd (5 bits): Specifies the destination register where the result will be stored.
  • Shamt (5 bits): Stands for "shift amount." This field is used in shift instructions to specify the number of bits to shift. For most R-type instructions, this field is set to 00000.
  • Funct (6 bits): Specifies the specific operation to be performed (e.g., addition, subtraction, etc.).

Examples of R-Type Instructions

  1. Addition (add):

    • Syntax: add $rd, $rs, $rt
    • Example: add $t0, $t1, $t2
      • This adds the contents of registers $t1 and $t2 and stores the result in $t0.
  2. Subtraction (sub):

    • Syntax: sub $rd, $rs, $rt
    • Example: sub $t0, $t1, $t2
      • This subtracts the contents of $t2 from $t1 and stores the result in $t0.
  3. Logical AND (and):

    • Syntax: and $rd, $rs, $rt
    • Example: and $t0, $t1, $t2
      • This performs a bitwise AND operation on the contents of $t1 and $t2 and stores the result in $t0.
  4. Shift Left Logical (sll):

    • Syntax: sll $rd, $rt, shamt
    • Example: sll $t0, $t1, 2
      • This shifts the contents of $t1 left by 2 bits and stores the result in $t0.

Key Characteristics of R-Type Instructions

  • Operate only on registers.
  • Use the funct field to specify the operation.
  • Do not involve immediate values or memory addresses.

2. I-Type Instructions (Immediate-Type)

I-type instructions are used for operations that involve an immediate value (a constant) or for accessing memory. The name "I-type" comes from the inclusion of an immediate value in the instruction.

Format of I-Type Instructions

I-type instructions also have a fixed 32-bit format, divided into four fields:

Opcode (6 bits) Rs (5 bits) Rt (5 bits) Immediate (16 bits)
Opcode Source Destination Immediate Value
  • Opcode (6 bits): Specifies the operation to be performed (e.g., load, store, branch, etc.).
  • Rs (5 bits): Specifies the source register.
  • Rt (5 bits): Specifies the destination register (for load operations) or the source register (for store operations).
  • Immediate (16 bits): A 16-bit constant value or offset.

Examples of I-Type Instructions

  1. Load Word (lw):

    • Syntax: lw $rt, offset($rs)
    • Example: lw $t0, 4($t1)
      • This loads a word from memory at the address $t1 + 4 into register $t0.
  2. Store Word (sw):

    • Syntax: sw $rt, offset($rs)
    • Example: sw $t0, 8($t1)
      • This stores the contents of register $t0 into memory at the address $t1 + 8.
  3. Add Immediate (addi):

    • Syntax: addi $rt, $rs, immediate
    • Example: addi $t0, $t1, 10
      • This adds the immediate value 10 to the contents of $t1 and stores the result in $t0.
  4. Branch if Equal (beq):

    • Syntax: beq $rs, $rt, offset
    • Example: beq $t0, $t1, 16
      • If the contents of $t0 and $t1 are equal, the program counter jumps to the address PC + 4 + (offset * 4).

Key Characteristics of I-Type Instructions

  • Include an immediate value or offset.
  • Used for memory access, arithmetic with constants, and branching.
  • The immediate value is sign-extended to 32 bits before use.

3. J-Type Instructions (Jump-Type)

J-type instructions are used for unconditional jumps, such as function calls or jumps to specific addresses. These instructions modify the program counter (PC) directly.

Format of J-Type Instructions

J-type instructions have a fixed 32-bit format, divided into two fields:

Opcode (6 bits) Address (26 bits)
Opcode Target Address
  • Opcode (6 bits): Specifies the operation (e.g., jump or jump and link).
  • Address (26 bits): Specifies the target address for the jump. This address is combined with the current program counter to form the final jump address.

Examples of J-Type Instructions

  1. Jump (j):

    • Syntax: j target
    • Example: j 0x00400000
      • This jumps to the address 0x00400000.
  2. Jump and Link (jal):

    • Syntax: jal target
    • Example: jal 0x00400000
      • This jumps to the address 0x00400000 and saves the return address (PC + 4) in the $ra register.

Key Characteristics of J-Type Instructions

  • Used for unconditional jumps.
  • Modify the program counter directly.
  • The target address is combined with the current PC to form the final address.

Comparison of the Three Instruction Types

Feature R-Type I-Type J-Type
Operands Registers only Registers + Immediate Address
Opcode Fixed (000000) Variable Variable
Use Case Arithmetic, logic Memory access, branching Unconditional jumps
Example add $t0, $t1, $t2 lw $t0, 4($t1) j 0x00400000

Conclusion

The MIPS instruction set is designed with simplicity and regularity in mind, making it an excellent tool for understanding computer architecture. The three types of instructions—R-type, I-type, and J-type—serve distinct purposes and are used in different scenarios. R-type instructions handle register-based operations, I-type instructions deal with immediate values and memory access, and J-type instructions manage unconditional jumps. By mastering these instruction types, one can gain a deep understanding of how a CPU executes instructions and processes data. Whether you're a student, a hobbyist, or a professional, understanding MIPS instructions is a valuable skill in the world of computing.

1.2K views 0 comments

Comments (45)

User Avatar