TAOCP Vol 1: Fundamental Algorithms · Week 31

TAOCP 1.4.2 Coroutines: reading guide

About the book

Coroutines: routines that call each other as equals, each resuming where it left off. Knuth shows a pipeline example where one coroutine produces characters and another consumes and transforms them.

Time About 4 hours this week

Why it matters

Generators, async/await, Go's goroutines and Lua coroutines all descend from this idea. Seeing it in assembly makes the "where does my state live" question concrete.

Before you start

  • 1.4.1 Subroutines

How to approach it

Trace the example with two columns on paper, one per coroutine, marking where control jumps. Then rewrite the example with Python generators or JavaScript generators and see how much simpler it gets.

Exercises

Do the 00 to 15 exercises to make sure you can read MIX fluently. Then write and actually run at least two of the 20-ish programming exercises in a MIX simulator (GNU MDK works). Reading assembly is not the same as writing it; the bugs you hit are the lesson.

Traps

  • Thinking coroutines need threads. They do not; they are just saved resume points.

Links

Back to week 31