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:¶
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:¶
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:¶
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 value0.$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:¶
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:¶
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:¶
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)).