
MIPS Assembly: A Program One Instruction at a Time
A 249-line MIPS program asks how many numbers to read, stores them in memory it requests at run time, prints them as a list, and reports the sum of the even ones. A 22-line Python script defines the behavior the assembly has to reproduce.
249
lines of MIPS assembly
22
lines in the Python reference
5
values at most
22
even sum in the test run
Problem
Understanding the challenge and user needs
A high-level language hides the machine. A loop, a list, and a modulo operator each stand for work the processor does with registers, memory addresses, and jumps, and assembly makes every one of those steps explicit.
One line of Python, several instructions of MIPS
| In Python | In MIPS |
|---|---|
| value % 2 == 0 | div, then mfhi to read the remainder, then bnez to branch |
| int_values.append(x) | Store at the current address, then advance the pointer four bytes |
| for value in int_values | Compare an index register with the count, then jump back |
| input() | Load a system call number into a register, then syscall |
Approach
Strategy and methods used to address the problem
The program validates the count against a maximum of five, requests heap memory with a system call, and writes each value four bytes after the last. A second loop loads each value, divides it by two, reads the remainder from the HI register, and adds the value to a running sum when the remainder is zero.
Final Solution
The final design and outcome
For the inputs 4, 7, 10, 3, and 8 the program prints the list and an even sum of 22, matching the Python reference. A request for six values draws the same refusal in both versions.
(opens in a new tab)Impact & Validation
Results, learnings, and outcomes
Writing a list and a loop by hand shows what every later abstraction costs and what it buys.
