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 · 2.2.5-9

Exercise 2.2.5-9

not stated yet in Lean solution written

Rating23
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 2.2.5 of Volume 1

The task

Write the code for the subroutine DECISION that the text omits.

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 subroutine is entered by JMP, by JAZ and by J5Z from different places. It returns to the location that follows the jump.

Hint 2

In step D3 the behaviour depends on whether E6 called the subroutine. The return location tells it, since E6 calls it from just before line E6B.

Hint 3

Register 6 holds the current node, and HOLD uses it. Save it around the call.

Solution

Show the solution

Index register 4 is FLOOR\mathrm{FLOOR}, register 5 is the state, and it is positive, negative or zero for GOINGUP\mathrm{GOINGUP}, GOINGDOWN\mathrm{GOINGDOWN}, NEUTRAL\mathrm{NEUTRAL}. The registers A, X, 1, 2, 3 may be changed. Registers 4 and 6 must come back as they were, and register 5 may be changed.

DECISION STJ  9F             Store the return address.
      J5NZ  9F               D1. Nothing to do unless NEUTRAL.
      LDX   ELEV1+2(NEXTINST)
      DECX  E1               rX = 0 if the elevator is at E1.
      JXNZ  1F
      LDA   CALL+2           D2. Any call on floor 2?
      JANZ  7F               Yes: schedule E3.
1H    ENT1  -4               D3. Look for the smallest j, j differing from FLOOR.
2H    ENTA  4,1
      DECA  0,4              j - FLOOR.
      JAZ   3F
      LDA   CALL+4,1         Calls on floor j?
      JANZ  5F               Found.
3H    INC1  1
      J1NP  2B
      LDA   9F(0:2)          None found. Who called us?
      DECA  E6B
      JANZ  9F               Not E6: leave.
      ENT1  -2               From E6: take j = 2.
5H    ENT2  4,1              D4. Set the state from j - FLOOR.
      DEC2  0,4
      ENT5  0,2              Zero if j = FLOOR.
      JXNZ  9F               D5. The elevator is not dormant: leave.
      ENT3  2,1
      J3Z   9F               j = 2: leave.
      ENTA  E6               Schedule E6.
      JMP   8F
7H    ENTA  E3               Schedule E3.
8H    ST6   SAV6             Save the current node.
      ENT6  ELEV1
      STA   ELEV1+2(NEXTINST)
      ENTA  20
      JMP   HOLD             Run the elevator after 20 units.
      LD6   SAV6             Restore the current node.
9H    JMP   *
SAV6  CON   0

At the label 5H the register 1 holds j−4j - 4. In the case where the loop finds no jj and E6 is the caller, register 1 is set to −2-2, that is j=2j = 2. The register 5 gets j−FLOORj - \mathrm{FLOOR}, so its sign is the state. The test at D5 uses rX from the beginning, which no instruction after it has changed.

Check

I tested the code in a simulator with a stub for HOLD, on 1500 random combinations of floor, state, call table, position of the elevator and caller. In each case the new state, the scheduled action, and the registers 4 and 6 equalled the values given by a direct python version of steps D1 to D5.

Remarks

When DECISION is called from step E6 the elevator coroutine is at E6 and not at E1, so D2 and D5 do nothing there. The calls from U2 and E9 are the ones for which the elevator can be at E1.

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.