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

Exercise 1.4.1-2

not stated yet in Lean solution written

Rating10
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.4.1 of Volume 1. Companion chapter: Subroutines

The task

Give code for the extra entrance MAX50 that avoids the instruction JSJ.

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 job of JSJ MAXN in (6) is to enter MAXN without changing rJ, so that STJ EXIT records the right address.

Hint 2

You can do that job yourself: store rJ into the exit cell before the new code jumps into the common body.

Solution

Show the solution

The entrance MAXN begins with STJ EXIT and then jumps to the body. Make a new entrance that does the same two things in another order, with its own ENT3:

MAX50  ENT3 50
       STJ  EXIT
       JMP  TWO       TWO is the label of the first instruction of the body.

ENT3 does not change rJ, so STJ EXIT stores the address that follows the caller's JMP MAX50. The last JMP goes to the body at its entrance TWO, where ENT2 0,3 starts the search with k=nk = n. The effect is the same as that of (6) followed by (5). It costs one more instruction than (6), but no instruction that leaves rJ alone is used.

Check

Load X[i]=iX[i] = i for i≤50i \le 50 and X[i]=9999X[i] = 9999 for i>50i > 50 and call JMP MAX50. The result is rA =50= 50, rI2 =50= 50, rI3 =0= 0: the elements beyond the fiftieth are not looked at. A simulator run gave these values and returned to the instruction after the call.

Remarks

The saving of (6) is one instruction. Section 1.4.1 uses JSJ for the same reason that a goto that skips linkage is sometimes useful: it enters a routine while keeping the return address of a caller.

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.