# TAOCP 1.4.1 Subroutines

> Study guide to TAOCP 1.4.1 Subroutines. How to package code as a subroutine in MIX: linkage, passing parameters, returning, and the trade-offs between…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 1.4.1 Subroutines: How to package code as a subroutine in MIX: linkage, passing parameters, returning, and the trade-offs between in-line code and calls. Budget about 5 hours; do exercises 1, 2 and 4.

MIX has no stack or call instruction in the modern sense, so you see the mechanics explicitly.

## Key facts {#key-facts}

- Week 45 of 145 on the cracked.guide path, in [TAOCP Vol 1: Fundamental Algorithms](https://cracked.guide/taocp/volume-1).
- Time budget: about 5 hours.
- 7 exercises in the section, rated 10 to 26; the guide picks 6.
- Picked exercises with Knuth's triangle for recommended: 4, 6 and 7.
- Before you start: 1.3.2 The MIX Assembly Language.

## Week 45 goals {#objectives}

By the end of week 45 you can:

- Implement a MIX subroutine that returns through the rJ register.
- Explain how the entry code stores the return address into the exit jump.
- Compare in-line code with subroutine calls in space and time.

## This week's steps {#steps}

- Read §1.4.1 Subroutines: Write a MIX subroutine and call it from two places.
- Work the exercises (6 picked): Start with Ex 1, Ex 2, Ex 4; log what you solve below.
- Write down one thing you'd explain to someone: A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

## Exercises {#exercises}

This section has 7 exercises, rated 10 to 26, and Knuth marks three of them with the triangle for recommended. Small set, all worth doing. Write and run 4 and 6.

Worth your time:

- 1 (rated 10): Write out the interface description for one of the text's subroutines.
- 2 (rated 10): Replace a JSJ-based calling sequence with ordinary jumps.
- 4 (rated 21, recommended): Generalize the max subroutine to a strided range with extra parameters.
- 5 (rated 21): Invent subroutine linkage for a MIX without rJ.
- 6 (rated 26, recommended): Write a subroutine that mimics the MOVE instruction, with its parameters stored right after the call.
- 7 (rated 20, recommended): Explain why self-modifying code fell out of favor.

## Why it matters {#why}

Calling conventions are still the glue of every compiled program. Seeing them built by hand makes stack frames, return addresses and ABIs make sense, and connects back to the call frames you built in clox.

## Before you start {#before}

- 1.3.2 The MIX Assembly Language

## How to approach it {#approach}

Write a MIX subroutine and call it from two places. Then compare the linkage mechanism to how clox pushes CallFrames.

## Common traps {#traps}

- Self-modifying return jumps are normal in MIX. Do not let that surprise you; it is a product of the era.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 1 errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err1.textxt)
- [Wikipedia: Subroutine](https://en.wikipedia.org/wiki/Subroutine)
- [Wikipedia: Calling convention](https://en.wikipedia.org/wiki/Calling_convention)

---

Source: https://cracked.guide/guide/knuth-w031-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
