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

Exercise 1.4.1-1

not stated yet in Lean solution written

Rating10
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 1.4.1 of Volume 1. Companion chapter: Subroutines

The task

Write down the calling sequence, the entry conditions and the exit conditions of subroutine (5), which has two entrances, in the style of the specification of Program 1.3.2M.

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

Read what the two entrances do before they join: one of them loads n=100n = 100 itself, the other expects nn in a register.

Hint 2

The exit conditions are those of MAXIMUM, with one register change: look at which index register carries the parameter now, and which registers the code uses.

Solution

Show the solution

The subroutine has two entrances.

  • Calling sequence: JMP MAX100 when n=100n = 100, or JMP MAXN for general nn.
  • Entry conditions: for the entrance MAXN, rI3 =n= n, and n≥1n \ge 1 is assumed. The entrance MAX100 has no entry condition, since it sets rI3 ←100\leftarrow 100 itself.
  • Exit conditions: rA =max⁡1≤k≤nCONTENTS(X+k)=CONTENTS(X+rI2)= \max_{1 \le k \le n} \text{CONTENTS}(X + k) = \text{CONTENTS}(X + \text{rI2}), rI3 =0= 0, and rI2 holds the largest index at which the maximum occurs. The registers rI1 and rX are not changed. The registers rJ and the comparison indicator are changed (see Exercise 1.4.1-3 for their exact values). No memory cell outside the subroutine is changed.

Two details need care. First, the parameter nn is in rI3 on entry and not in rI1, so the caller can leave rI1 alone. Second, MAX100 is a real entrance and not a call of MAXN: the instruction ENT3 100 does not change rJ, so the following STJ EXIT still stores the return address of the original JMP MAX100.

Check

Put the values 5,9,2,95, 9, 2, 9 in X+1,…,X+4X + 1, \ldots, X + 4 and call MAXN with rI3 =4= 4. Exit conditions: rA =9= 9, rI2 =4= 4 (the last of the two nines) and rI3 =0= 0. A run of the code on a MIX simulator, with random tables of length 1,2,51, 2, 5 and 100100, gave these values every time and left rI1 and rX alone.

Remarks

The characteristics of a subroutine are its contract with the caller. A caller may rely on every register that the list does not mention. See the C1 chapter on Section 1.4.1.

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.