Complete Guide to Learning MIPS Assembly
with MARS Simulator
From Installation to Program Debugging
Abdallah BENOUARETH
October 26, 2025
Contents
1 Introduction 4
1.1 What is MIPS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 What is MARS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Why Use MARS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Installation 4
2.1 System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Installing Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.2 macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.3 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Downloading MARS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Running MARS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4.2 macOS/Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 MARS Interface Overview 5
3.1 Main Window Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1.1 Menu Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1.2 Edit Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.3 Execute Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Tool Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4 MIPS Assembly Basics 6
4.1 Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 Common Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3.1 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3.2 Data Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.3.3 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Writing Your First Program 7
5.1 Hello World Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.2 System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6 Assembling and Running Programs 8
6.1 Step-by-Step Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
6.2 Understanding Assembly Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7 Debugging Techniques 8
7.1 Step-by-Step Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.1.1 Single Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.1.2 Backstep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
7.2 Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
7.3 Watching Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
7.4 Monitoring Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
8 Advanced Features 9
8.1 Memory Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
8.2 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
8.3 Useful Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2
From Installation to Debugging MARS MIPS Tutorial
8.3.1 Keyboard and Display MMIO Simulator . . . . . . . . . . . . . . . . . . . 10
8.3.2 Data Segment Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
8.3.3 Instruction Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
9 Complete Example Programs 10
9.1 Sum of Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
9.2 Factorial Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
10 Best Practices and Tips 12
10.1 Code Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.2 Debugging Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.3 Common Mistakes to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.4 Performance Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
11 Troubleshooting 13
11.1 MARS Won’t Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
11.2 Assembly Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
11.3 Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
12 Additional Resources 14
12.1 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
12.2 Practice Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
13 Conclusion 14
Page 3
From Installation to Debugging MARS MIPS Tutorial
1 Introduction
1.1 What is MIPS?
MIPS (Microprocessor without Interlocked Pipelined Stages) is a reduced instruction set com-
puter (RISC) instruction set architecture (ISA). It is widely used in education to teach computer
architecture and assembly language programming due to its simplicity and elegant design.
1.2 What is MARS?
MARS (MIPS Assembler and Runtime Simulator) is an Integrated Development Environment
(IDE) for programming in MIPS assembly language. It provides a user-friendly interface for
writing, assembling, running, and debugging MIPS programs.
1.3 Why Use MARS?
• Free and open-source
• Cross-platform (Windows, macOS, Linux)
• Visual debugging capabilities
• Real-time register and memory inspection
• Built-in MIPS instruction reference
• No physical hardware required
2 Installation
2.1 System Requirements
• Java Runtime Environment (JRE) 1.8 or higher
• Any operating system supporting Java (Windows, macOS, Linux)
• Minimum 512 MB RAM
• 50 MB free disk space
2.2 Installing Java
2.2.1 Windows
1. Visit [Link]
2. Download the Windows installer
3. Run the installer and follow the prompts
4. Verify installation by opening Command Prompt and typing: java -version
2.2.2 macOS
1. Visit [Link]
2. Download the macOS installer
3. Open the .dmg file and follow installation instructions
4. Verify by opening Terminal and typing: java -version
Page 4
From Installation to Debugging MARS MIPS Tutorial
2.2.3 Linux
Open terminal and run:
1 sudo apt update
2 sudo apt install default - jre
3 java - version
2.3 Downloading MARS
1. Visit [Link]
2. Download the latest version (Mars4 [Link] or similar)
3. Save it to a convenient location (e.g., Documents/MARS)
4. No installation needed - it’s a standalone JAR file
2.4 Running MARS
2.4.1 Windows
Double-click the [Link] file, or open Command Prompt and run:
1 java - jar Mars4_5 . jar
2.4.2 macOS/Linux
Open Terminal, navigate to the folder containing MARS, and run:
1 java - jar Mars4_5 . jar
Alternatively, make it executable:
1 chmod + x Mars4_5 . jar
2 ./ Mars4_5 . jar
3 MARS Interface Overview
3.1 Main Window Components
3.1.1 Menu Bar
• File: New, Open, Save, Print operations
• Edit: Undo, Redo, Cut, Copy, Paste, Find/Replace
• Run: Assemble, Go, Step, Backstep, Reset
• Settings: Editor, Exception Handler, Memory Configuration
• Tools: Various utilities and system calls
• Help: Documentation and instruction reference
Page 5
From Installation to Debugging MARS MIPS Tutorial
3.1.2 Edit Tab
The editor where you write MIPS assembly code. Features:
• Syntax highlighting
• Line numbering
• Auto-indentation
• Error highlighting
3.1.3 Execute Tab
Appears after assembly. Contains:
• Text Segment: Displays assembled machine code and instructions
• Data Segment: Shows memory contents
• Registers: Displays all 32 MIPS registers and their values
• Program Counter: Shows current instruction address
3.2 Tool Bar
Quick access buttons:
• New, Open, Save
• Assemble, Run, Step, Reset
• Undo, Redo
4 MIPS Assembly Basics
4.1 Program Structure
A MIPS assembly program typically consists of two sections:
1 . data # Data section
2 # Variables and constants go here
3
4 . text # Code section
5 . globl main # Declare main as global
6 main : # Program entry point
7 # Your code here
4.2 Registers
MIPS has 32 general-purpose registers:
4.3 Common Instructions
4.3.1 Arithmetic
1 add $t0 , $t1 , $t2 # $t0 = $t1 + $t2
2 sub $t0 , $t1 , $t2 # $t0 = $t1 - $t2
3 addi $t0 , $t1 , 10 # $t0 = $t1 + 10
4 mul $t0 , $t1 , $t2 # $t0 = $t1 * $t2
5 div $t1 , $t2 # Lo = $t1 / $t2 , Hi = $t1 % $t2
Page 6
From Installation to Debugging MARS MIPS Tutorial
Register Name Usage
$0 $zero Constant 0
$1 $at Assembler temporary
$2-$3 $v0-$v1 Function return values
$4-$7 $a0-$a3 Function arguments
$8-$15 $t0-$t7 Temporary registers
$16-$23 $s0-$s7 Saved registers
$24-$25 $t8-$t9 More temporaries
$28 $gp Global pointer
$29 $sp Stack pointer
$30 $fp Frame pointer
$31 $ra Return address
Table 1: MIPS Register Convention
4.3.2 Data Transfer
1 li $t0 , 100 # Load immediate : $t0 = 100
2 la $t0 , label # Load address
3 lw $t0 , 0( $t1 ) # Load word from memory
4 sw $t0 , 0( $t1 ) # Store word to memory
5 move $t0 , $t1 # $t0 = $t1
4.3.3 Control Flow
1 beq $t0 , $t1 , label # Branch if equal
2 bne $t0 , $t1 , label # Branch if not equal
3 blt $t0 , $t1 , label # Branch if less than
4 bgt $t0 , $t1 , label # Branch if greater than
5 j label # Jump to label
6 jal function # Jump and link ( function call )
7 jr $ra # Jump register ( return )
5 Writing Your First Program
5.1 Hello World Example
1 . data
2 message : . asciiz " Hello , MIPS World !\ n "
3
4 . text
5 . globl main
6 main :
7 # Print string
8 li $v0 , 4 # System call code for print_string
9 la $a0 , message # Load address of message
10 syscall # Execute system call
11
12 # Exit program
13 li $v0 , 10 # System call code for exit
14 syscall
5.2 System Calls
Common MARS system calls:
Page 7
From Installation to Debugging MARS MIPS Tutorial
Service $v0 Arguments
print int 1 $a0 = integer
print float 2 $f12 = float
print double 3 $f12 = double
print string 4 $a0 = string address
read int 5 result in $v0
read float 6 result in $f0
read double 7 result in $f0
read string 8 $a0 = buffer, $a1 = length
exit 10 none
Table 2: Common MARS System Calls
6 Assembling and Running Programs
6.1 Step-by-Step Process
1. Write Code: Type your program in the Edit tab
2. Save File: File → Save As (use .asm extension)
3. Assemble: Run → Assemble (or F3)
4. Check for Errors: Review the Messages tab at the bottom
5. Execute: Run → Go (or F5)
6. View Output: Check the Run I/O tab at the bottom
6.2 Understanding Assembly Errors
Common errors and solutions:
• Label not found: Check spelling and ensure label is defined
• Invalid instruction: Verify instruction syntax
• Register expected: Use proper register notation ($)
• Runtime exception: Check array bounds, null pointers
7 Debugging Techniques
7.1 Step-by-Step Execution
7.1.1 Single Step
1. Assemble your program (F3)
2. Click ”Step” button (F7) or Run → Step
3. Observe changes in registers and memory after each instruction
4. Current instruction is highlighted in yellow
Page 8
From Installation to Debugging MARS MIPS Tutorial
7.1.2 Backstep
• Click ”Backstep” (F8) to reverse one instruction
• Useful for examining what changed in previous step
• Limited history (default is 2000 steps)
7.2 Breakpoints
1. In Execute tab, click the checkbox next to an instruction
2. Run the program (F5)
3. Execution pauses at breakpoint
4. Inspect registers and memory
5. Continue with ”Go” or step through
7.3 Watching Registers
• Registers tab shows all register values in real-time
• Modified registers are highlighted
• Can view in decimal, hexadecimal, or binary
• Settings → Show Values in Hexadecimal
7.4 Monitoring Memory
1. Navigate to Data Segment tab
2. Shows memory addresses and contents
3. Can view different memory regions
4. Modified memory locations are highlighted
5. Use Tools → Memory Reference Visualizer for graphical view
8 Advanced Features
8.1 Memory Configuration
Settings → Memory Configuration:
• Adjust text segment start address
• Configure data segment size
• Set stack pointer initial value
• Modify global pointer
Page 9
From Installation to Debugging MARS MIPS Tutorial
8.2 Exception Handling
Settings → Exception Handler:
• Enable/disable exception handling
• View exception handler code
• Useful for learning about interrupts and exceptions
8.3 Useful Tools
8.3.1 Keyboard and Display MMIO Simulator
Tools → Keyboard and Display MMIO Simulator
• Simulates memory-mapped I/O
• Interactive keyboard input
• Character display output
8.3.2 Data Segment Window
Tools → Data Segment Window
• Enhanced memory visualization
• Search functionality
• Multiple view formats
8.3.3 Instruction Statistics
Tools → Instruction Statistics
• Counts instruction types executed
• Useful for performance analysis
• Shows program behavior patterns
9 Complete Example Programs
9.1 Sum of Array Elements
1 . data
2 array : . word 5 , 10 , 15 , 20 , 25 # Array of 5 integers
3 size : . word 5 # Array size
4 result_msg : . asciiz " Sum = "
5
6 . text
7 . globl main
8 main :
9 # Initialize
10 la $t0 , array # $t0 = address of array
11 lw $t1 , size # $t1 = size
12 li $t2 , 0 # $t2 = sum = 0
13 li $t3 , 0 # $t3 = index = 0
14
Page 10
From Installation to Debugging MARS MIPS Tutorial
15 loop :
16 beq $t3 , $t1 , end_loop # if index == size , exit loop
17
18 lw $t4 , 0( $t0 ) # Load array [ index ]
19 add $t2 , $t2 , $t4 # sum += array [ index ]
20
21 addi $t0 , $t0 , 4 # Move to next element (4 bytes )
22 addi $t3 , $t3 , 1 # index ++
23
24 j loop # Repeat
25
26 end_loop :
27 # Print result message
28 li $v0 , 4
29 la $a0 , result_msg
30 syscall
31
32 # Print sum
33 li $v0 , 1
34 move $a0 , $t2
35 syscall
36
37 # Exit
38 li $v0 , 10
39 syscall
9.2 Factorial Function
1 . data
2 prompt : . asciiz " Enter a number : "
3 result_msg : . asciiz " Factorial = "
4
5 . text
6 . globl main
7 main :
8 # Prompt user
9 li $v0 , 4
10 la $a0 , prompt
11 syscall
12
13 # Read integer
14 li $v0 , 5
15 syscall
16 move $a0 , $v0 # Argument for factorial
17
18 # Call factorial function
19 jal factorial
20 move $t0 , $v0 # Save result
21
22 # Print result message
23 li $v0 , 4
24 la $a0 , result_msg
25 syscall
26
27 # Print factorial
28 li $v0 , 1
29 move $a0 , $t0
30 syscall
31
32 # Exit
33 li $v0 , 10
34 syscall
Page 11
From Installation to Debugging MARS MIPS Tutorial
35
36 # Factorial function
37 # Input : $a0 = n
38 # Output : $v0 = n !
39 factorial :
40 # Save registers
41 addi $sp , $sp , -8 # Make space on stack
42 sw $ra , 4( $sp ) # Save return address
43 sw $a0 , 0( $sp ) # Save argument
44
45 # Base case : if n <= 1 , return 1
46 li $v0 , 1
47 ble $a0 , 1 , fact_return
48
49 # Recursive case : n * factorial (n -1)
50 addi $a0 , $a0 , -1 # n - 1
51 jal factorial # factorial (n -1)
52
53 lw $a0 , 0( $sp ) # Restore original n
54 mul $v0 , $a0 , $v0 # n * factorial (n -1)
55
56 fact_return :
57 # Restore registers
58 lw $ra , 4( $sp )
59 addi $sp , $sp , 8
60 jr $ra # Return
10 Best Practices and Tips
10.1 Code Organization
• Use meaningful label names
• Comment your code extensively
• Separate logical sections with blank lines
• Align instructions for readability
• Group related data declarations
10.2 Debugging Strategy
1. Start with simple test cases
2. Use step-by-step execution to understand flow
3. Watch key registers during execution
4. Add temporary print statements for debugging
5. Test boundary conditions
6. Verify loop termination conditions
10.3 Common Mistakes to Avoid
• Forgetting to initialize registers
• Incorrect branch conditions
Page 12
From Installation to Debugging MARS MIPS Tutorial
• Not saving/restoring registers in functions
• Stack pointer mismanagement
• Incorrect memory addressing
• Forgetting syscall for system calls
• Not including exit syscall
10.4 Performance Tips
• Minimize memory accesses
• Use registers for frequently accessed data
• Reduce unnecessary branches
• Unroll small loops when appropriate
• Use instruction statistics tool to identify bottlenecks
11 Troubleshooting
11.1 MARS Won’t Start
• Verify Java is installed: java -version
• Ensure you’re using Java 8 or higher
• Try running from command line to see error messages
• Check file permissions
11.2 Assembly Errors
• Read error messages carefully - they indicate line numbers
• Check syntax against MIPS reference
• Verify all labels are defined
• Ensure proper register notation
• Check for missing colons after labels
11.3 Runtime Errors
• Address out of range: Check array bounds
• Stack overflow: Verify recursive function termination
• Infinite loop: Check loop exit conditions
• Use breakpoints and stepping to locate issue
Page 13
From Installation to Debugging MARS MIPS Tutorial
12 Additional Resources
12.1 Online Resources
• MARS Homepage: [Link]
• MIPS Reference: [Link]
• MIPS Instruction Reference in MARS: Help → MIPS
12.2 Practice Exercises
1. Write a program to find the largest element in an array
2. Implement bubble sort algorithm
3. Create a program that reverses a string
4. Write a recursive Fibonacci function
5. Implement a simple calculator with basic operations
13 Conclusion
MARS provides an excellent platform for learning MIPS assembly language programming. By
following this guide, you should now be able to:
• Install and configure MARS on your system
• Write basic MIPS assembly programs
• Use the debugger effectively
• Understand common instructions and conventions
• Debug and optimize your code
The key to mastering MIPS assembly is practice. Start with simple programs and gradually
increase complexity. Use the debugging tools extensively to understand how your code executes
at the instruction level.
Remember that MARS is a simulator, and while it accurately represents MIPS behavior,
real hardware may have additional considerations. However, the fundamentals you learn with
MARS are directly applicable to real MIPS processors and provide excellent preparation for
understanding computer architecture at a deeper level.
Happy coding and may your registers always hold the right values!
Page 14