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-8

Exercise 2.2.5-8

not stated yet in Lean solution written

Rating21
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 step E8 of the elevator program, which the text leaves out.

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

Step E8 is the mirror image of E7. Floors are lower, the call variables to test are CALLCAR\mathrm{CALLCAR} and CALLDOWN\mathrm{CALLDOWN}, and the times are 61 and 23.

Hint 2

The CALL\mathrm{CALL} table has padding words on both sides, so that the sums over the floors below the first one read zeros.

Solution

Show the solution

Index register 4 is FLOOR\mathrm{FLOOR}, and rA holds 15 when E8A is entered from E6. The fields (1:1)(1{:}1), (3:3)(3{:}3) and (5:5)(5{:}5) of CALL+j\mathrm{CALL}+j are CALLUP\mathrm{CALLUP}, CALLCAR\mathrm{CALLCAR} and CALLDOWN\mathrm{CALLDOWN}.

E8A   JMP   HOLDC            Wait 15 units, then continue at E8.
E8    DEC4  1                E8. Go down a floor.
      ENTA  61
      JMP   HOLDC            Wait 61 units.
      LDA   CALL,4(3:5)      CALLCAR or CALLDOWN of this floor set?
      JANZ  5F               Yes: stop.
      ENT1  -2,4
      J1Z   4F               Floor 2 stops if nothing is called below.
      LDA   CALL,4(1:1)      Is CALLUP of this floor set?
      JAZ   E8               No: go on down.
4H    LDA   CALL-1,4         Calls for lower floors?
      ADD   CALL-2,4
      ADD   CALL-3,4
      ADD   CALL-4,4
      JANZ  E8               Yes: go on down.
5H    ENTA  23               Time to stop the elevator.
      JMP   E2A              Wait 23 units and go to E2.

The first test stops the elevator at the floor if the car button or the down button is set. Otherwise it stops when the floor is the floor 22, or when the up button is set, provided no floor below it has a call. The sums add whole words, and the three fields of a word are at most 1 each, so there is no carry between the fields.

Check

I tested the code in a simulator of MIX with stubs for HOLDC and E2A, on 1294 random call tables and starting floors. The elevator stopped on the floor that the rule of step E8 gives, the number of waits was right, and the final delay was 23 each time.

Remarks

The extra up-button test on a floor other than 22 is the mirror of the down-button test in E7. The program does not stop the elevator for a lone up call when there are calls further down.

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.