Mips Assembly

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-$t9 for temporary variables, $s0-$s7 for saved variables, $a0-$a3 for arguments, $v0-$v1 for return values.

  • Memory segments:

    • .data → for storing variables, strings, constants

    • .text → for storing code

  • main function 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

$zero

Constant 0

1

$at

Assembler temporary (reserved)

2–3

$v0–$v1

Function return values

4–7

$a0–$a3

Function arguments 1–4

8–15

$t0–$t7

Temporaries (caller-saved)

16–23

$s0–$s7

Saved registers (callee-saved)

24–25

$t8–$t9

Temporaries (caller-saved)

26–27

$k0–$k1

Reserved for OS / kernel

28

$gp

Global pointer

29

$sp

Stack pointer

30

$fp

Frame pointer

31

$ra

Return address (used by jal)

Operation

Syscall $v0

Input/Output Register

Notes

Print Integer

1

$a0

Integer to print

Print Float

2

$f12

32-bit float

Print Double

3

$f12

64-bit double

Print String

4

$a0

Address of null-terminated string

Print Character

11

$a0

ASCII char

Read Integer

5

$v0

Stores result in $v0

Read Float

6

$f0

Stores result in $f0

Read Double

7

$f0

Stores result in $f0

Read Character

12

$v0

Stores ASCII in $v0

Read String

8

$a0 (buffer), $a1 (length)

Reads up to $a1-1 chars, null-terminated

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 via slt + 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-$f31 registers

Updated on