Skip to content
MIPS Assembly: A Program One Instruction at a Time

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.

MIPS AssemblyComputer ArchitectureMemory Management

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 PythonIn MIPS
value % 2 == 0div, 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_valuesCompare 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.

The program opens by setting up its registers and refusing any count above the maximum.
The even-sum loop: load a value, divide by two, take the remainder from the HI register, and add the value only 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.

Terminal session of the Python reference program rejecting six values and summing the even ones (opens in a new tab)
The Python reference run with the test inputs: six values refused, five accepted, and an even sum of 22.

Impact & Validation

Results, learnings, and outcomes

Writing a list and a loop by hand shows what every later abstraction costs and what it buys.