Step 1: Understand MIPS Architecture Basics
Before coding, understand the core concepts:
-
MIPS is RISC (Reduced Instruction Set Computer): simple instructions executed in a single cycle.
-
Registers:
$t0-$t9for temporary variables,$s0-$s7for saved variables,$a0-$a3for arguments,$v0-$v1for return values. -
Memory segments:
-
.data→ for storing variables, strings, constants -
.text→ for storing code
-
-
mainfunction and program flow
Step 2: Learn the Sections
Data Section
.data
message: .asciiz "Hello, World!" # string
num: .word 10 # integer
flt: .float 3.14 # float
ch: .byte 'A' # char
Text Section
.text
main:
# your code here
li $v0, 10 # syscall to exit
syscall
Number(s) | Name(s) | Usage / Description |
|---|---|---|
0 |
| Constant 0 |
1 |
| Assembler temporary (reserved) |
2–3 |
| Function return values |
4–7 |
| Function arguments 1–4 |
8–15 |
| Temporaries (caller-saved) |
16–23 |
| Saved registers (callee-saved) |
24–25 |
| Temporaries (caller-saved) |
26–27 |
| Reserved for OS / kernel |
28 |
| Global pointer |
29 |
| Stack pointer |
30 |
| Frame pointer |
31 |
| Return address (used by |
Operation | Syscall | Input/Output Register | Notes |
|---|---|---|---|
Print Integer | 1 |
| Integer to print |
Print Float | 2 |
| 32-bit float |
Print Double | 3 |
| 64-bit double |
Print String | 4 |
| Address of null-terminated string |
Print Character | 11 |
| ASCII char |
Read Integer | 5 |
| Stores result in |
Read Float | 6 |
| Stores result in |
Read Double | 7 |
| Stores result in |
Read Character | 12 |
| Stores ASCII in |
Read String | 8 |
| Reads up to |
Exit Program | 10 | — | Program termination |
Step 3: Printing to Console
- Print string
li $v0, 4 # syscall code 4 = print string
la $a0, message # address of string
syscall
- Print integer
li $v0, 1
lw $a0, num # load word
syscall
- Print float
li $v0, 2
l.s $f12, flt # load float
syscall
- Print char
li $v0, 11
lb $a0, ch
syscall
Step 4: Basic Instructions
-
Arithmetic:
add, sub, mul, div -
Logical:
and, or, xor, nor, slt(set less than) -
Data movement:
li, lw, sw, la, lb, sb -
Branching:
beq, bne, blt, bgt(sometimes implemented viaslt+bne)
Step 5: Implementing Conditions
If-Else Example
lw $t0, num
li $t1, 10
beq $t0, $t1, equal
# else part
j end_if
equal:
# code if $t0 == 10
end_if:
Step 6: Loops
While / For Loop Example
li $t0, 0 # counter
loop:
bge $t0, 10, end_loop
# code
addi $t0, $t0, 1
j loop
end_loop:
Step 7: Functions
- Declaring Function
myFunction:
# function code
jr $ra # return
- Calling Function
jal myFunction # jump and link (saves return address)
Step 8: Recursion
factorial:
# assume n in $a0
li $t0, 1
beq $a0, $t0, base_case
addi $a0, $a0, -1
jal factorial # recursive call
mul $v0, $a0, $v0
jr $ra
base_case:
li $v0, 1
jr $ra
Step 9: Branching Logic
- Combining multiple conditions:
lw $t0, num
li $t1, 5
li $t2, 10
blt $t0, $t1, less_than_5
bgt $t0, $t2, greater_than_10
# between 5 and 10
j end_logic
less_than_5:
# code
j end_logic
greater_than_10:
# code
end_logic:
Step 10: Practice and Expand
-
Implement small programs:
-
Sum of array
-
Fibonacci sequence
-
Bubble sort
-
String reversal
-
-
Learn system calls (input/output, file handling)
-
Explore floating-point operations with
$f0-$f31registers