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

Exercise 1.3.1-22

not stated yet in Lean solution written

Rating28
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.3.1 of Volume 1. Companion chapter: Description of MIX

The task

Write two programs that compute X13X^{13} for the integer in cell 2000, one with the least number of words and one with the least running time.

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 condition that X13X^{13} fits in one word limits XX strongly. See how large ∣X∣|X| can be for b=64b = 64 and for b=100b = 100.

Hint 2

For the short program use a loop that multiplies by XX twelve times.

Hint 3

For the fast program think about a table.

Solution

Show the solution

Plan: short program: a loop with a counter; fast program: a table of the 1313th powers, indexed by XX.

Because 613=13060694016>10056^{13} = 13060694016 > 100^5, the condition X13<b5X^{13} < b^5 gives ∣X∣≤5|X| \le 5 for every bb. For b=64b = 64 it gives ∣X∣≤4|X| \le 4, because 513=1220703125>645=10737418245^{13} = 1220703125 > 64^5 = 1073741824.

Short program (seven words):

        ORIG 3000
START   ENT4 12
        LDA  2000
LOOP    MUL  2000
        SLAX 5
        DEC4 1
        J4P  LOOP
        HLT

The register rA starts as XX. Each pass computes rAX=rA⋅X\text{rAX} = \text{rA} \cdot X, and SLAX 5 moves the low five bytes back into rA. The product fits in a word, because it is a power of XX below X13X^{13}. After twelve passes rA is X13X^{13}. The time is 1+2+12⋅(10+2+1+1)=171u1 + 2 + 12 \cdot (10 + 2 + 1 + 1) = 171u without the HLT. Each of the seven words has its own task: the HLT, the load of XX, the multiply, the move of the product back to rA, and the three instructions of the loop counter (set, decrease, test). A program with fewer words would need a different method.

Fast program (a table, three instructions and the table of eleven words):

        ORIG 3000
START   LD1  2000
        LDA  TABLE,1
        HLT
TABLE   EQU  3500
        ORIG 3495
        CON  -1220703125
        CON  -67108864
        CON  -1594323
        CON  -8192
        CON  -1
        CON  0
        CON  1
        CON  8192
        CON  1594323
        CON  67108864
        CON  1220703125

Here the table has the eleven words x13x^{13} for x=−5,…,5x = -5, \dots, 5, put at the cells 3495,…,35053495, \dots, 3505 so that the cell 3500+x3500 + x holds x13x^{13}. For b=64b = 64 the entries for ±5\pm 5 are not needed. The value XX fits in two bytes, so LD1 2000 is legal. The time is 2+2=4u2 + 2 = 4u without the HLT. No program can be faster: it must read the cell 2000 (2u2u at least) and then get a new word in rA by an instruction that reads memory (2u2u at least), because an instruction of 1u1u cannot make an arbitrary word.

Check

Both programs ran for all XX from −5-5 to 55 with b=100b = 100 and for −4≤X≤4-4 \le X \le 4 with b=64b = 64. Each gave rA =X13= X^{13} exactly, and the times were 171u171u and 4u4u. The numbers: 5**13 = 1220703125, 6**13 = 13060694016, 100**5 = 10**10, 64**5 = 1073741824.

Remarks

Section 4.6.3 shows that five multiplications are needed for a generic X13X^{13} by addition chains. The loop uses twelve and the table uses none. An addition chain for 1313 has five steps and no fewer (Section 4.6.3), so a program with fewer than five multiplications has to use the byte structure of the words.

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.