Skip to content

MIPS Assembly Quick Cheat Sheet

āĻāĻ–āĻžāύ⧇ C āĻĨ⧇āϕ⧇ MIPS Assembly-āϤ⧇ āϰ⧂āĻĒāĻžāĻ¨ā§āϤāϰ⧇āϰ āϕ⧁āχāĻ• āϚāĻŋāϟ āĻļāĻŋāϟ āĻĻ⧇āĻ“ā§ŸāĻž āĻšāϞ⧋:

1. MIPS āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰ āĻĒāϰāĻŋāϚāĻŋāϤāĻŋ (MIPS Registers)

MIPS-āĻ ā§Šā§¨āϟāĻŋ āĻœā§‡āύāĻžāϰ⧇āϞ āĻĒāĻžāϰāĻĒāĻžāϏ āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰ āĻĨāĻžāϕ⧇āĨ¤ āϏāĻŦāĻšā§‡ā§Ÿā§‡ āĻŦ⧇āĻļāĻŋ āĻŦā§āϝāĻŦāĻšā§ƒāϤ āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰāϗ⧁āϞ⧋ āĻšāϞ⧋:

  • $zero: āĻāϰ āĻŽāĻžāύ āϏāĻŦāϏāĻŽā§Ÿ 0 (āĻ•āύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϟ)āĨ¤
  • $s0 - $s7: āϏ⧇āĻ­āĻĄ āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰ (Saved Registers), āϏāĻžāϧāĻžāϰāĻŖāϤ C-āĻāϰ āϭ⧇āϰāĻŋā§Ÿā§‡āĻŦāϞāϗ⧁āϞ⧋ āĻāĻ–āĻžāύ⧇ āϰāĻžāĻ–āĻž āĻšā§ŸāĨ¤
  • $t0 - $t9: āĻŸā§‡āĻŽā§āĻĒā§‹āϰāĻžāϰāĻŋ āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰ (Temporary Registers), āĻšāĻŋāϏāĻžāĻŦ-āύāĻŋāĻ•āĻžāĻļ⧇āϰ āĻŽāĻžāĻāĻ–āĻžāύ⧇āϰ āϏāĻžāĻŽā§ŸāĻŋāĻ• āĻŽāĻžāύ āϰāĻžāĻ–āĻžāϰ āϜāĻ¨ā§āϝāĨ¤
  • $a0 -$a3`: āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āφāĻ°ā§āϗ⧁āĻŽā§‡āĻ¨ā§āϟ (Arguments) āĻĒāĻžāϏ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝāĨ¤
  • $v0 - $v1: āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ­ā§āϝāĻžāϞ⧁ (Return Value) āϰāĻžāĻ–āĻžāϰ āϜāĻ¨ā§āϝāĨ¤

2. āĻĄāĻžāϟāĻž āĻŽā§āĻ­āĻŽā§‡āĻ¨ā§āϟ āĻāĻŦāĻ‚ āĻ…ā§āϝāĻžāϏāĻžāχāύāĻŽā§‡āĻ¨ā§āϟ (Variable Assignment)

MIPS-āĻ āϏāϰāĻžāϏāϰāĻŋ āĻŽā§‡āĻŽā§‹āϰāĻŋ-āϟ⧁-āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ…āĻĒāĻžāϰ⧇āĻļāύ āĻ•āϰāĻž āϝāĻžā§Ÿ āύāĻžāĨ¤ āĻŽāĻžāύ āϞ⧋āĻĄ (lw) āĻŦāĻž āĻ¸ā§āĻŸā§‹āϰ (sw) āĻ•āϰāϤ⧇ āĻšā§ŸāĨ¤

C Code MIPS Assembly āĻŦā§āϝāĻžāĻ–ā§āϝāĻž
int x = 5; li $s0, 5 Load Immediate: $s0 āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰ⧇ (āϧāϰāĻŋ āĻāϟāĻŋ x) āϏāϰāĻžāϏāϰāĻŋ 5 āϰāĻžāĻ–āĻž āĻšāϞ⧋āĨ¤
x = y; move $s0, $s1 $s1 (āϧāϰāĻŋ āĻāϟāĻŋ y) āĻāϰ āĻŽāĻžāύ $s0 āϤ⧇ āĻ•āĻĒāĻŋ āĻ•āϰāĻž āĻšāϞ⧋āĨ¤
x = arr[2]; lw $s0, 8($s2) Load Word: āĻ…ā§āϝāĻžāϰ⧇āϰ āĻŦ⧇āϏ āĻ…ā§āϝāĻžāĻĄā§āϰ⧇āϏ $s2 āĻšāϞ⧇, ā§Šā§Ÿ āωāĻĒāĻžāĻĻāĻžāύ (āχāĻ¨ā§āĻĄā§‡āĻ•ā§āϏ ⧍, āϤāĻžāχ \(2 \times 4 = 8\) āĻŦāĻžāχāϟ āĻ…āĻĢāϏ⧇āϟ) āĻĨ⧇āϕ⧇ āĻŽāĻžāύ āϞ⧋āĻĄ āĻ•āϰāĻž āĻšāϞ⧋āĨ¤

3. āĻ—āĻžāĻŖāĻŋāϤāĻŋāĻ• āĻ…āĻĒāĻžāϰ⧇āĻļāύ (Arithmetic Operations)

C Code MIPS Assembly āĻŦā§āϝāĻžāĻ–ā§āϝāĻž
x = a + b; add $s0, $s1, $s2 $s0 = $s1 + $s2
x = a - 5; subi $s0, $s1, 5 Add Immediate (āĻŦāĻŋāϝāĻŧā§‹āϗ⧇āϰ āϜāĻ¨ā§āϝ āύ⧇āϗ⧇āϟāĻŋāĻ­ āĻŽāĻžāύ āĻŦāĻž subi āĻŦā§āϝāĻŦāĻšā§ƒāϤ āĻšā§Ÿ): $s0 = $s1 - 5
x = a * b; mult $s1, $s2


mflo $s0 | āϗ⧁āĻŖāĻĢāϞ⧇āϰ āύāĻŋāĻšā§‡āϰ ā§Šā§¨-āĻŦāĻŋāϟ lo āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰ⧇ āĻĨāĻžāϕ⧇, āϏ⧇āĻ–āĻžāύ āĻĨ⧇āϕ⧇ āĻŽāĻžāύ $s0 āϤ⧇ āφāύāĻž āĻšāϞ⧋āĨ¤ |


4. āĻ•āĻ¨ā§āĻĄāĻŋāĻļāύāĻžāϞ āĻ¸ā§āĻŸā§‡āĻŸãƒĄãƒŗãƒˆ (If-Else)

MIPS-āĻ āĻ•āĻ¨ā§āĻĄāĻŋāĻļāύ⧇āϰ āϜāĻ¨ā§āϝ beq (Branch if Equal) āĻāĻŦāĻ‚ bne (Branch if Not Equal) āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšā§ŸāĨ¤

C Code:

if (x == y) {
    a = 1;
} else {
    a = 2;
}

MIPS Assembly:

# āϧāϰāĻŋ, x = $s0, y = $s1, a = $s2
    bne $s0, $s1, else_block    # āϝāĻĻāĻŋ x != y āĻšā§Ÿ, āϤāĻŦ⧇ else_block āĻ āϝāĻžāĻ“
    li $s2, 1                   # If block: a = 1
    j exit_block                # Else āĻŦā§āϞāĻ• āĻ¸ā§āĻ•āĻŋāĻĒ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ āϜāĻžāĻŽā§āĻĒ (Jump)

else_block:
    li $s2, 2                   # Else block: a = 2

exit_block:
    # āϕ⧋āĻĄā§‡āϰ āĻĒāϰāĻŦāĻ°ā§āϤ⧀ āĻ…āĻ‚āĻļ

5. āϞ⧁āĻĒ (While Loop)

C Code:

int i = 0;
while (i < 10) {
    i++;
}

MIPS Assembly:

# āϧāϰāĻŋ, i = $s0
    li $s0, 0           # i = 0

loop:
    slti $t0, $s0, 10   # āϝāĻĻāĻŋ i < 10 āĻšā§Ÿ, āϤāĻŦ⧇ $t0 = 1, āύ⧟āϤ⧋ $t0 = 0 (Set Less Than Immediate)
    beq $t0, $zero, exit # āϝāĻĻāĻŋ $t0 == 0 āĻšā§Ÿ (āĻ…āĻ°ā§āĻĨāĻžā§Ž i >= 10), āϤāĻŦ⧇ āϞ⧁āĻĒ āĻĨ⧇āϕ⧇ āĻŦ⧇āϰ āĻšā§Ÿā§‡ āϝāĻžāĻ“

    addi $s0, $s0, 1    # i++
    j loop              # āφāĻŦāĻžāϰ āϞ⧁āĻĒ⧇āϰ āĻļ⧁āϰ⧁āϤ⧇ āĻĢāĻŋāϰ⧇ āϝāĻžāĻ“

exit:
    # āϞ⧁āĻĒ⧇āϰ āĻĒāϰ⧇āϰ āϕ⧋āĻĄ

6. āĻĢāĻžāĻ‚āĻļāύ āĻ•āϞ (Function Call)

MIPS-āĻ āĻĢāĻžāĻ‚āĻļāύ āĻ•āϞ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ jal (Jump and Link) āĻāĻŦāĻ‚ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ jr $ra (Jump Register) āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšā§ŸāĨ¤

C Code:

int main() {
    int x = fun(5);
}

MIPS Assembly:

main:
    li $a0, 5           # āφāĻ°ā§āϗ⧁āĻŽā§‡āĻ¨ā§āϟ āϰ⧇āϜāĻŋāĻ¸ā§āϟāĻžāϰ $a0 āϤ⧇ 5 āϰāĻžāĻ–āĻž āĻšāϞ⧋
    jal fun             # fun āĻĢāĻžāĻ‚āĻļāύ āĻ•āϞ āĻ•āϰāĻž āĻšāϞ⧋ ($ra āϤ⧇ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ…ā§āϝāĻžāĻĄā§āϰ⧇āϏ āϏ⧇āĻ­ āĻšā§Ÿ)
    move $s0, $v0       # āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ­ā§āϝāĻžāϞ⧁ $v0 āĻĨ⧇āϕ⧇ $s0 (x) āĻ āϰāĻžāĻ–āĻž āĻšāϞ⧋

    # āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽ āĻļ⧇āώ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ āϏāĻŋāĻ¸ā§āĻŸā§‡āĻŽ āĻ•āϞ
    li $v0, 10
    syscall

fun:
    # āϧāϰāĻŋ āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ āχāύāĻĒ⧁āĻŸā§‡āϰ āϏāĻžāĻĨ⧇ ⧍ āϝ⧋āĻ— āĻ•āϰ⧇ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰ⧇
    addi $v0, $a0, 2    # $v0 = $a0 + 2 (āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ­ā§āϝāĻžāϞ⧁ $v0 āϤ⧇ āϰāĻžāĻ–āĻž āĻšāϞ⧋)
    jr $ra              # āĻŽā§‡āχāύ āĻĢāĻžāĻ‚āĻļāύ⧇ āĻĢāĻŋāϰ⧇ āϝāĻžāĻ“ (Jump Register)

💡 MIPS āϟāĻŋāĻĒ: MIPS-āĻ āĻĒā§āϰāϤāĻŋāϟāĻŋ āύāĻŋāĻ°ā§āĻĻ⧇āĻļāύāĻžāϰ (Instruction) āϏāĻžāχāϜ āύāĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āϟ (ā§Ē āĻŦāĻžāχāϟ āĻŦāĻž ā§Šā§¨-āĻŦāĻŋāϟ)āĨ¤ āϤāĻžāχ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻŦāĻž āĻ…ā§āϝāĻžāϰ⧇ āύāĻŋā§Ÿā§‡ āĻ•āĻžāϜ āĻ•āϰāĻžāϰ āϏāĻŽā§Ÿ āĻ…āĻĢāϏ⧇āϟ āϏāĻŦāϏāĻŽā§Ÿ ā§Ē āĻāϰ āϗ⧁āĻŖāĻŋāϤāĻ• (āϝ⧇āĻŽāύ: 0($s0), 4($s0), 8($s0)) āĻšāĻŋāϏ⧇āĻŦ⧇ āĻŦāĻžā§œā§‡āĨ¤

1. MIPS Register Reference

MIPS has 32 general-purpose registers. The most frequently used ones are:

  • $zero: Always holds the constant value 0.
  • $s0 -$s7`: Saved registers (used to hold C variables).
  • $t0 -$t9`: Temporary registers (used for intermediate calculations).
  • $a0 -$a3`: Argument registers (used to pass arguments to functions).
  • $v0 -$v1`: Value registers (used to return values from functions).

2. Data Movement & Assignment

MIPS cannot perform direct memory-to-memory operations. Data must be loaded (lw) into registers or stored (sw) back into memory.

C Code MIPS Assembly Explanation
int x = 5; li $s0, 5 Load Immediate: Loads the constant 5 directly into $s0 (assuming $s0 is x).
x = y; move $s0, $s1 Copies the value of $s1 (assuming y) into $s0.
x = arr[2]; lw $s0, 8($s2) Load Word: If $s2 holds the base address of the array, index 2 is at offset 8 bytes (\(2 \times 4\) bytes per integer).

3. Arithmetic Operations

C Code MIPS Assembly Explanation
x = a + b; add $s0, $s1, $s2 $s0 = $s1 + $s2
x = a - 5; addi $s0, $s1, -5 Add Immediate: Subtraction is done by adding a negative integer: $s0 = $s1 + (-5)
x = a * b; mult $s1, $s2


mflo $s0 | Multiplies $s1 and $s2. The lower 32 bits of the product go to the lo register, which is then moved to $s0. |


4. Conditional Statements (If-Else)

MIPS uses branches like beq (Branch if Equal) and bne (Branch if Not Equal) to implement conditions.

C Code:

if (x == y) {
    a = 1;
} else {
    a = 2;
}

MIPS Assembly:

# Assuming: x = $s0, y = $s1, a = $s2
    bne $s0, $s1, else_block    # If x != y, jump to else_block
    li $s2, 1                   # If block: a = 1
    j exit_block                # Unconditional Jump to skip the else block

else_block:
    li $s2, 2                   # Else block: a = 2

exit_block:
    # Next lines of code

5. Loops (While Loop)

C Code:

int i = 0;
while (i < 10) {
    i++;
}

MIPS Assembly:

# Assuming: i = $s0
    li $s0, 0           # i = 0

loop:
    slti $t0, $s0, 10   # Set Less Than Immediate: If i < 10, then $t0 = 1, else $t0 = 0
    beq $t0, $zero, exit # If $t0 == 0 (meaning i >= 10), break out of the loop

    addi $s0, $s0, 1    # i++
    j loop              # Jump back to the start of the loop

exit:
    # Code after loop

6. Function Calls

MIPS uses jal (Jump and Link) to call a function and jr $ra (Jump Register) to return back to the caller.

C Code:

int main() {
    int x = fun(5);
}

MIPS Assembly:

main:
    li $a0, 5           # Pass 5 as an argument to $a0
    jal fun             # Jump and Link to 'fun' (saves return address in $ra)
    move $s0, $v0       # Copy return value from $v0 to $s0 (x)

    # Exit program via system call
    li $v0, 10
    syscall

fun:
    # Let's assume the function adds 2 to the input and returns it
    addi $v0, $a0, 2    # $v0 = $a0 + 2 (Result is placed in return register $v0)
    jr $ra              # Jump back to main using the address in $ra

💡 MIPS Pointer Pointer: Every instruction in MIPS is exactly 4 bytes (32-bit). When dealing with memory byte-addressing (like arrays), your offsets must always increment by multiples of 4 (e.g., 0($s0), 4($s0), 8($s0)).