Exercise · Volume 1 · 1.4.2-5
Exercise 1.4.2-5
not stated yet in Lean solution written
| Rating | 15 |
|---|---|
| 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 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 and grows by , and OUT a counter that begins at and grows by . Each routine records rA and then jumps to the other. The record must be . 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.