Exercise · Volume 1 · 1.4.2-6
Exercise 1.4.2-6
not stated yet in Lean solution written
| Rating | 22 |
|---|---|
| Kind | K6, write a program: write a MIX or MMIX program from the written solution |
| Section | Section 1.4.2 of Volume 1. Companion chapter: Coroutines |
The task
Give the coroutine linkage for three coroutines A, B and C where each can jump to each of the other two.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
In (1) there is one entry for each direction. How many directions are there for three coroutines?
Hint 2
Each coroutine has one resume cell. The six entries only differ in which cell they save the return address in.
Solution
Show the solution
Each coroutine has a resume cell (AX, BX, CX), which holds an instruction that jumps to the place where X is to go on. At the start these cells are JMP A1, JMP B1, JMP C1. Each pair has a separate entry, so a call JMP AB in A means "go to B":
AB STJ AX Save where A is to go on.
JMP BX Go on in B.
AC STJ AX
JMP CX
BA STJ BX
JMP AX
BC STJ BX
JMP CX
CA STJ CX
JMP AX
CB STJ CX
JMP BX
AX JMP A1
BX JMP B1
CX JMP C1
The entry AB is named by the pair (from, to). Whoever jumps to it saves rJ in the resume cell of the caller and goes to the resume cell of the other coroutine. Twelve instruction words and three resume cells are needed. For coroutines the number is words for entries.
Check
Give each of A, B and C a list of jumps to the others (A: B, C, C, B; B: C, A, A, C; C: A, B, B, A, each in a cycle) and let each routine record its own letter before every jump. A hand trace and a run on a MIX simulator gave the record ABCACBACBABC for its first twelve steps, and the same as a model in Python in which each coroutine remembers its place.
Remarks
If a coroutine only has to jump to some of the others, one needs only the entries that the program uses.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.