TAOCP 1.4.1 Exercise 1

Calling sequence: either $\mathrm{JMP}\ MAX100$ or $\mathrm{JMP}\ MAXN$.

Section 1.4.1: Subroutines

Exercise 1. [10] State the characteristics of subroutine (5), just as (4) gives the characteristics of Subroutine 1.3.2M.

Verified: yes
Solve time: 1m35s


Calling sequence: either $\mathrm{JMP}\ MAX100$ or $\mathrm{JMP}\ MAXN$.

Entry conditions:

For $\mathrm{MAX100}$, the instruction $\mathrm{ENT3}\ 100$ has been executed, so $rI3 = 100$.

For $\mathrm{MAXN}$, $rI3 = n$, where $n \ge 1$.

Register $rI2$ is not required to be initialized in either entry (its initial value is irrelevant).

Registers $rX$ and $rI1$ contain the base address information for the array $X$, and are unchanged by the calling sequence.

Exit conditions:

The subroutine computes

$$ rA = \max_{1 \le k \le n} \mathrm{CONTENTS}(X + k), $$

and also determines an index $k$ at which this maximum occurs. This index is returned explicitly in

$$ rI2 = k \quad \text{where } \mathrm{CONTENTS}(X+k) = rA. $$

The loop counter is cleared:

$$ rI3 = 0. $$

Register preservation properties are:

  • $rX$ and $rI1$ are unchanged.
  • $rJ$ is altered by the subroutine.
  • The comparison indicator is altered.
  • $rA$, $rI2$, and $rI3$ are outputs and therefore overwritten.

Thus the full postcondition is that $rA$ holds the maximum value in $\mathrm{CONTENTS}(X+1), \ldots, \mathrm{CONTENTS}(X+n)$, and $rI2$ identifies a position where this maximum is attained, while $rI3$ is reset to zero.