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

Exercise 1.4.1-5

not stated yet in Lean solution written

Rating21
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

Invent a way to link to a subroutine on a machine without a J-register, and write MAX100 in this style with its characteristics.

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 return address must reach the subroutine from the caller. Which register or cell can the caller load with it?

Hint 2

The result registers of MAX100 are rA, rI2, rI3. Which register does MAX100 not promise to keep?

Hint 3

The old convention stores the return address into the address field of the exit instruction. You can do this with an ordinary store.

Solution

Show the solution

Idea. The caller loads the return address into a register that the subroutine may destroy, and the subroutine stores it into the address field of its exit instruction. Here the register is rA, because MAX100 returns its result in rA anyway, so nothing that the caller wants to keep is lost.

Calling sequence:

       ENTA *+2        rA <- address of the instruction after the JMP.
       JMP  MAX100

The subroutine:

MAX100 STA  EXIT(0:2)  Put the return address into the address field of EXIT.
       ENT3 100
       JMP  2F
1H     CMPA X,3
       JGE  *+3
2H     ENT2 0,3
       LDA  X,3
       DEC3 1
       J3P  1B
EXIT   JMP  *          Address field was set by the first line.

The word that ENTA *+2 creates has the address in its two last bytes and sign plus. The store STA EXIT(0:2) copies the sign and the two last bytes into the address field of the exit instruction, and the other fields keep their values. The final JMP * therefore goes to the location after the caller's JMP. The instruction JMP after the store is an ordinary jump, so rJ is still changed by the hardware, but the subroutine never reads it.

Characteristics.

  • Calling sequence: ENTA *+2 followed by JMP MAX100.
  • Entry conditions: none; rA is overwritten.
  • Exit conditions: rA =max⁡1≤k≤100CONTENTS(X+k)=CONTENTS(X+rI2)= \max_{1 \le k \le 100} \text{CONTENTS}(X + k) = \text{CONTENTS}(X + \text{rI2}), rI3 =0= 0, the comparison indicator is changed. Other registers are unchanged.

Check

Fill X[i]=(37i) mod 101X[i] = (37 i) \bmod 101 for 1≤i≤1001 \le i \le 100 and call the routine, with the instruction ENT4 77 after the call. A simulator run returned rA =100= 100, rI2 =30= 30 (since 37⋅30=1110=10⋅101+10037 \cdot 30 = 1110 = 10 \cdot 101 + 100), and then executed ENT4 77, which shows that the return went to the right place.

Remarks

A machine without a link register usually puts the return address on a stack or in a general register, as modern machines do. The calling sequence here costs one extra instruction per call. See Exercise 1.4.1-7 for why one avoids the modification of the exit instruction.

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.