taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.4.2-5

Exercise 1.4.2-5

not stated yet in Lean solution written

Rating15
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.4.2 of Volume 1. Companion chapter: Coroutines

The task

Give the linkage for IN and OUT so that the content of register A is the same on return from a jump to the other coroutine as it was before the jump.

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

The linkage (4) of the text saves A for one direction only. Do the same for the other direction.

Hint 2

Each coroutine needs a cell to hold the A of the one that is asleep.

Solution

Show the solution

Each time control leaves a coroutine, its A is stored in a cell. Each time control enters a coroutine, its A is loaded back from its cell. The new linkage is:

OUT   STJ  INX
      STA  HOLDIN      Save the A of IN, which is going to sleep.
      LDA  HOLDOUT     Take back the A of OUT.
OUTX  JMP  OUT1
IN    STJ  OUTX
      STA  HOLDOUT     Save the A of OUT.
      LDA  HOLDIN      Take back the A of IN.
INX   JMP  IN1

The cells HOLDIN and HOLDOUT are in ordinary memory. The first time that a coroutine is entered, its loaded value is not important.

Check

Hand trace. Let IN keep a counter in rA that begins at 100100 and grows by 11, and OUT a counter that begins at 500500 and grows by 1010. Each routine records rA and then jumps to the other. The record must be 500,100,510,101,520,102,530,103500, 100, 510, 101, 520, 102, 530, 103. A run on a MIX simulator gave exactly this list.

Remarks

Each coroutine saves only A, so the cost is two memory references per switch. If more registers must be kept, save them in the same way. One cell per register and per coroutine is needed.

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.