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

Exercise 1.4.1-3

not stated yet in Lean solution written

RatingM15
MarkersM (mathematical)
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

Complete the exit conditions of the subroutine MAXIMUM: say exactly what rJ and the comparison indicator contain at the end, and what the subroutine does when rI1 is not positive.

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

Which instruction is the last one that changes rJ, and what address does it store?

Hint 2

Find the last comparison made by the loop, and which two values it compares.

Hint 3

For n≤0n \le 0 follow the code by hand through the first pass of the three starting instructions.

Solution

Show the solution

Let the names be those of Program 1.3.2M, with EXIT the location of the final JMP *.

The register rJ. A jump instruction sets rJ to the address of the next instruction whenever the jump is taken, except JSJ. The return JMP * at EXIT is taken, so at the end rJ =EXIT+1= \text{EXIT} + 1, independent of the data.

The comparison indicator, when n≥1n \ge 1. If n=1n = 1 the loop does no comparison, so the indicator does not change. If n≥2n \ge 2 the last comparison is CMPA X,3 with rI3 =1= 1. At this time rA holds the maximum m′m' of X[2],…,X[n]X[2], \ldots, X[n] (the last of the maximal elements), and the comparison is between m′m' and X[1]X[1]. So the indicator ends as

  • greater, if m′>X[1]m' > X[1],
  • equal, if m′=X[1]m' = X[1] (then JGE skips the change and rI2 stays larger than 11),
  • less, if m′<X[1]m' < X[1] (then rI2 is set to 11, and X[1]X[1] is the unique maximum).

The case n≤0n \le 0. Now ENT3 0,1 gives rI3 =n≤0= n \le 0, and JMP CHANGEM runs the instructions ENT2 0,3, LDA X,3, DEC3 1 without any test. So rI2 =n= n, rA =CONTENTS(X+n)= \text{CONTENTS}(X + n) and rI3 =n−1= n - 1. Since rI3 is negative, J3P does not jump and the subroutine exits. The indicator is not changed. This assumes that X+nX + n is a valid address.

Summary of the exit conditions. For n≥1n \ge 1: rA, rI2, rI3 as stated, rJ =EXIT+1= \text{EXIT} + 1, and the indicator as above. For n≤0n \le 0: rA =CONTENTS(X+n)= \text{CONTENTS}(X + n), rI2 =n= n, rI3 =n−1= n - 1, rJ =EXIT+1= \text{EXIT} + 1 and the indicator unchanged.

Check

Table 4,4,14, 4, 1 (n=3n = 3): the loop compares 44 with 11 (greater) and 44 with 44 (equal, no change). The result is rA =4= 4, rI2 =2= 2 and the indicator is equal. Table 4,1,14, 1, 1: the last comparison is of 11 with 44, which is less, and rI2 =1= 1. For n=0n = 0 with X[0]=555X[0] = 555: rA =555= 555, rI2 =0= 0, rI3 =−1= -1. A simulator run of these cases and of random tables gave the same results.

Remarks

The behaviour for n≤0n \le 0 is not a feature to rely on. It is a result of the loop test being at the end. The exit value of rI3 for a loop of this form is always the first value of the counter that fails the test.

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.