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.3.1-2

Exercise 1.4.3.1-2

not stated yet in Lean solution written

Rating20
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.4.3.1 of Volume 1. Companion chapter: A MIX simulator

The task

Write the routine SHIFT for operation code 6 of the simulator: the six shifts SLA, SRA, SLAX, SRAX, SLC, SRC.

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

At the entry of an operator routine, rI5 holds MM and rI3 holds the F-field. Operation code 6 uses F=0,…,5F = 0, \ldots, 5 only.

Hint 2

The simulator keeps the registers as a magnitude and a separate sign. Shifts do not change the signs. So the shift can be done on the magnitudes with the real shift instructions.

Hint 3

Use the existing routine STOREAX to store the result: it stores both magnitudes and checks the overflow toggle.

Solution

Show the solution

A shift by a negative MM is not allowed, so M<0M < 0 is an address error. A value F>5F > 5 is a field error. Otherwise load the two magnitudes into the real rA and rX, do the real shift by MM, and go to STOREAX.

The real instruction SLA 0,5 has the effective address MM because rI5 =M= M. A jump table, indexed by FF, selects one of six such instructions, so that no instruction is changed.

SHIFT   J5N  ADDRERROR      M negative: error.
        DEC3 5
        J3P  FERROR         F > 5: error.
        LDA  AREG           Magnitudes of the simulated A and X.
        LDX  XREG
        JMP  SHTAB+5,3      rI3 = F - 5, so this goes to entry F.
SHTAB   JMP  SHSLA
        JMP  SHSRA
        JMP  SHSLAX
        JMP  SHSRAX
        JMP  SHSLC
        JMP  SHSRC
SHSLA   SLA  0,5
        JMP  STOREAX
SHSRA   SRA  0,5
        JMP  STOREAX
SHSLAX  SLAX 0,5
        JMP  STOREAX
SHSRAX  SRAX 0,5
        JMP  STOREAX
SHSLC   SLC  0,5
        JMP  STOREAX
SHSRC   SRC  0,5
        JMP  STOREAX

The signs SIGNA and SIGNX are not touched, and this is right: every shift of MIX keeps the signs of rA and rX, also when the magnitude becomes zero. The routine STOREAX stores the two magnitudes, and it jumps to OVCHECK, which clears nothing and sets nothing, because a shift cannot overflow. The shifts by a large MM (up to 4095) are done by the real machine, which gives zero for the non-circular shifts and the shift by M mod 10M \bmod 10 for the circular ones.

Check

The simulator program of the text was run with this routine on a MIX simulator. Six hundred random cases were tried: all six shifts, M∈{0,1,2,3,4,5,6,9,10,11,15,100,4095}M \in \{0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 15, 100, 4095\}, random five-byte magnitudes and random signs. After each simulated shift, the simulated rA and rX and their signs were equal to those that the real instruction gives on the host.

Remarks

This solution takes the simplest route: it uses the real shifts for the simulation. A simulator for another computer must do the shifts with the operations of that machine, by multiplication and division by powers of 64. The time of the simulated shift is the table value 2.

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.