The Gap Between What You Write and What Runs
- Parsed and compiled into bytecode by the Python interpreter
- The bytecode has been evaluated by the CPython virtual machine
- The VM has issued machine code instructions to the CPU
- The CPU has fetched operands from memory into registers, executed an ALU operation, and written the result back
What MIPS Assembly Actually Looks Like
Python
def gaussian_sum(n):
return n * (n + 1) // 2
Mips
# $a0 = n (argument)
# $v0 = return value
gaussian_sum:
addi $t0, $a0, 1 # $t0 = n + 1
mul $v0, $a0, $t0 # $v0 = n * (n + 1)
srl $v0, $v0, 1 # $v0 = result / 2 (arithmetic right shift)
jr $ra # return
Memory Operations and Addresses
- Calculating the address of each character in the string
- Loading characters from those addresses into registers
- Comparing and swapping characters by writing to the appropriate addresses
- Tracking array bounds explicitly to avoid reading past the end of allocated memory
C and Data Structures (COMP2521)
- Counts character frequencies in the input
- Builds a priority queue (min-heap) of frequency nodes
- Repeatedly extracts the two lowest-frequency nodes and merges them into a parent node
- Generates variable-length binary codes from the resulting tree (frequent characters get shorter codes)
- Encodes the input using the generated codes
- Random input of varying sizes
- Already-sorted input
- Reverse-sorted input
- Input with many repeated values
What This Changes
The Uncomfortable Part
Recommended Approach
- Read the MIPS instruction set reference regularly. You will forget which instructions exist and what they do. Having a reference open is not a crutch — it's normal.
- Use MARS or SPIM to step through execution. Watching the register values change with each instruction builds intuition faster than any other method.
- Write the high-level algorithm first. Knowing what you're trying to implement at an abstract level before translating to assembly reduces the cognitive load significantly.
- Test edge cases explicitly. Empty strings, zero values, maximum sizes — these are exactly the cases where off-by-one errors and memory mismanagement show up.