Programming lesson
MIPS Assembly Loop with Flux Bunny: A Step-by-Step Lab Guide
Learn how to implement a MIPS32 program that prints 'Flux', 'Bunny', or 'Flux Bunny' based on divisibility by 5 and 7. This guide covers MARS setup, register usage, syscalls, and clean code formatting.
Introduction to the Flux Bunny MIPS Lab
In this tutorial, we walk through the Engineering Visualization lab assignment that introduces MIPS assembly language programming using the MARS IDE. You will create a program that iterates from 0 to a user-input number and prints either Flux, Bunny, Flux Bunny, or the number itself based on divisibility by 5 and 7. This lab builds foundational skills in loop control, conditional branching, and syscall usage in MIPS32.
Setting Up Your Lab3 Folder and Files
Create a folder named Lab3 (capital L, lowercase ab, digit 3) with no extra characters. Inside, place two files: Lab3.asm (your MIPS code) and README.txt. After writing your code, commit and push to your repository, then tag the commit as Lab3_submission_# (replace # with your submission number). This tag is critical for automated grading.
Understanding the Specification
The program must prompt the user with exactly Please input a positive integer: (note the colon and space). Then, for each integer from 0 up to (and including) the input number, print one of four outputs on a new line:
- If divisible by 5 and 7: Flux Bunny
- If divisible by 5 only: Flux
- If divisible by 7 only: Bunny
- Otherwise: the integer itself
After the loop, the program must exit cleanly using syscall 10. No extra spaces or characters – the output format is strict. For example, input 10 produces:
Flux Bunny
...followed by the program finished message.
MARS Setup and First Steps
Download MARS from the official site and open it. Create a new file and save it as Lab3.asm. You'll need to read Chapters 2, 3, and 7 of Introduction To MIPS Assembly Language Programming and run the Fibonacci.asm example to understand syscalls and register conventions.
Registers You Can Use
For this lab, use only $v0 and $a0 for syscalls, and the temporary registers $t0–$t9 for everything else. A common mapping:
# REGISTER USAGE
# $t0: user input (limit)
# $t1: loop counter (current number)You may also use $t2 for remainder calculations or string addresses.
Writing the MIPS Code – Step by Step
1. Header Comment
Start every program with a header block containing your name, CruzID, date, lab number, course, quarter, school, and description. Example:
################################################################################
# Created by: Doe, John
# jdoe123
# 13 May 2026
#
# Assignment: Lab 3: Looping in MIPS
# CMPE 012, Computer Systems and Assembly Language
# UC Santa Cruz, Spring 2026
#
# Description: Prints Flux, Bunny, or Flux Bunny based on divisibility.
#
# Notes: Run in MARS IDE.
################################################################################2. Data Section
Store your prompt and output strings in the .data segment:
.data
prompt: .asciiz "Please input a positive integer: "
flux: .asciiz "Flux"
bunny: .asciiz "Bunny"
fluxbunny: .asciiz "Flux Bunny"
newline: .asciiz "\n"These strings reside in the data segment at addresses assigned by the assembler. After assembling, you can find their addresses in the MARS Data Segment window – typically starting around 0x10010000.
3. Text Section – Main Program
In the .text segment, start with main:. Print the prompt using syscall 4, read an integer using syscall 5, store it in $t0, then initialize $t1 to 0 (loop counter).
.text
main:
li $v0, 4 # syscall for print string
la $a0, prompt
syscall
li $v0, 5 # syscall for read integer
syscall
move $t0, $v0 # $t0 = user input
li $t1, 0 # loop counter = 04. Loop Structure
Create a label loop:. Check if counter <= input; if not, exit. Inside the loop, check divisibility by 5 and 7 using the modulo operation (divide by 5 or 7 and check remainder). In MIPS, you can use the div instruction followed by mfhi to get the remainder.
loop:
bgt $t1, $t0, exit # if counter > input, exit
# Check divisibility by 5 and 7
li $t2, 5
div $t1, $t2
mfhi $t3 # remainder for 5
li $t2, 7
div $t1, $t2
mfhi $t4 # remainder for 7
# Decision logic
beqz $t3, div5 # if remainder 5 == 0, check further
beqz $t4, div7 # else if remainder 7 == 0, print Bunny
j print_num # else print numberAdd labels div5:, div7:, print_fluxbunny:, print_flux:, print_bunny:, print_num: and the associated syscalls. Each print should be followed by printing a newline. Then increment counter (addi $t1, $t1, 1) and jump back to loop.
div5:
beqz $t4, print_fluxbunny # divisible by both
j print_flux # only divisible by 5
div7:
j print_bunny # only divisible by 7
print_fluxbunny:
li $v0, 4
la $a0, fluxbunny
syscall
j print_newline
print_flux:
li $v0, 4
la $a0, flux
syscall
j print_newline
print_bunny:
li $v0, 4
la $a0, bunny
syscall
j print_newline
print_num:
li $v0, 1
move $a0, $t1
syscall
j print_newline
print_newline:
li $v0, 4
la $a0, newline
syscall
addi $t1, $t1, 1
j loop5. Exit Cleanly
exit:
li $v0, 10
syscallCode Formatting and Comments
Use consistent indentation – align instructions, operands, and comments with spaces (not tabs) for portability. Every block should have a comment explaining its purpose. Inline comments should be aligned for readability.
Example of Good Formatting
loop:
bgt $t1, $t0, exit # if counter > input, exit
li $t2, 5 # divisor = 5
div $t1, $t2 # divide counter by 5
mfhi $t3 # remainder in $t3Creating the README.txt
The README must be plain text (.txt) and follow the template. Include your name and CruzID. Answer these questions in at least 8 total sentences:
- After assembling, what is the address range of your strings? (Open MARS after assembling, go to Data Segment tab, note the addresses of
prompt,flux, etc. Typically they are in the .data segment starting around 0x10010000.) - What were the learning objectives? (MIPS loops, conditionals, syscalls, register usage, modular arithmetic.)
- Any issues? (e.g., off-by-one errors, remainder logic, string formatting.)
- How would you redesign? (e.g., use functions, add error handling, accept multiple inputs.)
Example snippet:
----------------
Lab 3: Looping in MIPS
CMPE 012 Spring 2026
Doe, John
jdoe123
----------------
The strings are stored starting at address 0x10010000 for the prompt, 0x10010020 for "Flux", etc. The learning objectives included understanding MIPS branching and syscalls. I encountered issues with the newline placement. I would redesign to allow negative input handling.Testing and Debugging
Assemble your code (F3) and run (F5). Test with small inputs like 5, 7, 35, and 10. Compare output to the specification. Common mistakes: forgetting the space after colon, extra spaces in output, missing newline after last line, using wrong syscall numbers, or not clearing remainder registers.
Trend Connection: Loop Logic in AI and Gaming
Just as your MIPS loop iterates over numbers and applies rules, modern AI models run loops over data to classify outputs. For example, a recommendation system checks conditions (user liked, watched, etc.) and returns relevant content. In gaming, a hit detection loop checks if a projectile collides with enemies – similar to checking divisibility conditions. This lab's logic is a microcosm of decision-making in algorithms across tech.
Final Checklist Before Submission
- Folder named
Lab3(no extra characters). - Files:
Lab3.asmandREADME.txt. - Header comment complete.
- Code assembles without errors.
- Output matches specification exactly: prompt, capitalization, newlines, no extra spaces.
- Exit syscall used.
- README answers all questions, total ≥8 sentences.
- Commit, push, and tag as
Lab3_submission_#.
Good luck! This lab builds essential assembly skills that translate to understanding how higher-level languages manage loops and conditions under the hood.