Introduction
MIPS (Microprocessor without Interlocked Pipeline Stages) is a reduced instruction set computer (RISC) instruction set architecture developed by MIPS Technologies. Widely used in embedded systems, MIPS serves as an educational tool in computer architecture courses due to its simplicity and ease of understanding.
Structure of MIPS Code
MIPS programs are typically structured with two main sections:
-
.data
section:- This is where variables and data are declared.
-
.text
section:- This section contains the program instructions.
- Programs often end with a
syscall
instruction to execute system calls, such as printing or exiting.
Key Features
Register Naming
- Register names are prefixed with a dollar sign (
$
). - Common registers include:
$t0
to$t9
: Temporary registers for computations.$s0
to$s7
: Saved registers that retain their values across function calls.$a0
to$a3
: Argument registers for function inputs.$v0
,$v1
: Used for function return values and system call codes.
Common Instructions and System Calls
Instruction | Description |
---|---|
li | Load immediate |
lw | Load word |
sw | Store word |
add | Addition |
sub | Subtraction |
mul | Multiplication |
div | Division (integer) |
move | Move data between registers |
syscall | System call for input/output |
Common System Call Codes (using $v0
register)
li $v0, x | System Call Code | Description |
---|---|---|
li $v0, 1 | Print Integer | Outputs the integer in $a0 |
li $v0, 4 | Print String | Outputs the string at the address in $a0 |
li $v0, 5 | Read Integer | Reads an integer from user input |
li $v0, 8 | Read String | Reads a string input into memory |
li $v0, 10 | Exit Program | Terminates the program |
li $v0, 11 | Print Character | Outputs a single character in $a0 |
li $v0, 12 | Read Character | Reads a single character from input |
li $v0, 3 | Print Floating-Point Number | Outputs a float (in $f12 ) |
Example of Using System Calls
Below is a simple example to show how to use various system calls in MIPS assembly.
Writing MIPS Code
When writing MIPS assembly, follow these steps:
- Begin with the
.data
section for variable declarations. - Follow with the
.text
section for your program instructions. - End your program with a
syscall
to execute the final system call (e.g., exit).
Example Code
Example 1: Basic Addition and Print
Example 2
.data
message: .asciiz "Hello, MIPS!"
.text
main:
li $v0, 4 # System call code for print_string
la $a0, message # Load address of the string
syscall # Make the system call
li $v0, 10 # System call code for exit
syscall # Make the system call