TAOCP Vol 1: Fundamental Algorithms · Week 45

TAOCP 1.4.1 Subroutines

Updated

About 5 hours7 exercises, rated 10 to 26

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.

Flashcards 12 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

3 steps
  1. Read §1.4.1 Subroutines

    Write a MIX subroutine and call it from two places.

  2. Work the exercises (6 picked)

    Start with Ex 1, Ex 2, Ex 4; log what you solve below.

  3. 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.

Sign in to track this: tick each step off and it counts toward your week.

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

  • Exercise 110

    Write out the interface description for one of the text's subroutines.

    Discuss

  • Exercise 210

    Replace a JSJ-based calling sequence with ordinary jumps.

    Discuss

  • Exercise 421▶ Recommended

    Generalize the max subroutine to a strided range with extra parameters.

    Discuss

  • Exercise 521

    Invent subroutine linkage for a MIX without rJ.

    Discuss

  • Exercise 626▶ Recommended

    Write a subroutine that mimics the MOVE instruction, with its parameters stored right after the call.

    Discuss

  • Exercise 720▶ Recommended

    Explain why self-modifying code fell out of favor.

    Discuss

Your log

Sign in to log the exercises you work: number, rating, how far you got.

Your notes

Only you

Sign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.

Discussion

0 posts

Questions about the reading go here; each exercise has its own thread below. Hints over answers.

No posts yet. Ask a question or share a tip.

Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Exercise threads

One thread per exercise. Open one to ask for a nudge or check your thinking.

  • Exercise 110No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 210No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 421No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 521No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 626No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 720No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Reference

Why it matters

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
  • 1.3.2 The MIX Assembly Language
How to approach it

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

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

Spot a mistake? Report it

Back to week 45